dmaicher / doctrine-test-bundle

Symfony bundle to isolate your app's doctrine database tests and improve the test performance
MIT License
1.08k stars 60 forks source link

Not working (no rollback) #217

Closed aless673 closed 2 years ago

aless673 commented 2 years ago

Maybe i am missing something but nothing is rolledback in this simple test

<?php

namespace App\Tests;

use Doctrine\DBAL\Connection;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class TestitTest extends KernelTestCase
{
    private ?Connection $connection;

    public function testSomething(): void
    {
        $this->connection = static::getContainer()->get('doctrine.dbal.default_connection');
        $this->connection->update('user', [
            'lastName' => 'test',
        ], ['id' => 1]);
    }
}

extension is enabled

    <extensions>
        <extension class="DAMA\DoctrineTestBundle\PHPUnit\PHPUnitExtension" />
    </extensions>

bundle is enabled

    DAMA\DoctrineTestBundle\DAMADoctrineTestBundle::class => ['test' => true],

bundle config

dama_doctrine_test:
    enable_static_connection: true
    enable_static_meta_data_cache: true
    enable_static_query_cache: true

Rollback is not working, the data is still changed in DB after test finish : vendor/bin/phpunit --debug tests/TestitTest.php

dmaicher commented 2 years ago

Would you be able to provide a reproducer? I cannot reproduce this on any of my projects or on the testsuite of the bundle.

dmaicher commented 2 years ago

Does version 7.1.0 work for you?

aless673 commented 2 years ago

Does version 7.1.0 work for you?

same problem with 7.1.0

here the reproducer : https://github.com/aless673/test_rollback_doctrine i did not pushed my docker-compose config

dmaicher commented 2 years ago

I currently don't have a local postgresql instance available. Quickly tried sqlite and mysql: works fine.

diff --git a/tests/TestitTest.php b/tests/TestitTest.php
index 94d4c26..accff73 100644
--- a/tests/TestitTest.php
+++ b/tests/TestitTest.php
@@ -13,4 +13,13 @@ class TestitTest extends KernelTestCase
             'first_name' => 'Alex',
         ]);
     }
+
+    /**
+     * @depends testSomething
+     */
+    public function testSomethingElse(): void
+    {
+        $numberOfUsers = (int) static::getContainer()->get('doctrine.dbal.default_connection')->fetchOne('SELECT COUNT(*) FROM user');
+        self::assertSame(0, $numberOfUsers);
+    }
 }

No not sure if this is related to postgresql :thinking: Will try to find some time and setup a local postgresql with docker

dmaicher commented 2 years ago

@aless673 I just tried it with a docker instance of postgres:latest and for me this works fine :man_shrugging: on the second test the insert is rolled back.

So for you that second test I added really fails?

aless673 commented 2 years ago

@aless673 I just tried it with a docker instance of postgres:latest and for me this works fine 🤷‍♂️ on the second test the insert is rolled back.

So for you that second test I added really fails?

    public function testSomething(): void
    {
        static::getContainer()->get('doctrine.dbal.default_connection')->insert('user', [
            'last_name' => 'Peter',
            'first_name' => 'Alex',
        ]);
    }

    public function testSomethingElse(): void
    {
        $numberOfUsers = (int) static::getContainer()->get('doctrine.dbal.default_connection')->fetchOne('SELECT COUNT(*) FROM user');
        self::assertSame(0, $numberOfUsers);
    }

Does not work

image

NB: i am using MySQL

dmaicher commented 2 years ago

For me this just works with Mysql:


it@32b013e527bd:/var/www/test_rollback_doctrine  (main) $ bin/console doctrine:database:drop -f -q
it@32b013e527bd:/var/www/test_rollback_doctrine  (main) $ bin/console doctrine:database:create -q
it@32b013e527bd:/var/www/test_rollback_doctrine  (main) $ bin/console doctrine:schema:create -q
it@32b013e527bd:/var/www/test_rollback_doctrine  (main) $ vendor/bin/phpunit 
PHPUnit 9.5.21 #StandWithUkraine

Testing 
..                                                                  2 / 2 (100%)

Time: 00:00.094, Memory: 22.00 MB

OK (2 tests, 2 assertions)

Remaining indirect deprecation notices (1)

  1x: The "DAMA\DoctrineTestBundle\Doctrine\DBAL\VersionAwarePlatformStaticDriver" class implements "Doctrine\DBAL\VersionAwarePlatformDriver" that is deprecated All drivers will have to be aware of the server version in the next major release.
    1x in TestitTest::testSomething from App\Tests

it@32b013e527bd:/var/www/test_rollback_doctrine  (main) $ vendor/bin/phpunit 
PHPUnit 9.5.21 #StandWithUkraine

Testing 
..                                                                  2 / 2 (100%)

Time: 00:00.092, Memory: 22.00 MB

OK (2 tests, 2 assertions)

Remaining indirect deprecation notices (1)

  1x: The "DAMA\DoctrineTestBundle\Doctrine\DBAL\VersionAwarePlatformStaticDriver" class implements "Doctrine\DBAL\VersionAwarePlatformDriver" that is deprecated All drivers will have to be aware of the server version in the next major release.
    1x in TestitTest::testSomething from App\Tests
aless673 commented 2 years ago

i don't have deprecations, probably the bundle is not active. But why..

dmaicher commented 2 years ago

But tests are running in test env?


static::bootKernel();
dd(static::$kernel->getEnvironment());
aless673 commented 2 years ago

But tests are running in test env?

static::bootKernel();
dd(static::$kernel->getEnvironment());

yes

PHPUnit 9.5.21 #StandWithUkraine

Testing
"test"
dmaicher commented 2 years ago

Well then honestly no idea whats happening for you. I cannot reproduce it :cry: