Strong validation check PHPUnit assertEquals

My goal is to ensure that the graphic has the expected values โ€‹โ€‹and types. I want each value to have the expected type.

For this purpose assertEquals(), unfortunately, it will not be useful:

$this->assertEquals(
    [ 'prop' => '0' ],
    [ 'prop' => 0 ]
);
// -> no failures

In this case, it assertSame()works beautifully:

$this->assertSame(
    [ 'prop' => '0' ],
    [ 'prop' => 0 ]
);
// Failed asserting that Array &0 (
//     'prop' => 0
// ) is identical to Array &0 (
//     'prop' => '0'
// ).

The problem with this assertSame()is that it also checks the link for objects:

$this->assertSame(
    (object) [ 'prop' => 0 ],
    (object) [ 'prop' => 0 ]
);
// Failed asserting that two variables reference the same object.

What options do I have?


In a separate note, Iโ€™m not sure why it was designed in this way - it seems to me that it assertSame()does two things at once (I have no more tested class of objects, not links).

+4
source share
3 answers

So far I have come up with the following option:

/**
 * @param mixed $expected
 * @param mixed $actual
 * @param string $message
 */
public function assertObjectGraph($expected, $actual, $message = '')
{
    $expected = $this->convertObjectsToHashes($expected);
    $actual = $this->convertObjectsToHashes($actual);

    $this->assertSame($expected, $actual, $message);
}

/**
 * @param mixed $value
 * @return mixed
 */
private function convertObjectsToHashes($value)
{
    if (is_object($value)) {
        $value = ['__CLASS__' => get_class($value)] + get_object_vars($value);
    }

    if (is_array($value)) {
        $value = array_map([$this, __FUNCTION__], $value);
    }

    return $value;
}

:

$this->assertObjectGraph(
    (object) [ 'prop' => 0 ],
    (object) [ 'prop' => 0 ]
);
// -> ok

$this->assertObjectGraph(
    (object) [ 'prop' => 0 ],
    (object) [ 'prop' => '0' ],
);
// Failed asserting that Array &0 (
//     '__CLASS__' => 'stdClass'
//     'prop' => '0'
// ) is identical to Array &0 (
//     '__CLASS__' => 'stdClass'
//     'prop' => 0
// ).

class Test{public $prop = 0;}
$this->assertObjectGraph(
    (object) [ 'prop' => 0 ],
    new Test()
);
// Failed asserting that Array &0 (
//     '__CLASS__' => 'Test'
//     'prop' => 0
// ) is identical to Array &0 (
//     '__CLASS__' => 'stdClass'
//     'prop' => 0
// ).
0

, , :

    $this->assertArrayHasKey('prop', $input);
    $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_INT, $input['prop']);
    // or more simply as this->assertTrue(is_int($new_id));
    $this->assertEquals(0, $input['prop']);

,

0

, 2 ,
, PHP - Array Operators - ===

$ a === $ b , $ a $ b / .// -

, , :

$this->assertTrue(['a' => '1', 'b' => '2'] === ['a' => 1, 'b' => '2']);

assertEquals() , , , == - equal, , , ,

$this->assertEquals(321, true); // this would PASS!

For objects, you have to define a whole separate comparator.

0
source

Source: https://habr.com/ru/post/1650901/


All Articles