Skip to content

Commit d05f06d

Browse files
leandrogehlenDavertMik
authored andcommitted
[Yii] Splitted cleanup and transactional configuration (#4379)
* Splitted `cleanup` and `transactional` configuration * Avoid BC with `transaction` setting * Fixes code style * Fixes code style * Fixes `rollBack` method invocation
1 parent cc1b6a8 commit d05f06d

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

src/Codeception/Module/Yii2.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
* * `configFile` *required* - the path to the application config file. File should be configured for test environment and return configuration array.
2323
* * `entryUrl` - initial application url (default: http://localhost/index-test.php).
2424
* * `entryScript` - front script title (like: index-test.php). If not set - taken from entryUrl.
25-
* * `cleanup` - (default: true) wrap all database connection inside a transaction and roll it back after the test. Should be disabled for acceptance testing..
25+
* * `transaction` - (default: true) wrap all database connection inside a transaction and roll it back after the test. Should be disabled for acceptance testing..
26+
* * `cleanup` - (default: true) cleanup fixtures after the test
2627
*
2728
* You can use this module by setting params in your functional.suite.yml:
2829
*
@@ -77,7 +78,8 @@
7778
* - Yii2:
7879
* configFile: 'config/test.php'
7980
* part: ORM # allow to use AR methods
80-
* cleanup: false # don't wrap test in transaction
81+
* transaction: false # don't wrap test in transaction
82+
* cleanup: false # don't cleanup the fixtures
8183
* entryScript: index-test.php
8284
* ```
8385
*
@@ -91,7 +93,7 @@
9193
* $I->haveFixtures(['posts' => PostsFixture::className()]);
9294
* ```
9395
*
94-
* or, if you need to load fixtures before the test (probably before the cleanup transaction is started), you
96+
* or, if you need to load fixtures before the test, you
9597
* can specify fixtures with `_fixtures` method of a testcase:
9698
*
9799
* ```php
@@ -131,6 +133,7 @@ class Yii2 extends Framework implements ActiveRecord, PartedModule
131133
*/
132134
protected $config = [
133135
'cleanup' => true,
136+
'transaction' => null,
134137
'entryScript' => '',
135138
'entryUrl' => 'http://localhost/index-test.php',
136139
];
@@ -150,6 +153,10 @@ class Yii2 extends Framework implements ActiveRecord, PartedModule
150153

151154
public function _initialize()
152155
{
156+
if ($this->config['transaction'] === null) {
157+
$this->config['transaction'] = $this->config['cleanup'];
158+
}
159+
153160
if (!is_file(Configuration::projectDir() . $this->config['configFile'])) {
154161
throw new ModuleConfigException(
155162
__CLASS__,
@@ -184,7 +191,7 @@ public function _before(TestInterface $test)
184191
$this->loadFixtures($test);
185192
}
186193

187-
if ($this->config['cleanup']
194+
if ($this->config['transaction']
188195
&& $this->app->has('db')
189196
&& $this->app->db instanceof \yii\db\Connection
190197
) {
@@ -221,13 +228,12 @@ public function _after(TestInterface $test)
221228
$fixture->unloadFixtures();
222229
}
223230
$this->loadedFixtures = [];
224-
225-
if ($this->transaction) {
226-
$this->transaction->rollback();
227-
$this->debugSection('Database', 'Transaction cancelled; all changes reverted.');
228-
}
229231
}
230-
232+
233+
if ($this->config['transaction'] && $this->transaction) {
234+
$this->transaction->rollBack();
235+
$this->debugSection('Database', 'Transaction cancelled; all changes reverted.');
236+
}
231237

232238
if ($this->client) {
233239
$this->client->resetPersistentVars();

0 commit comments

Comments
 (0)