Я на CakePHP v3.x с phpunit v5.1.3. и мой unit test, похоже, не признает существование моих светильников. Похоже, что тесты считываются из локального (default
) подключения к базе данных.
Здесь мой класс TestCase:
namespace App\Test\TestCase\Model\Table;
use Cake\Datasource\ConnectionManager;
use Cake\I18n\Time;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
/**
* App\Model\Table\ScreensTable Test Case
*
* @property \App\Model\Table\ScreensTable ScreensTable
*/
class ScreensTableTest extends TestCase
{
/**
* Fixtures
*
* @var array
*/
public $fixtures = [
'app.screens'
];
/**
* setUp method
*
* @return void
*/
public function setUp()
{
parent::setUp();
$config = TableRegistry::exists('Screens') ? [] : ['className' => 'App\Model\Table\ScreensTable'];
$this->Screens = TableRegistry::get('Screens', $config);
}
public testSomethingHere(){
$newScreen = $this->Screens->newEntity(['who' => 'cares']);
$screen = $this->Screens->save($newScreen);
// ^ This record will show up in my app DB!
}
Здесь мой файл привязки:
<?php
namespace App\Test\Fixture;
use App\TestSuite\Fixture\TestFixture;
/**
* ScreensFixture
*
*/
class ScreensFixture extends TestFixture
{
/**
* Records
*
* @var array
*/
public $records = [
[
'id' => 2,
'name' => 'Bla bla',
],
...
Когда я debug()
возвращаемое значение из $this->Screens->find('all')->order(['id'=>'ASC'])->first()
, я вижу первую запись REAL в БД. Не мое приспособление.
Что мне не хватает?
Update1: Ниже перечислены соединения, определенные в моем файле конфигурации:
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Postgres',
'persistent' => false,
'host' => 'localhost',
'port' => '5432',
'username' => 'postgres',
'password' => 'postgres',
'database' => 'transitscreen',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
'quoteIdentifiers' => false,
],
/**
* Used by test suites
*/
'test' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Postgres',
'persistent' => false,
'host' => 'localhost',
'port' => '5432',
'username' => 'postgres',
'password' => 'postgres',
'database' => 'ts_test',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
'quoteIdentifiers' => false,
],
],
Обновление
Содержимое App\Test\Fixture
(Не думаю, что здесь что-то интересное происходит... Я думаю, что это просто расширяет класс светильника Cake)
<?php
namespace App\TestSuite\Fixture;
use Cake\I18n\Time;
use Cake\TestSuite\Fixture\TestFixture as CoreTestFixture;
/**
* Override core TestFixture.
*/
class TestFixture extends CoreTestFixture
{
/**
* Initialize the fixture.
*
* @return void
* @throws \Cake\ORM\Exception\MissingTableClassException When importing from a table that does not exist.
*/
public function init()
{
$this->setRecordTimestamps();
$this->encodeJsonColumns();
parent::init();
}
/**
* Set record timestamps.
*
* - created
* - modified
*
* @return void
*/
public function setRecordTimestamps()
{
foreach (['created', 'modified'] as $timestampField) {
if (array_key_exists($timestampField, $this->fields)) {
foreach ($this->records as &$record) {
$record[$timestampField] = new Time();
}
}
}
}
/**
* Encode JSON columns.
*
* Bake will output JSON fields as arrays (because of the `JsonType` object mapped to this field) but since
* migrations execute directly against the database we need to encode these fields manually before insertion.
*
* @return void
*/
public function encodeJsonColumns()
{
$fixtureName = namespaceSplit(get_called_class())[1];
if ($fixtureName === 'DatasourcesFixture' && array_key_exists('widget_options', $this->fields)) {
foreach ($this->records as &$record) {
$record['widget_options'] = json_encode($record['widget_options']);
}
}
}
}