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

Bug with assertArraySubset() #3319

Closed
KartaviK opened this issue Oct 2, 2018 · 1 comment
Closed

Bug with assertArraySubset() #3319

KartaviK opened this issue Oct 2, 2018 · 1 comment

Comments

@KartaviK
Copy link

KartaviK commented Oct 2, 2018

Q A
PHPUnit version 7.3.5
PHP version 7.2
Installation Method Composer

I use Yii2 framework.
I try to assert array with rules for model but i catch an unhandled situation.
I have an abstract class and another class that extends it. I merged rules from abstract class and extended

This is how functions look like:

...
    public function rules(): array
    {
        return [
            [
                ['token', 'input',],
                'required',
            ],
            [
                ['token', 'input', 'language'],
                'string',
            ],
            ['language', 'string', 'length' => 2,],
            [
                'language',
                'default',
                'value' => function (): string {
                    return \Yii::$app->language;
                },
            ],
        ];
    }
...

...
   //
    public function rules(): array
    {
        return array_merge(parent::rules(), [
            [
                ['type', 'city',],
                'string'
            ],
            ['type', 'filter', 'filter' => 'preg_quote'],
        ]);
    }
...

And how tests look like:

...
    /** @var Model */
    protected $fakeModel;

    protected function setUp(): void
    {
        $this->fakeModel = Model::instance();
    }

    public function testRulesArray(): void
    {
        $expectRules = [
            [
                ['token', 'input',],
                'required',
            ],
            [
                ['token', 'input', 'language'],
                'string',
            ],
            ['language', 'string', 'length' => 2,],
            [
                'language',
                'default',
                'value' => function (): string {
                    return \Yii::$app->language;
                },
            ],
            [
                ['type', 'city',],
                'string'
            ],
            ['type', 'filter', 'filter' => 'preg_quote'],
        ];

        $this->assertArraySubset($expectRules, $this->fakeModel->rules());
    }
...

And finally here is exception:

Phpunit say me that asserting subset arrays is failing:

Failed asserting that an array has the subset Array &0 (
    0 => Array &1 (
        0 => Array &2 (
            0 => 'token'
            1 => 'input'
        )
        1 => 'required'
    )
    1 => Array &3 (
        0 => Array &4 (
            0 => 'token'
            1 => 'input'
            2 => 'language'
        )
        1 => 'string'
    )
    2 => Array &5 (
        0 => 'language'
        1 => 'string'
        'length' => 2
    )
    3 => Array &6 (
        0 => 'language'
        1 => 'default'
        'value' => Closure Object &00000000615f6b9e000000004dec992d (
            0 => Closure Object &00000000615f6b9e000000004dec992d
        )
    )
    4 => Array &7 (
        0 => Array &8 (
            0 => 'type'
            1 => 'city'
        )
        1 => 'string'
    )
    5 => Array &9 (
        0 => 'type'
        1 => 'filter'
        'filter' => 'preg_quote'
    )
). <Click to see difference>

But! When i click "see difference" phpstorm say that arrays is equal

excepted:

array (
  0 => 
  array (
    0 => 
    array (
      0 => 'token',
      1 => 'input',
    ),
    1 => 'required',
  ),
  1 => 
  array (
    0 => 
    array (
      0 => 'token',
      1 => 'input',
      2 => 'language',
    ),
    1 => 'string',
  ),
  2 => 
  array (
    0 => 'language',
    1 => 'string',
    'length' => 2,
  ),
  3 => 
  array (
    0 => 'language',
    1 => 'default',
    'value' => 
    Closure::__set_state(array(
    )),
  ),
  4 => 
  array (
    0 => 
    array (
      0 => 'type',
      1 => 'city',
    ),
    1 => 'string',
  ),
  5 => 
  array (
    0 => 'type',
    1 => 'filter',
    'filter' => 'preg_quote',
  ),
)

actual:

array (
  0 => 
  array (
    0 => 
    array (
      0 => 'token',
      1 => 'input',
    ),
    1 => 'required',
  ),
  1 => 
  array (
    0 => 
    array (
      0 => 'token',
      1 => 'input',
      2 => 'language',
    ),
    1 => 'string',
  ),
  2 => 
  array (
    0 => 'language',
    1 => 'string',
    'length' => 2,
  ),
  3 => 
  array (
    0 => 'language',
    1 => 'default',
    'value' => 
    Closure::__set_state(array(
    )),
  ),
  4 => 
  array (
    0 => 
    array (
      0 => 'type',
      1 => 'city',
    ),
    1 => 'string',
  ),
  5 => 
  array (
    0 => 'type',
    1 => 'filter',
    'filter' => 'preg_quote',
  ),
)

And finally, if i use assertEquals(...) test will be passed:

    public function testRulesArray(): void
    {
        $expectRules = [
            [
                ['token', 'input',],
                'required',
            ],
            [
                ['token', 'input', 'language'],
                'string',
            ],
            ['language', 'string', 'length' => 2,],
            [
                'language',
                'default',
                'value' => function (): string {
                    return \Yii::$app->language;
                },
            ],
            [
                ['type', 'city',],
                'string'
            ],
            ['type', 'filter', 'filter' => 'preg_quote'],
        ];

        $this->assertArraySubset($expectRules, $this->fakeStreetsPanel->rules());
    }

console output:

Testing started at 12:01 ...
/usr/bin/php /home/kartavik/projects/yii2-google-autocomplete/vendor/phpunit/phpunit/phpunit --configuration /home/kartavik/projects/yii2-google-autocomplete/phpunit.xml --filter "/(::testRulesArray)( .*)?$/" Wearesho\GoogleAutocomplete\Yii\Tests\Unit\Panels\StreetsPanelTest /home/kartavik/projects/yii2-google-autocomplete/tests/Unit/Panels/StreetsPanelTest.php --teamcity
PHPUnit 7.3.5 by Sebastian Bergmann and contributors.
@KartaviK
Copy link
Author

This issue is no longer relevant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant