Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested transactions in Yii2 unit tests #3520

Closed
kalyabin opened this issue Sep 15, 2016 · 0 comments
Closed

Nested transactions in Yii2 unit tests #3520

kalyabin opened this issue Sep 15, 2016 · 0 comments
Labels
Yii

Comments

@kalyabin
Copy link
Contributor

@kalyabin kalyabin commented Sep 15, 2016

What are you trying to achieve?

I use Yii2 Codeception module and ActiveFixtures for unit tests. It's required before each test begin a transaction in the database and rollback it after the test.

I use option cleanup to wrapping each test within transaction.

What do you get instead?

My DB is MySQL and it does not supports ALTER TABLE within transactions. But the ActiveFixture model clears all data in table including a sequence.

If my code has no nested transactions it's ok, but if testing code has nested transaction I saw PDOException like this:

There were 2 errors:

---------
1) UserManagerTest: Create admin
 Test  codeception/unit/UserManagerTest.php:testCreateAdmin

  [yii\db\Exception] SQLSTATE[42000]: Syntax error or access violation: 1305 SAVEPOINT LEVEL1 does not exist
The SQL being executed was: ROLLBACK TO SAVEPOINT LEVEL1  

#1  /Users/max/www/comitka/web/vendor/yiisoft/yii2/db/Command.php:855
#2  /Users/max/www/comitka/web/vendor/yiisoft/yii2/db/Schema.php:423
#3  /Users/max/www/comitka/web/vendor/yiisoft/yii2/db/Transaction.php:189
#4  /Users/max/www/comitka/web/app/modules/user/UserModule.php:98
#5  /Users/max/www/comitka/web/app/modules/user/UserModule.php:54
#6  /Users/max/www/comitka/web/app/tests/codeception/unit/UserManagerTest.php:55
#7  UserManagerTest->testCreateAdmin

---------
2) UserManagerTest: Create user
 Test  codeception/unit/UserManagerTest.php:testCreateUser

  [yii\db\Exception] SQLSTATE[42000]: Syntax error or access violation: 1305 SAVEPOINT LEVEL1 does not exist
The SQL being executed was: RELEASE SAVEPOINT LEVEL1  

#1  /Users/max/www/comitka/web/vendor/yiisoft/yii2/db/Command.php:855
#2  /Users/max/www/comitka/web/vendor/yiisoft/yii2/db/Schema.php:414
#3  /Users/max/www/comitka/web/vendor/yiisoft/yii2/db/Transaction.php:159
#4  /Users/max/www/comitka/web/app/modules/user/UserModule.php:428
#5  /Users/max/www/comitka/web/app/tests/codeception/unit/UserManagerTest.php:290
#6  UserManagerTest->testCreateUser
<?php
namespace models;

use Codeception\Test\Unit;
use tests\codeception\fixtures\UserFixture;
use UnitTester;
use user\models\User;

/**
 * Test user manager
 */
class UserManagerTest extends Unit
{
    /**
     * @var UnitTester
     */
    protected $tester;

    /**
     * @inheritdoc
     */
    public function setUp()
    {
        parent::setUp();
        $this->getModule('Yii2')->haveFixtures([
            'users' => UserFixture::className(),
        ]);
    }

    /**
     * Test model validation and save
     *
     * @return User
     */
    public function testMe()
    {
        // assertions here
    }
}

Details

  • Codeception version: dev-master
  • PHP Version: 5.6.15
  • Operating System: OS X El Capitan
  • Installation type: Composer
  • List of installed packages (composer show)
  • Suite configuration:
class_name: UnitTester
modules:
    enabled:
        - Yii2:
            configFile: 'codeception/config/unit.php'
            part: init
            cleanup: true
        - Asserts
        - \Helper\Unit
kalyabin added a commit to kalyabin/Codeception that referenced this issue Sep 15, 2016
kalyabin added a commit to kalyabin/Codeception that referenced this issue Sep 16, 2016
@samdark samdark added the Yii label Sep 16, 2016
kalyabin pushed a commit to kalyabin/Codeception that referenced this issue Sep 23, 2016
@DavertMik DavertMik closed this in 57a2593 Sep 29, 2016
chris1312 pushed a commit to chris1312/Codeception that referenced this issue Jun 16, 2017
…n#3522)

* fixes Codeception#3520 nested transactions in yii2 module

* fixes Codeception#3520 use fixtures method for unit tests only
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.