From 0d941593fb32e00f8ec6bfad6e4b0b03c84188b6 Mon Sep 17 00:00:00 2001 From: mscherer Date: Tue, 22 Apr 2025 21:27:27 +0200 Subject: [PATCH 1/2] Docs for Fixture updates. --- en/development/testing.rst | 76 ++++++++++++++++++++++---------------- 1 file changed, 45 insertions(+), 31 deletions(-) diff --git a/en/development/testing.rst b/en/development/testing.rst index 288df98e94..658e79526a 100644 --- a/en/development/testing.rst +++ b/en/development/testing.rst @@ -510,7 +510,7 @@ An example table would be:: ], ], ], - // More tables. + // More tables ]; 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:: At the beginning of each test run ``SchemaLoader`` will drop all tables in the connection and rebuild tables based on the provided schema file. -.. _fixture-state-management: - -Fixture State Managers ----------------------- - -By default CakePHP resets fixture state at the end of each test by truncating -all the tables in the database. This operation can become expensive as your -application grows. By using ``TransactionStrategy`` each test method will be run -inside a transaction that is rolled back at the end of the test. This can yield -improved performance but requires your tests not heavily rely on static fixture -data, as auto-increment values are not reset before each test. - -The fixture state management strategy can be defined within the test case:: - - use Cake\TestSuite\TestCase; - use Cake\TestSuite\Fixture\FixtureStrategyInterface; - use Cake\TestSuite\Fixture\TransactionStrategy; - - class ArticlesTableTest extends TestCase - { - /** - * Create the fixtures strategy used for this test case. - * You can use a base class/trait to change multiple classes. - */ - protected function getFixtureStrategy(): FixtureStrategyInterface - { - return new TransactionStrategy(); - } - } - Creating Fixtures ----------------- @@ -744,6 +714,50 @@ You can also directly include fixtures by FQCN:: } + +.. _fixture-state-management: + +Fixture State Managers +---------------------- + +By default CakePHP resets fixture state at the end of each test by truncating +all the tables in the database. This operation can become expensive as your +application grows. By using ``TransactionStrategy`` each test method will be run +inside a transaction that is rolled back at the end of the test. This can yield +improved performance but requires your tests not heavily rely on static fixture +data, as auto-increment values are not reset before each test. + +The fixture state management strategy can be defined within the test case:: + + use Cake\TestSuite\TestCase; + use Cake\TestSuite\Fixture\FixtureStrategyInterface; + use Cake\TestSuite\Fixture\TransactionStrategy; + + class ArticlesTableTest extends TestCase + { + /** + * Create the fixtures strategy used for this test case. + * You can use a base class/trait to change multiple classes. + */ + protected function getFixtureStrategy(): FixtureStrategyInterface + { + return new TransactionStrategy(); + } + } + +To switch out the general default strategy, use Configure key ``TestSuite.fixtureStrategy`` in your ``app.php``:: + + 'TestSuite' => [ + 'fixtureStrategy' => \Cake\TestSuite\Fixture\TransactionStrategy::class, + ], + + +The recommended strategy for medium and large applications is the TransactionStrategy, as it also +leaves the test cases after each run in a more clean state, making cross-contamination and side-effects less likely. +As pointed out before, the fixture's should not have primary keys fixated, and neither should the tests +expect fixture data with a specific ID. Instead, query the fixtures before your test run and use those then for the subsequent +tests. + Fixture Factories ----------------- From 904ac2aa2ad83956592dd30169342dc87ca531a2 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Wed, 23 Apr 2025 09:57:34 -0400 Subject: [PATCH 2/2] Update en/development/testing.rst --- en/development/testing.rst | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/en/development/testing.rst b/en/development/testing.rst index 658e79526a..4e11e505a3 100644 --- a/en/development/testing.rst +++ b/en/development/testing.rst @@ -752,11 +752,7 @@ To switch out the general default strategy, use Configure key ``TestSuite.fixtur ], -The recommended strategy for medium and large applications is the TransactionStrategy, as it also -leaves the test cases after each run in a more clean state, making cross-contamination and side-effects less likely. -As pointed out before, the fixture's should not have primary keys fixated, and neither should the tests -expect fixture data with a specific ID. Instead, query the fixtures before your test run and use those then for the subsequent -tests. +The recommended strategy for medium and large applications is the ``TransactionStrategy``, as using rollbacks to undo changes from tests is simpler to maintain, and reduces the chances of cross-contamination and side-effects between tests. Fixture Factories -----------------