Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kbuczynski's avatar

Laravel PHPUnit Testing Issue

Hi I have an rest api application which uses PostgreSQL multi-schema solution to store data base on a subdomain instance.

While running the unit test is hang half way threw after upgrading the application from 5.8 to ^6.0 Laravel. Further debugging shows it hangs at specific point (around 48% done threw going all the test) when it tries to run migrate:fresh as part of a database migration trait used in the php test file.

The test env uses a database is set up on docker (I tried memory setup too same issue)

The tests has been run individually and they all work fine (passing and everything) just hangs when I try to run them all.

How do I debug it? I tried stepping threw the code not showing anything. Is there a way I can try to print out the migration output or enable some sort of extra login.

I am happy to provide more information if I can but I am not able to share any code.

Edit:

I tried running it on mysqlite but it still hangs

I have debug this further and turns out postgres is locking the transaction casing drop to wait for the release of previous one .

Idk yet how to solve this but at least i got that far

0 likes
20 replies
automica's avatar

if you switch to running your tests using mysqlite, do they complete then?

kbuczynski's avatar

Hmm... Running now I update the result once its done

1 like
automica's avatar

when it hangs, does it eventually time out? I wonder if you DB is running out of memory at that point.

bugsysha's avatar

If you are using RefreshDatabase trait, then try DatabaseMigrations trait instead. Have you changed phpunit.xml to match Laravel6?

kbuczynski's avatar

The tests are already using DatabaseMigrations only Do you have an example of phpunit.xml for Laravel 6 that i can compare with ?

automica's avatar

@kbuczynski is the phpunit.xml for laravel6

<?xml version="1.0" encoding="UTF-8"?>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"

         bootstrap="vendor/autoload.php"

         colors="true">

    <testsuites>

        <testsuite name="Unit">

            <directory suffix="Test.php">./tests/Unit</directory>

        </testsuite>



        <testsuite name="Feature">

            <directory suffix="Test.php">./tests/Feature</directory>

        </testsuite>

    </testsuites>

    <filter>

        <whitelist processUncoveredFilesFromWhitelist="true">

            <directory suffix=".php">./app</directory>

        </whitelist>

    </filter>

    <php>

        <server name="APP_ENV" value="testing"/>

        <server name="BCRYPT_ROUNDS" value="4"/>

        <server name="CACHE_DRIVER" value="array"/>

        <server name="DB_CONNECTION" value="sqlite"/>

        <server name="DB_DATABASE" value=":memory:"/>

        <server name="MAIL_DRIVER" value="array"/>

        <server name="QUEUE_CONNECTION" value="sync"/>

        <server name="SESSION_DRIVER" value="array"/>

    </php>

</phpunit>
Talinon's avatar

@kbuczynski Try running phpunit --verbose --debug and see if it provides any additional information

At the very least, it'll show you at what point in your test suite it hangs... or if it hangs at different places, that could be a clue that it's resource exhaustion.

bugsysha's avatar

Without codebase it is hard to solve this by guessing.

kbuczynski's avatar

@bugsysha I agree its is a tough cookie but I would like to know if there is anything I can do on my end to debug this so I can understand why it hangs. something like print out extra msg on the screen or in the log file would be very helpful

@talinon already run using the arguments and i discover that it was hanging on migrate:refresh runned by the DatabaseMigration trait.

automica's avatar

@kbuczynski you can use:

       $myDebugVar = array(1, 2, 3);
        fwrite(STDERR, print_r($myDebugVar, TRUE));

to print to console.

bugsysha's avatar

You can run tests with ‘vvv’ option to see if it outputs anything useful. Also you can find PhpUnit docs and see logging options there.

kbuczynski's avatar

Thanks for the help with a debug @automica It showed me what query its failing on

I found out it hangs on (as part of databasemigration trait) drop table "migrations","oauth_auth_codes","oauth_access_tokens","oauth_refresh_tokens","oauth_clients","oauth_personal_access_clients","users","organisation_user","organisations","password_resets","jobs","failed_jobs","roles","abilities","permissions","preferences","assigned_roles","shared_buckets","shared_assets","shared_asset_folders","shared_asset_shared_font","shared_fonts","shared_font_shared_theme","shared_themes","cultures" cascade

and this occurs in dbal package which is at version 2.10.4

Do you have any further tips how to debug this ?

kbuczynski's avatar

I have debug this further and turns out posgress is locking the transaction casing drop to wait for the release of previous one .

Idk yet how to solve this but at least i got that far

automica's avatar
automica
Best Answer
Level 54

@kbuczynski you may want to look at disabling foreign key constraints when dropping tables.

   Schema::disableForeignKeyConstraints();

you could also look at unlocking the table before dropping it.

   DB::statement('UNLOCK TABLES;');
1 like
kbuczynski's avatar

Small update thanks to @automica i manage to unlock the tables with simple query he gave me as a example. Also manage to determinate which actions cause the lock and fixed it

many thanks <3

Please or to participate in this conversation.