dmaicher
Repos
53
Followers
45
Following
15

Symfony bundle to isolate your app's doctrine database tests and improve the test performance

905
52

Symfony bundle for creating dynamic menues

8
1

Small just for fun project

1
0

Integrates the Solarium library as a Symfony bundle

1
0

Events

dmaicher delete branch fix_issue_95
Created at 1 month ago
pull request opened
fix issue with overwriting default config on default profile

Fixes https://github.com/Exercise/HTMLPurifierBundle/issues/95

Created at 1 month ago

fix issue with overwriting default config on default profile

Created at 1 month ago
dmaicher create branch fix_issue_95
Created at 1 month ago
Cache permission issue since 4.1

It seems https://github.com/Exercise/HTMLPurifierBundle/commit/9712ab74579222d58f5e20a12afe8ff0648b0ed3 is causing some issues for one of my apps.

I have this config:

exercise_html_purifier:
    html_profiles:
        default:
            config:
                Cache.SerializerPermissions: 0o777

Using 4.1 however my config is overwritten by the new default config it seems.

Created at 1 month ago
dmaicher delete branch test_cleanup
Created at 1 month ago

make sure to cleanup persisted row after test (#225)

Created at 1 month ago
pull request closed
make sure to cleanup persisted row after test
Created at 1 month ago
DB is not empty between two test files

Hello :)

The problem I'm facing is that my DB is not empty when my second test is running. I have tried many things to understand the problem and to make it reproducible.

I'm sure I'm doing something wrong somewhere, but I don't know if it is a matter of setting up Foundry or DAMA, or maybe both.

Let's first talk about the environment :

  • PHP-FPM 8.1.9
  • Symfony 6.1.2
  • PHPunit 8.5.21
  • Foundry 1.21.0
  • DAMA\DoctrineTestBundle 7.1.1
  • MariaDB 10.5.16

I have copied an adapted the test file and its trait from DAMA Bundle : https://gist.github.com/GlucNAc/7f7986f2303f67d519b67818370aff70.

If I run the file PhpunitTest (from the above gist) or Phpunit2Test on their own, it works fine. But if I run them both subsequently, then the second one fails :

image

As you can see here testChangeDbState() of the subsequent test performs $this->assertRowCount(0) which actually returns 1.

What I don't understand is why the methods inside a unique file seem to be independent, but not from file to file ?

If this assertion is false, then it seems a commit has been done to the DB. If we take look at the code we can see there are lines that perform a commit :

  • https://gist.github.com/GlucNAc/7f7986f2303f67d519b67818370aff70#file-phpunittest-L63
  • https://gist.github.com/GlucNAc/7f7986f2303f67d519b67818370aff70#file-phpunittest-L98

But even if I remove the corresponding methods, the problem is still here. And these lines bring me to another question :

image

Line 63 a commit is done, meaning data have been written in the DB (right?). So why the subsequent assertion line 72 does not fail then? I think the answer of this question is one of the key to understand what am I doing wrong ^^

Created at 1 month ago
DB is not empty between two test files

The last test cases are actually testing that the transactional behavior can be disabled and thus the row will be actually persisted.

Like this the tests all pass:

diff --git a/tests/PhpunitTest.php b/tests/PhpunitTest.php
index b2e5eb2..4b33123 100644
--- a/tests/PhpunitTest.php
+++ b/tests/PhpunitTest.php
@@ -120,6 +120,18 @@ class PhpunitTest extends KernelTestCase
      */
     public function testChangesFromPreviousTestAreVisibleWhenDisabledDuringRuntime(): void
     {
+        StaticDriver::setKeepStaticConnections(false);
+
+        self::ensureKernelShutdown();
+        $this->setUp();
+
         $this->assertRowCount(1);
+
+        // cleanup persisted rows to not affect any other tests afterwards
+        $this->connection->executeQuery('DELETE FROM question');
+
+        $this->assertRowCount(0);
+
+        StaticDriver::setKeepStaticConnections(true);
     }
 }

I now added a cleanup for this case: https://github.com/dmaicher/doctrine-test-bundle/pull/225

Created at 1 month ago
pull request opened
make sure to cleanup persisted row after test
Created at 1 month ago
dmaicher create branch test_cleanup
Created at 1 month ago
Start new transaction only once

I'm using Symfony 5.4.

Could you create a minimal reproducer for this? Maybe based on the Symfony demo?

Then I will gladly take a look.

Created at 1 month ago
Start new transaction only once

I tried

        // The underlying connection already has a transaction started.
        // We start a transaction on the connection as well
        // so the internal state ($_transactionNestingLevel) is in sync with the underlying connection.
        $args->getConnection()->beginTransaction();

        var_dump("here!", spl_object_hash($args->getConnection()));

and this is never executed for the same connection more than once for me.

Created at 1 month ago
Start new transaction only once

I tested it with a listener like this on one of my apps

<?php

namespace App;

use Doctrine\Common\EventSubscriber;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Events;
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;

#[AutoconfigureTag(name: 'doctrine.event_subscriber')]
class TestListener implements EventSubscriber
{
    public function __construct(private Connection $connection)
    {
    }

    public function getSubscribedEvents()
    {
        return [Events::postConnect];
    }

    public function postConnect(): void
    {
        $this->connection->executeQuery('SELECT 1');
    }
}

and this plays nicely with this bundle and the PostConnectEventListener. I fail to understand how it can be registered multiple times.

Could you create a minimal reproducer for this? Maybe based on the Symfony demo?

Created at 1 month ago
Start new transaction only once

I cannot reproduce this issue.

How does your Doctrine event subscriber look like in such a case? Which events does it subscribe to?

Created at 1 month ago
DB is not empty between two test files

Thanks. I will try to find some time and have a look.

Created at 1 month ago
DB is not empty between two test files

Line 63 a commit is done, meaning data have been written in the DB (right?). So why the subsequent assertion line 72 does not fail then? I think the answer of this question is one of the key to understand what am I doing wrong ^^

Doctrine supports nested transactions via SAVEPOINTs. So this test makes sure rolling back a nested transaction (that is wrapped by some outer transaction of the bundle) works.

Created at 1 month ago
DB is not empty between two test files

Does it work without using Zenstruck\Foundry\Test\ResetDatabase?

Created at 1 month ago
dmaicher delete branch dmaicher-patch-1
Created at 1 month ago