Starla833's avatar

Getting phpunit tests to work in phpstorm

I'm using phpstorm, wsl and docker. As far as I'm aware I have all my settings correct. The reason I say this is because in my test method if I just do dd('im here') then it shows up when I run php artisan test --filter=PostTest, but when I have this piece of code

$user = User::create([
    'name' => 'Test User',
    'email' => '[email protected]',
    'password' => Hash::make('secret')
]);

I get this error

SQLSTATE[08006] [7] connection to server at "pgsql" (172.20.0.5), port 5432 failed: FATAL:  password authentication failed for user "wip" (Connection: pgsql, SQL: inse
rt into "users" ("name", "email", "password", "updated_at", "created_at") values (Test User, [email protected], y$hGapciy8HfUnpjC6s5pJRuxW2zMz2.knh1lAX.g2626Rv52dOReMG, 2023-05-08 06:28:31, 2023-05-08 06:28:31) returning "id")

This is the full PostTest class

<?php

namespace Tests\Feature\Models;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Hash;
use Tests\TestCase;

class PostTest extends TestCase
{
    public function test_createPost(): void
    {
        $user = User::create([
            'name' => 'Test User',
            'email' => '[email protected]',
            'password' => Hash::make('secret'),
        ]);
    }
}

and this is my phpunit.xml

<?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>
    <coverage>
        <include>
            <directory suffix=".php">./app</directory>
        </include>
    </coverage>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="BCRYPT_ROUNDS" value="4"/>
        <env name="CACHE_DRIVER" value="array"/>
        <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>
</phpunit>
0 likes
1 reply
JabatoForever's avatar

If u are using sail or any docker image in general u should make sure you are running the test from inside the container. If u use Laravel sail just copy and paste this command: ./vendor/bin/sail test If u use a different docker image : docker exec ./vendor/bin/sail test you can also open a shell from the docker dashboard, select your project and open the shell tab.

Please or to participate in this conversation.