PHPUnit will not use :memory: database

Posted 2 months ago by nexxai

Ok so I'm definitely taking crazy pills, I just can't figure out which ones.

No matter what I do, I cannot get PHPUnit to use the in-memory database. I've tried:

Setting my .env to this (just to ensure it wasn't using the real database connection in the background):

DB_CONNECTION=sqlite
DB_DATABASE=":memory:"

and also:

DB_CONNECTION=testing

while having database.php show:

        'testing' => [
            'driver' => 'sqlite',
            'database' => ':memory:',
            'prefix' => '',
        ],

Configuring my phpunit.xml to use:

        <env name="DB_CONNECTION" value="testing"/>

as well as:

        <env name="DB_CONNECTION" value="sqlite"/>
        <env name="DB_DATABASE" value=":memory:"/>

This is an example error, which is weird because it's showing the Connection.php error inside the HTML of the page it's testing:

1) Tests\Feature\ExampleTest::a_user_can_reorder_a_Examples_characters_by_importance
Failed asserting that Failed asserting that '<!DOCTYPE html><!--


Illuminate\Database\QueryException: SQLSTATE[HY000] [2002] Connection refused (SQL: select * from `sessions` where `id` = y6wLaM4CgTSQjVjpHzzcxOISLhZTfoLpaCr9mEiN limit 1) in file /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 664
Stack trace:
  1. Illuminate\Database\QueryException-&gt;() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664
  2. Doctrine\DBAL\Driver\PDOException-&gt;() /Users/nexxai/code/examplesite/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:31
  3. PDOException-&gt;() /Users/nexxai/code/examplesite/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:27
  4. PDO-&gt;__construct() /Users/nexxai/code/examplesite/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:27
  5. Doctrine\DBAL\Driver\PDOConnection-&gt;__construct() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:67
  6. Illuminate\Database\Connectors\Connector-&gt;createPdoConnection() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:46
  7. Illuminate\Database\Connectors\Connector-&gt;createConnection() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Database/Connectors/MySqlConnector.php:24
  8. Illuminate\Database\Connectors\MySqlConnector-&gt;connect() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Database/Connectors/ConnectionFactory.php:182
  9. Illuminate\Database\Connectors\ConnectionFactory-&gt;Illuminate\Database\Connectors\{closure}() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Database/Connection.php:918
 10. call_user_func() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Database/Connection.php:918
 11. Illuminate\Database\Connection-&gt;getPdo() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Database/Connection.php:943
 12. Illuminate\Database\Connection-&gt;getReadPdo() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Database/Connection.php:399
 13. Illuminate\Database\Connection-&gt;getPdoForSelect() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Database/Connection.php:325
 14. Illuminate\Database\Connection-&gt;Illuminate\Database\{closure}() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Database/Connection.php:657
 15. Illuminate\Database\Connection-&gt;runQueryCallback() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Database/Connection.php:624
 16. Illuminate\Database\Connection-&gt;run() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Database/Connection.php:333
 17. Illuminate\Database\Connection-&gt;select() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2124
 18. Illuminate\Database\Query\Builder-&gt;runSelect() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2112
 19. Illuminate\Database\Query\Builder-&gt;Illuminate\Database\Query\{closure}() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2598
 20. Illuminate\Database\Query\Builder-&gt;onceWithColumns() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2113
 21. Illuminate\Database\Query\Builder-&gt;get() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Database/Concerns/BuildsQueries.php:77
 22. Illuminate\Database\Query\Builder-&gt;first() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2087
 23. Illuminate\Database\Query\Builder-&gt;find() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Session/DatabaseSessionHandler.php:91
 24. Illuminate\Session\DatabaseSessionHandler-&gt;read() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Session/Store.php:97
 25. Illuminate\Session\Store-&gt;readFromHandler() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Session/Store.php:87
 26. Illuminate\Session\Store-&gt;loadSession() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Session/Store.php:71
 27. Illuminate\Session\Store-&gt;start() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php:81
 28. Illuminate\Session\Middleware\StartSession-&gt;Illuminate\Session\Middleware\{closure}() /Users/nexxai/.composer/vendor/illuminate/support/helpers.php:1027
 29. tap() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php:82
 30. Illuminate\Session\Middleware\StartSession-&gt;startSession() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php:51
 31. Illuminate\Session\Middleware\StartSession-&gt;handle() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:163
 32. Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
 33. Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php:37
 34. Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse-&gt;handle() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:163
 35. Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
 36. Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}() /Users/nexxai/code/examplesite/app/Http/Middleware/UpdateLastSeen.php:19
 37. App\Http\Middleware\UpdateLastSeen-&gt;handle() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:163
 38. Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
 39. Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php:66
 40. Illuminate\Cookie\Middleware\EncryptCookies-&gt;handle() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:163
 41. Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
 42. Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:104
 43. Illuminate\Pipeline\Pipeline-&gt;then() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Routing/Router.php:682
 44. Illuminate\Routing\Router-&gt;runRouteWithinStack() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Routing/Router.php:657
 45. Illuminate\Routing\Router-&gt;runRoute() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Routing/Router.php:623
 46. Illuminate\Routing\Router-&gt;dispatchToRoute() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Routing/Router.php:612
 47. Illuminate\Routing\Router-&gt;dispatch() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:176
 48. Illuminate\Foundation\Http\Kernel-&gt;Illuminate\Foundation\Http\{closure}() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:30
 49. Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}() /Users/nexxai/code/examplesite/vendor/beyondcode/laravel-query-detector/src/QueryDetectorMiddleware.php:33
 50. BeyondCode\QueryDetector\QueryDetectorMiddleware-&gt;handle() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:163
 51. Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
 52. Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}() /Users/nexxai/code/examplesite/vendor/barryvdh/laravel-debugbar/src/Middleware/InjectDebugbar.php:58
 53. Barryvdh\Debugbar\Middleware\InjectDebugbar-&gt;handle() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:163
 54. Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
 55. Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}() /Users/nexxai/code/examplesite/vendor/fideloper/proxy/src/TrustProxies.php:57
 56. Fideloper\Proxy\TrustProxies-&gt;handle() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:163
 57. Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
 58. Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php:21
 59. Illuminate\Foundation\Http\Middleware\TransformsRequest-&gt;handle() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:163
 60. Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
 61. Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php:21
 62. Illuminate\Foundation\Http\Middleware\TransformsRequest-&gt;handle() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:163
 63. Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
 64. Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php:27
 65. Illuminate\Foundation\Http\Middleware\ValidatePostSize-&gt;handle() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:163
 66. Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
 67. Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php:62
 68. Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode-&gt;handle() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:163
 69. Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
 70. Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:104
 71. Illuminate\Pipeline\Pipeline-&gt;then() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:151
 72. Illuminate\Foundation\Http\Kernel-&gt;sendRequestThroughRouter() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:116
 73. Illuminate\Foundation\Http\Kernel-&gt;handle() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:347
 74. Illuminate\Foundation\Testing\TestCase-&gt;call() /Users/nexxai/code/examplesite/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:170
 75. Illuminate\Foundation\Testing\TestCase-&gt;get() /Users/nexxai/code/examplesite/tests/Feature/ExampleTest.php:145
 76. Tests\Feature\ExampleTest-&gt;a_user_can_reorder_a_Examples_characters_by_importance() /Users/nexxai/code/examplesite/vendor/phpunit/phpunit/src/Framework/TestCase.php:1153
 77. PHPUnit\Framework\TestCase-&gt;runTest() /Users/nexxai/code/examplesite/vendor/phpunit/phpunit/src/Framework/TestCase.php:842
 78. PHPUnit\Framework\TestCase-&gt;runBare() /Users/nexxai/code/examplesite/vendor/phpunit/phpunit/src/Framework/TestResult.php:687
 79. PHPUnit\Framework\TestResult-&gt;run() /Users/nexxai/code/examplesite/vendor/phpunit/phpunit/src/Framework/TestCase.php:796
 80. PHPUnit\Framework\TestCase-&gt;run() /Users/nexxai/code/examplesite/vendor/phpunit/phpunit/src/Framework/TestSuite.php:746
 81. PHPUnit\Framework\TestSuite-&gt;run() /Users/nexxai/code/examplesite/vendor/phpunit/phpunit/src/Framework/TestSuite.php:746
 82. PHPUnit\Framework\TestSuite-&gt;run() /Users/nexxai/code/examplesite/vendor/phpunit/phpunit/src/Framework/TestSuite.php:746
 83. PHPUnit\Framework\TestSuite-&gt;run() /Users/nexxai/code/examplesite/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:641
 84. PHPUnit\TextUI\TestRunner-&gt;doRun() /Users/nexxai/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:207
 85. PHPUnit\TextUI\Command-&gt;run() /Users/nexxai/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:163
 86. PHPUnit\TextUI\Command-&gt;main() /Users/nexxai/.composer/vendor/phpunit/phpunit/phpunit:61











--><html>
  <head>
    <meta charset="utf-8">
    <meta name="robots" content="noindex,nofollow"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"/>
    <title>Whoops! There was an error.</title>

It's almost as if PHPUnit is ignoring the phpunit.xml file altogether.

Is there a way that I can test (even something stupid like echoing out a "caught" string) if it's even seeing the phpunit.xml file, or if there's something obvious that I've done wrong with it?

<?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="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>

        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
        </whitelist>
    </filter>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="DB_CONNECTION" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
    </php>
</phpunit>

Please sign in or create an account to participate in this conversation.