Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUESTION] Seed does not rollback in test #460

Open
MP-okui opened this issue Sep 29, 2020 · 5 comments
Open

[QUESTION] Seed does not rollback in test #460

MP-okui opened this issue Sep 29, 2020 · 5 comments

Comments

@MP-okui
Copy link

MP-okui commented Sep 29, 2020

I expect a seed rollback.

Package version, Laravel version

  • laravel-doctrine/orm v1.6.1
  • laravel/framework v7.25.0

Steps to reproduce the behaviour

Using the RefreshDatabase.

<?php

namespace Tests\Traits;

use Illuminate\Contracts\Console\Kernel;
use Illuminate\Foundation\Testing\RefreshDatabase as IlluminateRefreshDatabase;
use Illuminate\Foundation\Testing\RefreshDatabaseState;

trait RefreshDatabase
{
    use IlluminateRefreshDatabase;

    /**
     * Define hooks to migrate the database before and after each test.
     *
     * @return void
     */
    public function refreshDatabase()
    {
        $this->refreshTestDatabase();
    }

    /**
     * Refresh a conventional test database.
     *
     * @return void
     */
    protected function refreshTestDatabase()
    {
        if (! RefreshDatabaseState::$migrated) {
            $this->artisan('doctrine:schema:drop --force');
            $this->artisan('doctrine:schema:create');

            $this->app[Kernel::class]->setArtisan(null);

            RefreshDatabaseState::$migrated = true;
        }

        $this->beginDatabaseTransaction();
    }

    /**
     * Begin a database transaction on the testing database.
     *
     * @return void
     */
    public function beginDatabaseTransaction()
    {
        $connection = $this->app->make('em')->getConnection();
        $connection->beginTransaction();

        $this->beforeApplicationDestroyed(function () use ($connection) {
            $connection->rollBack();
        });
    }
}

Execute seed() with setUp().

<?php

namespace Tests\Feature;

use Tests\TestCase;
use Tests\Traits\RefreshDatabase;

class ExampleTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();

        $this->seed();
    }
}

Is it because not using EntityManager in seed()?

@eigan
Copy link
Member

eigan commented Sep 30, 2020

Have you confirmed that beginDatabaseTransaction have been called?

@MP-okui
Copy link
Author

MP-okui commented Sep 30, 2020

beginDatabaseTransaction() is being called.

    public function beginDatabaseTransaction()
    {
        $connection = $this->app->make('em')->getConnection();
        $connection->beginTransaction();
        Log::debug('run beginTransaction()');

        $this->beforeApplicationDestroyed(function () use ($connection) {
            $connection->rollBack();
            Log::debug('run rollBack()');
        });
    }
    protected function setUp(): void
    {
        parent::setUp();

        $this->seed();
        Log::debug('run seed()');
    }

    public function testFoo()
    {
        Log::debug('run testFoo()');
        entity(ExampleEntity::class)->create();
    }

    public function testBar()
    {
        Log::debug('run testBar()');
        entity(ExampleEntity::class)->create();
    }

This is the log.
I get an error on the second seed() call. (Duplicate primary key)
The created entity was rolled back.

[2020-09-30 17:29:04] testing.DEBUG: run beginTransaction()  
[2020-09-30 17:29:04] testing.DEBUG: run seed()  
[2020-09-30 17:29:04] testing.DEBUG: run testFoo()  
[2020-09-30 17:29:04] testing.DEBUG: run rollBack()  
[2020-09-30 17:29:04] testing.DEBUG: run beginTransaction()  
[2020-09-30 17:29:05] testing.DEBUG: run rollBack()  

@eigan
Copy link
Member

eigan commented Jan 6, 2021

Did you manage to figure out what the problem was @MP-okui ?

@MP-okui
Copy link
Author

MP-okui commented Jan 6, 2021

I solved it by running seed() with refreshTestDatabase().
Has there been an update for laravel-doctrine/orm about this problem?

    protected function refreshTestDatabase()
    {
        if (! RefreshDatabaseState::$migrated) {
            $this->artisan('doctrine:schema:drop --force');
            $this->artisan('doctrine:schema:create');

            // added
            $this->seed();

            $this->app[Kernel::class]->setArtisan(null);

            RefreshDatabaseState::$migrated = true;
        }

        $this->beginDatabaseTransaction();
    }

@eigan
Copy link
Member

eigan commented Jan 31, 2021

Has there been an update for laravel-doctrine/orm about this problem?

Not that I am aware of.

I am not able to figure the problem with the information currently available. If you want help fixing this then I would be grateful for a reproducible test (either a new test-repo or a testcase here).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants