2
votes

I am using Laravel and PHPUnit for my Unit Tests and I have a test class like so:

class ImportServiceTest extends BaseTestCase
{
   use DatabaseMigrations;

   public function setUp()
   {
      // Parent Setup
      parent::setUp();
   }

   public function tearDown()
   {
      parent::tearDown();
   }
}

However when I run PHPUnit, my test database is only migrating 70 of the tables as opposed to the normal 160. It is also not migrating any migrations prefixed with 2017. Only those prefixed with 2016.

I am wondering if anyone has run into this?

Here is my phpunit.xml:

<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="true"
         stopOnError="true"
         stopOnIncomplete="true"
         stopOnSkipped="false"
         stopOnRisky="false"
         syntaxCheck="true"
         timeoutForSmallTests="1"
         timeoutForMediumTests="10"
         timeoutForLargeTests="60"
         verbose="true"
         bootstrap="bootstrap/autoload.php">
    <testsuites>
        <testsuite name="Application Test Suite">
            <testsuite name="Modules Test Suite">
                <directory suffix="Test.php">./Modules/*/Tests</directory>
            </testsuite>
            <!--<directory suffix="Test.php">./tests</directory>-->
        </testsuite>
    </testsuites>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="APP_DEBUG" value="true"/>
        <env name="APP_LOG_LEVEL" value="debug"/>
        <env name="DB_CONNECTION" value="testing"/>
        <env name="APP_NAME" value="Central"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="redis"/>
        <env name="BROADCAST_DRIVER" value="redis"/>
        <env name="REDIS_HOST" value="127.0.0.1"/>
        <env name="REDIS_PORT" value="6379"/>
    </php>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="false">
            <directory suffix=".php">./tests</directory>
            <directory suffix=".php">./tests/coverage</directory>
            <directory suffix=".php">./database/migrations</directory>
            <directory suffix=".php">./src</directory>
            <directory>./vendor</directory>
            <exclude>
                <directory suffix=".php">./tests</directory>
                <directory suffix=".php">./tests/coverage</directory>
                <directory suffix=".php">./src/resources</directory>
                <directory>./vendor</directory>
            </exclude>
        </whitelist>
    </filter>
    <!--<logging>-->
    <!--<log type="coverage-html"-->
    <!--target="./tests/coverage/"-->
    <!--lowUpperBound="35"-->
    <!--highLowerBound="70"/>-->
    <!--</logging>-->
    <groups>
        <exclude>
            <group>performance</group>
        </exclude>
    </groups>
</phpunit>

I am quite stumped as I have used PHPUnit before and never seen this behavior.

My testing connection settings are as follows:

         # Testing Database Credentials - set in production environment or .env
    'testing' => [
        'driver' => 'mysql',
        'host' => env('DB_TESTING_HOST', 'localhost'),
        'port' => env('DB_TESTING_PORT', '3306'),
        'database' => env('DB_TESTING_DATABASE', 'testing'),
        'username' => env('DB_TESTING_USERNAME', 'homestead'),
        'password' => env('DB_TESTING_PASSWORD', 'secret'),
        'charset' => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],

Thanks!

1
what happens when you run php artisan migrate --db=test_db_name?Alex Harris
What's the settings on the testing connection?Camilo
Added the testing connection settings from config/database.phpliamjnorman

1 Answers

3
votes

I had the same issue as you and was stumped at first. But I resolved it.

Before I give the solution, let me explain my setup:

  1. I do NOT use an "in memory" sqlite database for testing. I was at first, but I encountered this issue when I decided to start using a separate PostgreSQL database strictly for PHPUnit.

  2. I use migrations to seed test data, based on application env, so that I can guarantee things happen in a certain order, so I know which columns exist at that time.

In my case what was happening was that one of my migrations was throwing an exception that was never displayed. So artisan would just stop at that migration, and phpunit would continue. I discovered I was seeding things for my 'local' environment but not my 'testing' environment. After making sure the 'testing' environment was seeded appropriately (not necessarily identical to 'local', mind you), everything started working again.