Class TestFixture

Cake TestFixture is responsible for building and destroying tables to be used during testing.

Properties summary

  • $_constraints protected
    array

    Fixture constraints to be created.

  • $_schema protected
    \Cake\Database\Schema\TableSchema

    The schema for this fixture.

  • $_tableLocator protected
    \Cake\ORM\Locator\LocatorInterface

    Table locator instance

  • $connection public
    string

    Fixture Datasource

  • $fields public
    array

    Fields / Schema for the fixture.

  • $import public
    array|null

    Configuration for importing fixture schema

  • $records public
    array

    Fixture records to be inserted.

  • $table public
    string

    Full Table Name

Method Summary

Method Detail

__construct() public

__construct()

Instantiate the fixture.

Throws

Cake\Core\Exception\Exception
on invalid datasource usage.

_getRecords() protected

_getRecords()

Converts the internal records into data used to generate a query.

Returns

array

_schemaFromFields() protected

_schemaFromFields()

Build the fixtures table schema from the fields property.

_schemaFromImport() protected

_schemaFromImport()

Build fixture schema from a table in another datasource.

Throws

Cake\Core\Exception\Exception
when trying to import from an empty table.

_schemaFromReflection() protected

_schemaFromReflection()

Build fixture schema directly from the datasource

Throws

Cake\Core\Exception\Exception
when trying to reflect a table that does not exist

_tableFromClass() protected

_tableFromClass()

Returns the table name using the fixture class

Returns

string

connection() public

connection()

Get the connection name this fixture should be inserted into.

Returns

string

create() public

create(\Cake\Datasource\ConnectionInterface $db)

Create the fixture schema/mapping/definition

Parameters

\Cake\Datasource\ConnectionInterface $db

An instance of the connection the fixture should be created on.

Returns

bool

True on success, false on failure.

createConstraints() public

createConstraints(\Cake\Datasource\ConnectionInterface $db)

Build and execute SQL queries necessary to create the constraints for the fixture

Parameters

\Cake\Datasource\ConnectionInterface $db

An instance of the database into which the constraints will be created

Returns

bool

on success or if there are no constraints to create, or false on failure

drop() public

drop(\Cake\Datasource\ConnectionInterface $db)

Run after all tests executed, should remove the table/collection from the connection.

Parameters

\Cake\Datasource\ConnectionInterface $db

An instance of the connection the fixture should be removed from.

Returns

bool

True on success, false on failure.

dropConstraints() public

dropConstraints(\Cake\Datasource\ConnectionInterface $db)

Build and execute SQL queries necessary to drop the constraints for the fixture

Parameters

\Cake\Datasource\ConnectionInterface $db

An instance of the database into which the constraints will be dropped

Returns

bool

on success or if there are no constraints to drop, or false on failure

getTableLocator() public

getTableLocator()

Gets the table locator.

Returns

\Cake\ORM\Locator\LocatorInterface

getTableSchema() public

getTableSchema()

init() public

init()

Initialize the fixture.

Throws

Cake\ORM\Exception\MissingTableClassException
When importing from a table that does not exist.

insert() public

insert(\Cake\Datasource\ConnectionInterface $db)

Run before each test is executed.

Should insert all the records into the test database.

Parameters

\Cake\Datasource\ConnectionInterface $db

An instance of the connection into which the records will be inserted.

Returns

\Cake\Database\StatementInterface|bool

on success or if there are no records to insert, or false on failure.

schema() public

schema(\Cake\Database\Schema\TableSchema $schema)

Gets/Sets the TableSchema instance used by this fixture.

Parameters

\Cake\Database\Schema\TableSchema|null $schema optional

The table to set.

Returns

\Cake\Database\Schema\TableSchema|null

setTableLocator() public

setTableLocator(\Cake\ORM\Locator\LocatorInterface $tableLocator)

Sets the table locator.

Parameters

\Cake\ORM\Locator\LocatorInterface $tableLocator

LocatorInterface instance.

Returns

$this

setTableSchema() public

setTableSchema(\Cake\Database\Schema\TableSchemaInterface $schema)

Get and set the schema for this fixture.

Parameters

\Cake\Database\Schema\TableSchemaInterface $schema

The table to set.

Returns

$this

sourceName() public

sourceName()

Get the table/collection name for this fixture.

Returns

string

tableLocator() public

tableLocator(\Cake\ORM\Locator\LocatorInterface $tableLocator)

Sets the table locator.

If no parameters are passed, it will return the currently used locator.

Parameters

\Cake\ORM\Locator\LocatorInterface|null $tableLocator optional

LocatorInterface instance.

Returns

\Cake\ORM\Locator\LocatorInterface

truncate() public

truncate(\Cake\Datasource\ConnectionInterface $db)

Truncates the current fixture.

Parameters

\Cake\Datasource\ConnectionInterface $db

A reference to a db instance

Returns

bool

Property Detail

$_constraints protected

Fixture constraints to be created.

Type

array

$_schema protected

The schema for this fixture.

Type

\Cake\Database\Schema\TableSchema

$_tableLocator protected

Table locator instance

Type

\Cake\ORM\Locator\LocatorInterface

$connection public

Fixture Datasource

Type

string

$fields public

Fields / Schema for the fixture.

This array should be compatible with Cake\Database\Schema\Schema. The _constraints, _options and _indexes keys are reserved for defining constraints, options and indexes respectively.

Type

array

$import public

Configuration for importing fixture schema

Accepts a connection and model or table key, to define which table and which connection contain the schema to be imported.

Type

array|null

$records public

Fixture records to be inserted.

Type

array

$table public

Full Table Name

Type

string

© 2005–present The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/3.9/class-Cake.TestSuite.Fixture.TestFixture.html