PHP Unit Tests don't work after updating to Laravel 11 #50797
Replies: 7 comments 9 replies
-
So, this looks like it is going to be some problem within your application code. Not Laravel. Laravel 11 is a breaking upgrade. Please ensure you follow the upgrade guide here. There were a lot of DB related changes. |
Beta Was this translation helpful? Give feedback.
-
@ZacharyDuBois Yes, as mentioned I followed the upgrade guide. I will try to do some debugging to find out what is going on to see if it is in my application code. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
If you're resetting database between tests you should be using |
Beta Was this translation helpful? Give feedback.
-
@crynobone I replaced RefreshDatabase with DatabaseMigrations. Unfortunately the same result. Why can sqlite_master table not be modified? Why does this work in Laravel 10, and not in Laravel 11? |
Beta Was this translation helpful? Give feedback.
-
Did some further debugging. Where Laravel 10 had the following code:
Here the function refreshInMemoryDatabase() did a migrate. In Laravel 11 the code changed to the following:
For some reason in restoreInMemoryDatabase() there is no migration anymore, variable $name is null, so there is also no $inMemoryConnections available. The function refreshTestDatabase() is unchanged. `protected function refreshTestDatabase()
The migrate:fresh command will probably try to delete all tables, but there are no tables due to the fact that no migration has been executed. Could this be the problem I'm running into? If so, how can it be solved? |
Beta Was this translation helpful? Give feedback.
-
@crynobone Looks like the problem has been fixed in the latest version I've been updating to (v11.4). I see some issues while testing, but I have to figure out what's creating that (could be an v10 to v11 update issue). |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Laravel Version
11
PHP Version
8.2
Database Driver & Version
No response
Description
After updating to Laravel 11 I tried to run some PHP Unit tests that I used in the previous Laravel version (10). I use MySql as the main database and for testing I use Sqlite in memory (as set in phpunit.xml). Testing in Laravel 10 worked fine, but running the tests in Laravel 11 showed errors.
Using MAMP 6.9 for PHP (8.2) and MySql (5.7.39) on Mac M1 with MacOS Sonoma 14.4.1.
Sqlite3 version 3.43.2 installed.
Steps To Reproduce
I updated a working Laravel 10 project to Laravel 11, following the steps in the Laravel documentation (Prologue > Upgrade guide). Dropped the vendor files, and reinstalled them using composer. At this point the application is still working as supposed to. But running a PHP Unit test
Testresult:
`php artisan test --filter AuthorisationTest
FAIL Tests\Feature\App\AuthorisationTest
⨯ admin can login
⨯ teacher can login
⨯ student can login
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
FAILED Tests\Feature\App\AuthorisationTest > admin can login QueryException
SQLSTATE[HY000]: General error: 1 table sqlite_master may not be modified (Connection: sqlite, SQL: delete from sqlite_master where type in ('table', 'index', 'trigger'))
at vendor/laravel/framework/src/Illuminate/Database/Connection.php:813
809▕ $this->getName(), $query, $this->prepareBindings($bindings), $e
810▕ );
811▕ }
812▕
➜ 813▕ throw new QueryException(
814▕ $this->getName(), $query, $this->prepareBindings($bindings), $e
815▕ );
816▕ }
817▕ }
37 tests/Feature/App/AuthorisationTest.php:19`
Line 19 is the line of parent::setUp();
AuthorisationTest.php:
`<?php
namespace Tests\Feature\App;
use App\Enums\Roles;
use App\Models\User;
use App\Providers\RouteServiceProvider;
use Database\Seeders\PermissionsSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class AuthorisationTest extends TestCase
{
use RefreshDatabase;
}`
phpunit.xml:
<?xml version="1.0" encoding="UTF-8"?> <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/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> <coverage /> <php> <env name="APP_ENV" value="testing" /> <env name="BCRYPT_ROUNDS" value="4" /> <env name="CACHE_DRIVER" value="array" /> <env name="DB_CONNECTION" value="sqlite" /> <env name="DB_DATABASE" value=":memory:" /> <env name="MAIL_MAILER" value="array" /> <env name="QUEUE_CONNECTION" value="sync" /> <env name="SESSION_DRIVER" value="array" /> <env name="TELESCOPE_ENABLED" value="false" /> </php> <source> <include> <directory suffix=".php">./app</directory> </include> </source> </phpunit>
Beta Was this translation helpful? Give feedback.
All reactions