Cakephp 3 - 运行 phpunit 测试时出现 MissingDatasourceConfigException
Cakephp 3 - MissingDatasourceConfigException when running phpunit test
我正在尝试 运行 使用 PHPUnit 4.7.3 在 CakePHP 3 中进行一些单元测试,但出现以下错误:
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.
我试着按照这本书做,但可能我遗漏了什么。
在 app.php 我有:
'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,
],
],
我的测试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>
问题出在命令提示符下,当我 运行 在 Windows PowerShell 中进行测试时一切正常。
我在使用 Ubuntu 14.04(这里没有 PowerShell)时遇到了同样的问题。就我而言,问题是我 运行 从错误的位置进行测试。根据 documentation 你需要 站在你的应用根文件夹中 才能正确地 运行 你的测试。
所以在你的情况下它会是这样的:
$ cd /path/to/your/app
$ vendor/bin/phpunit tests/TestCase/Model/Table/MoviesTable
我正在尝试 运行 使用 PHPUnit 4.7.3 在 CakePHP 3 中进行一些单元测试,但出现以下错误:
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.
我试着按照这本书做,但可能我遗漏了什么。
在 app.php 我有:
'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,
],
],
我的测试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>
问题出在命令提示符下,当我 运行 在 Windows PowerShell 中进行测试时一切正常。
我在使用 Ubuntu 14.04(这里没有 PowerShell)时遇到了同样的问题。就我而言,问题是我 运行 从错误的位置进行测试。根据 documentation 你需要 站在你的应用根文件夹中 才能正确地 运行 你的测试。
所以在你的情况下它会是这样的:
$ cd /path/to/your/app
$ vendor/bin/phpunit tests/TestCase/Model/Table/MoviesTable