When I run my tests with Phpstorm everthing works as expected and the connection switches to mysql_test. But when I run php artisan test manually the connection wont switch and remain mysql.
phpunit.xml file
<?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">./Modules/**/Tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./Modules/**/Tests/Feature</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_ENV" value="testing"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="DB_CONNECTION" value="mysql_test"/>
<env name="DB_CONNECTION" value="mysql_test"/>
<env name="MAIL_MAILER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<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>
<directory suffix=".php">./Modules</directory>
</include>
</source>
</phpunit>
One of my test files
class BannerTest extends TestCase
{
use WithFaker;
private static $authUser;
public function setUp(): void
{
// first include all the normal setUp operations
parent::setUp();
self::$authUser = Operator::factory()->create();
$this->app->make(\Spatie\Permission\PermissionRegistrar::class)->registerPermissions();
}
public function giveBannerPermission()
{
$operator = self::$authUser;
$this->be(self::$authUser);
$operator->assignRole('admin');
$role = Role::findByName('admin');
$role->givePermissionTo('bannerAdd');
}
public function banner_requires_status()
{
$this->giveBannerPermission();
$banner = Banner::factory()->raw(['title' => NULL]);
$this->post(route('banner.store'), $banner)->assertSessionHasErrors('title');
}
}
i've also tried php artisan optimize:clear and php artisan cache:clear before running test but nothing changes.
you need to create another .env called ".env.testing" then update your phpunit.xml like this:
your .env.testing:
this should fix your problem.