I am trying to use a real Sql Server connection to run my php unit test in Laravel because SqLite does not have the function I am trying to test. The database name is "Test" and the connection details have all been added to database.php.
I have changed my phpunit.xml env variables to refer to the new test database.
<env name="DB_CONNECTION" value="test_sqlserver"/>
<env name="DB_DATABASE" value="Test"/>
Now, when I try to run a simple test with a class that use the RefreshDatabase trait, or even the DatabaseMigrations trait, it result in the following error:
Symfony\Component\Console\Exception\InvalidOptionException: The "--drop-views" option does not exist.
It can be reproduced with the following test:
<?php
namespace Tests\Unit\Actions;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class GetResourcesExceedingHoursByPayPeriodWeeksActionTest extends TestCase
{
use RefreshDatabase;
protected $dropViews = false;
/** @test */
public function it_can_get_resources_exceeding_hours_by_pay_period_weeks()
{
}
}
Edit The solution bellow proposed by Daniel did not solve the issue. Inspiring from that however, I added the following method:
protected function migrateFreshUsing()
{
$seeder = $this->seeder();
return array_merge([
// '--drop-views' => $this->shouldDropViews(),
// '--drop-types' => $this->shouldDropTypes(),
],
// $seeder ? ['--seeder' => $seeder] : ['--seed' => $this->shouldSeed()]
);
}
It resolved the error, however, now phpunit does not output any result. (I added a simple assertion $this->assertTrue(false);)
Full class code:
<?php
namespace Tests\Unit\Actions;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class GetResourcesExceedingHoursByPayPeriodWeeksActionTest extends TestCase
{
use RefreshDatabase;
protected $dropViews = false;
protected function migrateFreshUsing()
{
$seeder = $this->seeder();
return array_merge([
// '--drop-views' => $this->shouldDropViews(),
// '--drop-types' => $this->shouldDropTypes(),
],
//$seeder ? ['--seeder' => $seeder] : ['--seed' => $this->shouldSeed()]
);
}
/** @test */
public function it_can_get_resources_exceeding_hours_by_pay_period_weeks()
{
$this->assertTrue(false);
}
}
Edit 13:00
Adding the new tag in the phpunit.xml did not help to add output to the console. However, I found the line causing the issue, as when it is commented, phpunit does output to the console. To test correctly, I extended from the base TestCase class instead of the one shown above.
Then I added
$this->seed(DatabaseSeeder::class);
as the first line of my test, which resulted in no output. Commenting it would allow phpunit to output, but obviously, the test would work with the dummy assertion, but not with anything that requires seeding.
With further investigation it seems that the issue is caused by 3 seeders that uses a third party package to seed from a csv file. Every other seeder run fine.
I will keep investigating this path and update my question with the solution.
How can I test using a real Sql server database?