I’m trying to load a fixture and its data into my CakePHP Unit tests.
The fixture listener is set in phpunit.xml.dist.
There’s the fixture ‘AdminServiceFixture.php’ in the tests/Fixture folder with the content:
<?php
namespace App\Test\Fixture;
use Cake\TestSuite\Fixture\TestFixture;
class AdminServiceFixture extends TestFixture
{
public $connection = 'test';
public $fields = [
'id' => ['type' => 'integer'],
'user_id' => ['type' => 'integer'],
'service_id' => ['type' => 'integer'],
'_constraints' => [
'primary' => ['type' => 'primary', 'columns' => ['id']]
]
];
public $records = [
[
'user_id' => 1,
'service_id' => 2,
],
];
}
There’s the model test file ‘AdminServiceTableTest’ defined as:
<?php
namespace App\Test\TestCase\Model\Table;
use App\Model\Table\AdminServiceTable;
use Cake\TestSuite\TestCase;
class AdminServiceTableTest extends TestCase
{
private $AdminService;
protected $fixtures = ['app.AdminService'];
function setUp(): void {
parent::setUp();
$this->AdminService = new AdminServiceTable;
$this->loadFixtures('AdminService');
}
function testGetAdminService() {
$this->addFixture('app.AdminService');
//setup
$user_id = 1;
//test
$actual = $this->AdminService->getAdminService($user_id);
//assert
$expected = 1;
$this->assertEquals($expected, $actual);
}
}
But it doesn’t load and test against the fixture and its data, it’s still using the data in the Test database.