1
  • Laravel v5.5.48
  • Phpunit 6.5.14

Was able to setup the test to run from the real mysql database, but of course, this is not desirable. So, changed phpunit.xml to use sqlite, but now I get "No such table" errors. Digged for hours, but cannot get past this error.

It seems that Migrations are not getting run into the Memory Database (my theory). It is important to mention that our application has Asgard CMS, so it has many modules (in the Modules folder) and each has its own database/migrations folder where the 'true' migrations are kept. The migration for the table setting__settings mentioned in the error, would be in the Module/Setting/database/migrations folder.

Error info:

PHPUnit 6.5.14 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.3.13
Configuration: /var/www/html/phpunit.xml

E

Time: 5.93 seconds, Memory: 10.00MB

There was 1 error:

1) Tests\Unit\ExampleTest::testBasicTest
Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 no such table: setting__settings (SQL: select * from "setting__settings" where "name" = sitesearch::base_path limit 1)

/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:624
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:333
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:1719
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:1704
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:481
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:465
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Concerns/BuildsQueries.php:77
/var/www/html/Modules/Setting/Repositories/Eloquent/EloquentSettingRepository.php:86
/var/www/html/Modules/Setting/Support/Settings.php:34
/var/www/html/Modules/Setting/helpers.php:6
/var/www/html/Modules/Sitesearch/Http/frontendRoutes.php:6
/var/www/html/Modules/Core/Providers/RoutingServiceProvider.php:76
/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Router.php:389
/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Router.php:349
/var/www/html/Modules/Core/Providers/RoutingServiceProvider.php:77
/var/www/html/Modules/Core/Providers/RoutingServiceProvider.php:61
/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Router.php:389
/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Router.php:349
/var/www/html/Modules/Core/Providers/RoutingServiceProvider.php:62
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:29
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:87
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:31
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php:549
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Support/Providers/RouteServiceProvider.php:74
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Support/Providers/RouteServiceProvider.php:33
/var/www/html/Modules/Core/Providers/RoutingServiceProvider.php:25
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:29
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:87
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:31
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php:549
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:792
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:775
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:776
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/BootProviders.php:17
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:213
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:296
/var/www/html/tests/CreatesApplication.php:18
/var/www/html/vendor/orchestra/testbench-core/src/TestCase.php:71
/var/www/html/vendor/orchestra/testbench-core/src/Concerns/Testing.php:59
/var/www/html/vendor/orchestra/testbench-core/src/TestCase.php:41

How the TestCase is extended:

namespace Tests;

use Orchestra\Testbench\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;
}

The test itself:

namespace Tests\Unit;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ExampleTest extends TestCase
{
    use RefreshDatabase;

    /** @test */
    public function testBasicTest()
    {
        $this->post(route('search_results'), ['search_term' => 'calgary'])
            ->assertStatus(200)
            ->assertViewIs('sitesearch::results.index')
            ->assertViewHas('search_term', 'calgary');

        $this->assertTrue(true);
    }
}

Phpunit.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="vendor/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="true"
         stopOnFailure="true"
         verbose="true">
    <testsuites>
        <testsuite name="Application Test Suite">
            <testsuite name="Test Suite">
                <directory>./tests/Unit</directory>
                <directory>./Modules/*/Tests</directory>
<!--                <directory>./tests/Feature</directory>-->
            </testsuite>
<!--            <testsuite name="Modules Test Suite">-->
<!--                <directory>./Modules/*/Tests</directory>-->
<!--            </testsuite>-->
        </testsuite>
    </testsuites>

<!--    UN-COMMENT & RUN TO GET CODE COVERAGE-->
<!--    <logging>-->
<!--        <log type="coverage-html" target="./coverage" charset="UTF-8"-->
<!--             yui="true" highlight="true" lowUpperBound="50" highLowerBound="80"-->
<!--             showUncoveredFiles="false" />-->
<!--    </logging>-->
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./Modules/*/Tests</directory>
        </whitelist>
    </filter>
    <php>
        <env name="DB_CONNECTION" value="sqlite_memory" force="true"/>
        <env name="APP_ENV" value="testing"/>

        <env name="APP_URL" value=""/>

        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>

    </php>
</phpunit>

Also, added sqlite_memory to config/database.php:

    'connections' => [
        'sqlite_memory' => [
            'driver' => 'sqlite',
            'database' => ':memory:',
            'prefix' => '',
        ],
        'sqlite' => [
            'driver' => 'sqlite',
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
        ],

Dharman
  • 30,962
  • 25
  • 85
  • 135
Eric Day
  • 142
  • 1
  • 10
  • ``php artisan migrate`` only runs files from migrations folder. when you create a package you have two options 1) publish your migrations to main migrations folder 2) use loadMigrationsFrom() function I dont' think that your setup is wrong but your can try this in your phpunit.xml `` `` – AmirRezaM75 Jan 01 '20 at 07:23
  • Do have the loadMigrationsFrom() function in all the module's serviceprovider. Also, the `env` options in the phpunit.xml is correct, and did try what you suggested. But did not work...any more ideas? – Eric Day Jan 01 '20 at 18:47

0 Answers0