Skip to content

Commit 0d94159

Browse files
committed
Docs for Fixture updates.
1 parent 00a4aca commit 0d94159

File tree

1 file changed

+45
-31
lines changed

1 file changed

+45
-31
lines changed

en/development/testing.rst

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ An example table would be::
510510
],
511511
],
512512
],
513-
// More tables.
513+
// More tables
514514
];
515515

516516
The options available to ``columns``, ``indexes`` and ``constraints`` match the
@@ -536,36 +536,6 @@ To load a SQL dump file you can use the following::
536536
At the beginning of each test run ``SchemaLoader`` will drop all tables in the
537537
connection and rebuild tables based on the provided schema file.
538538

539-
.. _fixture-state-management:
540-
541-
Fixture State Managers
542-
----------------------
543-
544-
By default CakePHP resets fixture state at the end of each test by truncating
545-
all the tables in the database. This operation can become expensive as your
546-
application grows. By using ``TransactionStrategy`` each test method will be run
547-
inside a transaction that is rolled back at the end of the test. This can yield
548-
improved performance but requires your tests not heavily rely on static fixture
549-
data, as auto-increment values are not reset before each test.
550-
551-
The fixture state management strategy can be defined within the test case::
552-
553-
use Cake\TestSuite\TestCase;
554-
use Cake\TestSuite\Fixture\FixtureStrategyInterface;
555-
use Cake\TestSuite\Fixture\TransactionStrategy;
556-
557-
class ArticlesTableTest extends TestCase
558-
{
559-
/**
560-
* Create the fixtures strategy used for this test case.
561-
* You can use a base class/trait to change multiple classes.
562-
*/
563-
protected function getFixtureStrategy(): FixtureStrategyInterface
564-
{
565-
return new TransactionStrategy();
566-
}
567-
}
568-
569539
Creating Fixtures
570540
-----------------
571541

@@ -744,6 +714,50 @@ You can also directly include fixtures by FQCN::
744714
}
745715

746716

717+
718+
.. _fixture-state-management:
719+
720+
Fixture State Managers
721+
----------------------
722+
723+
By default CakePHP resets fixture state at the end of each test by truncating
724+
all the tables in the database. This operation can become expensive as your
725+
application grows. By using ``TransactionStrategy`` each test method will be run
726+
inside a transaction that is rolled back at the end of the test. This can yield
727+
improved performance but requires your tests not heavily rely on static fixture
728+
data, as auto-increment values are not reset before each test.
729+
730+
The fixture state management strategy can be defined within the test case::
731+
732+
use Cake\TestSuite\TestCase;
733+
use Cake\TestSuite\Fixture\FixtureStrategyInterface;
734+
use Cake\TestSuite\Fixture\TransactionStrategy;
735+
736+
class ArticlesTableTest extends TestCase
737+
{
738+
/**
739+
* Create the fixtures strategy used for this test case.
740+
* You can use a base class/trait to change multiple classes.
741+
*/
742+
protected function getFixtureStrategy(): FixtureStrategyInterface
743+
{
744+
return new TransactionStrategy();
745+
}
746+
}
747+
748+
To switch out the general default strategy, use Configure key ``TestSuite.fixtureStrategy`` in your ``app.php``::
749+
750+
'TestSuite' => [
751+
'fixtureStrategy' => \Cake\TestSuite\Fixture\TransactionStrategy::class,
752+
],
753+
754+
755+
The recommended strategy for medium and large applications is the TransactionStrategy, as it also
756+
leaves the test cases after each run in a more clean state, making cross-contamination and side-effects less likely.
757+
As pointed out before, the fixture's should not have primary keys fixated, and neither should the tests
758+
expect fixture data with a specific ID. Instead, query the fixtures before your test run and use those then for the subsequent
759+
tests.
760+
747761
Fixture Factories
748762
-----------------
749763

0 commit comments

Comments
 (0)