yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.9k forks source link

Using same fixture class multiple times in a test #18340

Open Tarinu opened 4 years ago

Tarinu commented 4 years ago

What steps will reproduce the problem?

  1. Create a basic yii2 app
  2. Setup a database connection and create the tester classes (php vendor/bin/codecept build)
  3. Create foo.php and bar.php files in tests/_data directory, the data they return is irrelevant
  4. Create the test class

    <?php
    // tests/unit/FooTest.php
    
    use yii\test\ArrayFixture;
    
    class FooTest extends \Codeception\Test\Unit
    {
        /**
         * @var \UnitTester
         */
        protected $tester;
    
        public function _fixtures()
        {
            return [
                'foo' => [
                    'class' => ArrayFixture::class,
                    'dataFile' => codecept_data_dir('foo.php')
                ],
                'bar' => [
                    'class' => ArrayFixture::class,
                    'dataFile' => codecept_data_dir('bar.php')
                ]
            ];
        }
    
        // tests
        public function testSomeFeature()
        {
            // holds foo fixture data
            $this->tester->grabFixture('bar');
            // throws [ModuleException] Yii2: Fixture foo is not loaded
            $this->tester->grabFixture('foo');
        }
    }
  5. Run the test php vendor/bin/codecept run unit -- FooTest

What is the expected result?

Both foo and bar fixtures are loaded with correct data

What do you get instead?

Only bar fixture is loaded but with wrong data

Additional info

The issue seems to come from how FixtureTrait assigns aliases to fixtures. Each class will only have 1 alias, in this case 'yii\test\ArrayFixture' => 'bar'. First it creates bar instance with foo fixture config. Next iteration when it tries to create the bar fixture, it skips it since since bar named instance already exists.

Q A
Yii version 2.0.38
PHP version 7.4.9
Operating system Debian 10 Buster
codeception/module-yii2 version 1.1.1
astwinioenergy commented 11 months ago

Any news?