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

[BUG] Unit tests not working when using :memory: driver sqlite database. #275

Open
bigperson opened this issue Sep 13, 2017 · 11 comments
Open

Comments

@bigperson
Copy link
Contributor

bigperson commented Sep 13, 2017

Package version, Laravel version

Laravel 5.5
Laravel doctrine 1.4
PHPUnit 6.0

Expected behaviour

Tests must pass success.

Actual behaviour

Entity manager can not access the tables in databases
Errors:

 Doctrine\DBAL\Driver\PDOException: SQLSTATE[HY000]: General error: 1 no such table: cities

Steps to reproduce the behaviour

/config/database.php

...
'connections' => [
...
        'tests' => [
            'driver' => 'sqlite',
            'database' => ':memory:',
            'prefix' => '',
        ],
...

/config/doctrine.php

...
'connection'    => env('DB_CONNECTION', 'mysql'),
...

phpunit.xml

<env name="DB_CONNECTION" value="tests"/>

ExampleTest.php

/** @var \Doctrine\ORM\EntityManagerInterface */
    protected $em;

    /**
     * @return void
     */
    public function setUp()
    {
        parent::setUp();
        $this->em = $this->app->make(EntityManagerInterface::class);
    }

    /**
     * {@inheritDoc}
     */
    protected function tearDown()
    {
        parent::tearDown();

        $this->em->close();
        $this->em = null;
    }

    /**
     * A basic test example.
     *
     * @return void
     */
    public function testBasicTest()
    {
        \DB::table('cities')->insert(['name'=>'test']); //Working
        entity(City::class)->create(); //crashed
        $city = (new City())->setName('test');
        $this->em->persist($city);
        $this->em->flush(); //crashed
        $cities = $this->em->getRepository(City::class)->findAll(); //crashed

        $this->assertCount(3, $cities);
    }
@elambro
Copy link

elambro commented Nov 15, 2017

Do you have a 'create_cities_table' migration?

@bicatu
Copy link

bicatu commented Nov 25, 2017

I have similar issue (not same table of course), but I do have the VersionXXX files that when I run from the console create the tables I am looking for. (when I call the same methods myself not from phpunit they work)

@garret-gunter
Copy link
Contributor

I don't see where you are running the migrations. We use in memory sqlite for our tests. We haven't had this issue. Have you tried adding the helper trait for refreshing the database during tests? It should be in the laravel doctrine migrations package.

@patrickbrouwers
Copy link
Contributor

Cannot reproduce

@dallincoons
Copy link
Contributor

dallincoons commented Mar 16, 2018

I have the same issue, I had to use a sqlite file instead of the in-memory version. I'm using Orchestra Testbench.

@nospoon
Copy link

nospoon commented May 9, 2018

I think this might be related to running the tests inside vm.

@R3VoLuT1OneR
Copy link
Contributor

R3VoLuT1OneR commented May 15, 2018

I have same problem.
Doctrine connection and Laravel DatabaseManager connections have different PDOConnection.
So Illuminate\Foundation\Testing\RefreshDatabase trait not working.
So tests not working with default Laravel migrations.

@patrickbrouwers
Copy link
Contributor

You cannot use that trait as indeed we don't share the PDOConnection

@xxRockOnxx
Copy link

xxRockOnxx commented Jun 16, 2018

Any workaround available yet? I'm having this problem also. Can confirm this is a bug.

More info:

setUp()
{
    parent::setUp();
    Artisan::call('migrate'); // Works properly
    \DB::statement('SELECT * FROM tablename'); // works fine

     // Scenario 2
     parent::setUp();
     \DB::statement('SELECT * FROM tablename'); // Exception
}

This just confirms it again. Not sure whether it is laravel-doctrine's problem or doctrine itself.

@Stolz
Copy link

Stolz commented Sep 7, 2018

This is my workaround so far. Provided you set up phpunit.xml and config/database.php as @bigperson mentioned, in order to have the RefreshDatabase behavior working with Docrine make your tests to use this trait instead of Illuminate\Foundation\Testing\RefreshDatabase

<?php

namespace Tests\Traits;

use Illuminate\Contracts\Console\Kernel;

trait RefreshDatabase
{
    use \Illuminate\Foundation\Testing\RefreshDatabase;

    /**
     * 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();
        });
    }

    /**
     * Refresh the in-memory database.
     *
     * @return void
     */
    protected function refreshInMemoryDatabase()
    {
        $this->artisan('doctrine:schema:create');

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

It works without problems is several of my projects although for others I get Operation 'Doctrine\DBAL\Platforms\AbstractPlatform::getCreateSequenceSQL' is not supported by platform error but at least DB schema seems to be successfully created in memory and my tests run faster now.

If anyone has a better approach please let me know.

@bicatu
Copy link

bicatu commented Oct 2, 2018

Hi, any updates on this one?

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

10 participants