DanielPenkov
DanielPenkov

Reputation: 87

Cakephp 3 - MissingDatasourceConfigException when running phpunit test

I am trying to run some unit tests in CakePHP 3 with PHPUnit 4.7.3, but I`m getting the following error:

PHPUnit 4.7.3 by Sebastian Bergmann and contributors.

There was 1 error:

1) App\Test\TestCase\Model\Table\MoviesTableTest::testFindMoviesByGenre
Cake\Datasource\Exception\MissingDatasourceConfigException: The datasource configuration "default" was not found.

C:\xampp\htdocs\movie-pal\vendor\cakephp\cakephp\src\Datasource\ConnectionManager.php:188
C:\xampp\htdocs\movie-pal\vendor\cakephp\cakephp\src\ORM\TableRegistry.php:191
C:\xampp\htdocs\movie-pal\tests\TestCase\Model\Table\MoviesTableTest.php:17

FAILURES!
Tests: 1, Assertions: 0, Errors: 1.

I have tried to follow the book, but probably I`m missing something.

In the app.php I have:

'Datasources' => [
    'default' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => 'localhost',
        'username' => 'root',
        'password' => '',
        'database' => 'movies',
        'encoding' => 'utf8',
        'timezone' => 'UTC',
        'cacheMetadata' => true,
        'quoteIdentifiers' => false,
    ],
    'test' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => 'localhost',
        'username' => 'root',
        'password' => '',
        'database' => 'test_movies',
        'encoding' => 'utf8',
        'timezone' => 'UTC',
        'cacheMetadata' => true,
        'quoteIdentifiers' => false,
    ],
],

My test class:

namespace App\Test\TestCase\Model\Table;

use App\Model\Table\MoviesTable;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;

class MoviesTableTest extends TestCase
{
    public $fixtures = ['app.movies'];

    public function setUp()
    {
        parent::setUp();
        $this->Movies = TableRegistry::get('Movies');
    }

    public function testFindMoviesByGenre()
    {
       .......
    }

}

phpunit.xml

 <?xml version="1.0" encoding="UTF-8"?>
<phpunit
  colors="true"
  processIsolation="false"
  stopOnFailure="false"
  syntaxCheck="false"
  bootstrap="./tests/bootstrap.php"
  >
  <php>
    <ini name="memory_limit" value="-1"/>
    <ini name="apc.enable_cli" value="1"/>
  </php>

  <!-- Add any additional test suites you want to run here -->
  <testsuites>
    <testsuite name="App Test Suite">
      <directory>./tests/TestCase</directory>
    </testsuite>
    <!-- Add plugin test suites here. -->
  </testsuites>

  <!-- Setup a listener for fixtures -->
  <listeners>
    <listener
    class="\Cake\TestSuite\Fixture\FixtureInjector"
    file="./vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureInjector.php">
      <arguments>
        <object class="\Cake\TestSuite\Fixture\FixtureManager" />
      </arguments>
    </listener>
  </listeners>

</phpunit>

Upvotes: 2

Views: 1165

Answers (2)

Parzival
Parzival

Reputation: 51

I had the same problem using Ubuntu 14.04 (no PowerShell here). In my case the problem was that I was running the tests from a wrong location. According to the documentation you need to be standing in your app root's folder to correctly run your tests.

So in your case it would be something like this:

$ cd /path/to/your/app
$ vendor/bin/phpunit tests/TestCase/Model/Table/MoviesTable

Upvotes: 1

DanielPenkov
DanielPenkov

Reputation: 87

The problem was in the Command prompt, when I run my tests in Windows PowerShell everything was OK.

Upvotes: 0

Related Questions