Fixture improvements - markstory/cakephp GitHub Wiki
With the new schema system underway, fixtures will need to be updated as well. There are only a few problematic areas around fixtures:
- Import fixtures are a mess. They are fragile and currently use the entire model stack.
- Importing records is stupid. You almost never want to import the records from the development database into the test suite. This feature should be removed.
- Skipping schema should be possible. There are situations where you would want to have predefined empty tables setup in a test database. The testsuite currently makes this very difficult.
There are two primary kinds of fixtures in CakePHP. Static, and import. Both kinds of fixtures are supported by Cake\TestSuite\Fixture\TestFixture
.
Static fixtures
Static fixtures define both the schema and records in the Fixture class. This kind of fixture is used extensively by CakePHP itself, and by many plugins. It is an extremely portable fixture format that sacrifices some performance for portability. These fixtures are required to define both the $fields
and $records
properties. In addition they may define the $table
or $connection
property.
An example fixture would look like:
<?php
namespace App\TestSuite\Fixture;
class ArticleFixture extends Fixture {
// Define the table if it is not inflected from the classname
public $table = 'articles';
// Define the connection name if it is not 'test'
public $connection = 'custom_ds';
// Define the schema for the table.
public $fields = [
'id' => ['type' => 'integer', 'null' => false],
'title' => 'string',
'body' => 'text',
'constraints' => [
'primary' => ['columns' => ['id'], 'type' => 'primary'],
],
'indexes' => [
'title_idx' => ['columns' => ['title'], 'type' => 'index'],
]
];
// Define the records for a fixture.
public $records = [
// records here
];
}
The constraints
and indexes
keys are special in that they allow the definition of constraints and indexes for a table.
Import fixtures
Import fixtures allow you to keep your code DRY'er by not repeating the schema for your application. They allow you to reflect and recreate the schema into the test database using CakePHP's schema system. Imported schemas can be lossy if you are using platform specific features. If our ArticleFixture
was defined as an import fixture it would look like:
<?php
namespace App\TestSuite\Fixture;
class ArticleFixture extends Fixture {
// Define the table if it is not inflected from the classname
public $table = 'articles';
// Define the connection name if it is not 'test'
public $connection = 'custom_ds';
// Define the table and connection to import from.
// You can no longer define just a model.
public $import = ['table' => 'articles', 'connection' => 'default'];
// Define the records for a fixture.
public $records = [
// records here
];
}
Import fixtures are often shorter and more useful for general application development as they are always in sync with your application schema. You can also set import to false
and leave $fields
undefined to make the fixture assume that the tables will already be set in the test database connections.