yii2 icon indicating copy to clipboard operation
yii2 copied to clipboard

Fixtures does't work as they should

Open zeleniy opened this issue 1 year ago • 6 comments

This is Yii version 2.0.47.
# vendor/bin/codecept run common/tests/unit/YiiFixtureTest.php
Codeception PHP Testing Framework v4.2.2 https://helpukrainewin.org
Powered by PHPUnit 8.5.33 by Sebastian Bergmann and contributors.

Common\tests.unit Tests (1) ---------------------------------------------------------------------------------------------------
E YiiFixtureTest: Empty input (0.07s)
-------------------------------------------------------------------------------------------------------------------------------


Time: 1.12 seconds, Memory: 16.00 MB

There was 1 error:

---------
1) YiiFixtureTest: Empty input
 Test  tests/unit/YiiFixtureTest.php:testEmptyInput

  [yii\base\ErrorException] array_merge(): Argument #2 is not an array

#1  /var/www/edrid/vendor/yiisoft/yii2/test/FixtureTrait.php:145
#2  /var/www/edrid/vendor/yiisoft/yii2/test/FixtureTrait.php:145
#3  /var/www/edrid/vendor/yiisoft/yii2/test/FixtureTrait.php:112
#4  /var/www/edrid/common/tests/_support/_generated/UnitTesterActions.php:56
#5  /var/www/edrid/common/tests/unit/YiiFixtureTest.php:29
#6  /var/www/edrid/vendor/symfony/console/Application.php:301
#7  /var/www/edrid/vendor/symfony/console/Application.php:171
#8  /var/www/edrid/vendor/bin/codecept:115

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
cat common/fixtures/TrademarkFixture.php
<?php


namespace common\fixtures;


use yii\test\ActiveFixture;
use tz_app\models\Trademark;


class TrademarkFixture extends ActiveFixture {

  public $modelClass = Trademark::class;
}

zeleniy avatar Apr 20 '23 14:04 zeleniy

array_merge(): Argument #2 is not an array

Which code produces the exception?

samdark avatar Apr 22 '23 10:04 samdark

Oh sorry. Here is the test:

cat common/tests/unit/YiiFixtureTest.php
<?php


namespace common\tests\unit;


use common\fixtures\TrademarkFixture;


class YiiFixtureTest extends \Codeception\Test\Unit {


  /**
   * @var \common\tests\UnitTester
   */
  protected $tester;


  public function _fixtures() {

    return [
      'trademarks' => TrademarkFixture::class,
    ];
  }


  public function testEmptyInput() {

    $this->tester->haveFixtures('trademarks');
  }
}

zeleniy avatar Apr 22 '23 13:04 zeleniy

Do you have a fixture data file?

https://github.com/yiisoft/yii2/blob/master/framework/test/FixtureTrait.php#L145

$this->_fixtures = $this->createFixtures(array_merge($this->globalFixtures(), $this->fixtures()));

Produces:

  [yii\base\ErrorException] array_merge(): Argument #2 is not an array

#1  /var/www/edrid/vendor/yiisoft/yii2/test/FixtureTrait.php:145

That means that $this->fixtures() returns non-array.

It might be that you need to rename _fixtures() into fixtures() but I can be wrong. Go by the path of the stacktrace backwards.

samdark avatar Apr 23 '23 20:04 samdark

Renaming has no sense. Here is fixture data.

ls -1R common/fixtures/
common/fixtures/:
OrderFixture.php
OrderTrademarkFixture.php
OrderTypeFixture.php
TrademarkFixture.php
data

common/fixtures/data:
r_order_trademark.php
r_orders.php
r_orders_type.php
r_trademark.php
cat common/fixtures/data/r_trademark.php
<?php

return [
  'tm1' => [
    'title' => 'title1'
  ],
  'tm2' => [
    'title' => 'title2'
  ]
];

And here is slightly update test class:

<?php


namespace common\tests\unit;


use common\fixtures\TrademarkFixture;
use Codeception\Lib\Console\Output;


class YiiFixtureTest extends \Codeception\Test\Unit {


  /**
   * @var \common\tests\UnitTester
   */
  protected $tester;


  public function _fixtures() {

    return [
      'trademarks' => TrademarkFixture::class,
    ];
  }


  public function testEmptyInput() {

    foreach ($this->tester->grabFixture('trademarks')->getIterator() as $key => $value) {
      (new Output([]))->debug([$key, $value, $this->tester->grabFixture('trademarks', 'tm1')]);
    }

    $this->tester->haveFixtures('trademarks');
  }
}

Which produce next output:

vendor/bin/codecept run common/tests/unit/YiiFixtureTest.php
Codeception PHP Testing Framework v4.2.2 https://helpukrainewin.org
Powered by PHPUnit 8.5.33 by Sebastian Bergmann and contributors.

Common\tests.unit Tests (1) ---------------------------------------------------------------------------------------------------
- YiiFixtureTest: Empty input
  Array
  (
      [0] => tm1
      [1] => Array
          (
              [title] => title1
              [id] => 1
          )

      [2] =>
  )


  Array
  (
      [0] => tm2
      [1] => Array
          (
              [title] => title2
              [id] => 2
          )

      [2] =>
  )

E YiiFixtureTest: Empty input (0.14s)
-------------------------------------------------------------------------------------------------------------------------------


Time: 1.31 seconds, Memory: 16.00 MB

There was 1 error:

---------
1) YiiFixtureTest: Empty input
 Test  tests/unit/YiiFixtureTest.php:testEmptyInput

  [yii\base\ErrorException] array_merge(): Argument #2 is not an array

#1  /var/www/edrid/vendor/yiisoft/yii2/test/FixtureTrait.php:145
#2  /var/www/edrid/vendor/yiisoft/yii2/test/FixtureTrait.php:112
#3  /var/www/edrid/common/tests/_support/_generated/UnitTesterActions.php:56
#4  /var/www/edrid/common/tests/unit/YiiFixtureTest.php:34
#5  /var/www/edrid/vendor/symfony/console/Application.php:301
#6  /var/www/edrid/vendor/symfony/console/Application.php:171
#7  /var/www/edrid/vendor/bin/codecept:115

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.

So $this->tester->grabFixture('trademarks', 'tm1') returns nothing despite that the code executed within foreach over fixture iterator.

zeleniy avatar Apr 24 '23 09:04 zeleniy

Are you sure your fixture data is loaded properly?

samdark avatar Apr 24 '23 11:04 samdark

No, but as you can see they gain an id's:

Array
  (
      [0] => tm1
      [1] => Array
          (
              [title] => title1
              [id] => 1
          )

      [2] =>
  )

zeleniy avatar Apr 24 '23 12:04 zeleniy