Synopsis
I have a package which contains a database/migrations directory, within contains a number of migrations. I have a tests directory which contains one test with one assert.
I wish to unit test migrations using phpunit.
Occurring Problems
I run PHP Unit and my test fails:
$ phpunit
PHPUnit 4.6.6 by Sebastian Bergmann and contributors.
Configuration read from /var/www/.../phpunit.xml
F
Time: 594 ms, Memory: 15.25Mb
There was 1 failure:
1) MigrationTest::testTableExists
Failed asserting that false is true.
/var/www/../tests/MigrationTest.php:31
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
This command does't create any tables in the test database, it does create the migration table in the test database.
I then attempted to run the migration on the command line:
$ php artisan migrate --env=testing --path=/vendor/../database/migrations/
Migration table created successfully.
Migrated: 2015_.._table
Migrated: 2015_.._table
Migrated: 2015_.._table
The --env=testing argument has been completely ignored and my tables were migrated to the production database (NOTE: by production database I do mean development, but the default/production settings are incorrectly used)
I added the develop database credentials to phpunit.xml:
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="DB_DATABASE" value="test"/>
<env name="DB_USERNAME" value="test"/>
<env name="DB_PASSWORD" value="test"/>
</php>
My testcase is:
<?php
class MigrationTest extends \Illuminate\Foundation\Testing\TestCase
{
public function createApplication()
{
$app = require __DIR__. '/../../../../bootstrap/app.php';
$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();
return $app;
}
public function setUp()
{
parent::setUp();
$this->artisan('migrate', ['--env' => 'testing', '--path' => __DIR__.'/../database/migrations']);
}
public function tearDown()
{
//$this->artisan('migrate:rollback', ['--path' => __DIR__.'/../']);
parent::tearDown();
}
public function testTableExists()
{
$hasTable = Schema::hasTable('table_name');
$this->assertTrue($hasTable);
}
}
During the test I can use dd to dump out the env() data and the database credentials are correctly set to test, test and test.
I'm really baffled as to how to solve this problem.
Aucun commentaire:
Enregistrer un commentaire