My Laravel
local setup with Sail is using pgsql
as database, so everything get orchestrated by sail up
. Now I want to write tests by using sqlite
in memory because the testing pipeline finally should not require to spin up a Postgres server nor I want to introduce database tenants to the codebase just for the sake of testing.
I have setup everything correct according to the docs of larvel and pest. Everything works to the point where I am verifying if the user registration is working and if the user got actually saved to the database.
From the error message I can tell that the test would like to connect to the Postgres server inside docker instead of creating an in memory sqlite database. EnvironmentTest.php
is passing while RegistrationTest.php
is failing in both methods.
Please, if I get something completely wrong tell me as for me PHP, Laravel and overall Testing is new. Also I am open to any kind of suggestions.
Function | Error |
---|---|
render:registration | Expected response status code [200] but received 500. Failed asserting that 200 is identical to 500. |
function:registration:register | Illuminate\Database\QueryException : SQLSTATE[08006] [7] could not translate host name "pgsql" to address: Name or service not known (SQL: select * from information_schema.tables where table_catalog = laravel and table_schema = public and table_name = migrations and table_type = 'BASE TABLE') |
RegistrationTest.php
<?php
namespace Tests\Feature\Auth;
use App\Providers\RouteServiceProvider;
test('render:registration', function () {
$response = $this->get('/register');
$response->assertStatus(200);
$response->assertInertia(fn($page) => $page->component('Auth/Register'));
});
test('function:registration:register', function () {
$response = $this->post('/register', [
'name' => 'Test User',
'email' => '[email protected]',
'password' => 'password',
'password_confirmation' => 'password',
]);
$this->assertAuthenticated();
$response->assertRedirect(RouteServiceProvider::HOME);
});
EnvironmentTest.php
<?php
it('environment:testing', function () {
expect(env('APP_ENV'))->toBe('testing')
->and(env('DB_CONNECTION'))->toBe('sqlite');
});
CreatesApplication.php
trait CreatesApplication
{
public function createApplication()
{
$app = require __DIR__ . '/../bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
return $app;
}
}
phpunit.xml
<php>
<env name="APP_ENV" value="testing"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
</php>