Cakephp 3 - MissingDatasourceConfigException when running phpunit test

前提是你 提交于 2019-12-24 03:24:02

问题


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>

回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/30800709/cakephp-3-missingdatasourceconfigexception-when-running-phpunit-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!