0

How can we assert some of the array's property values which contain the expected object values?

My code below is working okay, but it checks all array property values. I want to ask if there's a way we can check only some of it.


$dataToBeTested = [
    'name' => 'Johnny',
    'address' => 'Somewhere',
    'age' => 21,
    'card_no' => 13331577121,
    'rep_no' => 441546661,
    'status' => 'in-progress',
    'created_at' => '2022-07-31T10:05:27.011000Z',
    'updated_at' => '2022-07-31T10:05:27.011000Z',
];

$expectedPropValue = [
    'name' => 'Johnny',
    'address' => 'Somewhere',
    'age' => 21,
];

as expected it will return fail, since expectedPropValue has some missing properties.

$this->assertEquals($dataToBeTested, $expectedPropValue);

Goal is something like this,

$this->assertSomeOfIt($dataToBeTested, $expectedPropValue); // return true

1 Answers1

0

You are looking for assertArraySubset, Which it's been deprecated with the newer version of PHPUnit. As mentioned here https://github.com/sebastianbergmann/phpunit/issues/3494. But you can always introduce your approach and use it for your project. Or you can also try this package https://packagist.org/packages/dms/phpunit-arraysubset-asserts. which gives you what you need

self::assertArraySubset($expectedSubset, $content, true);
Mohammad.Kaab
  • 1,025
  • 7
  • 20