Wojtek O | 29 Sep 2011 07:26
Picon
Gravatar

assertIsSubset

Hi chaps,
I've just forked phpunit and added new assert for checking if a
$expected value is a subset of $actual.
Works with objects and arrays.

Make sense if you want to test you code but don't want to write 10
lines of assertEquals for individual fields.

example usage
{{{
$expected = array(
 'id' => 7,
 'nodes' => array(
  array('id' => 3),
  array('id' => 12),
 )
);
$actual = $myAPI->myCall();

$this->assertIsSubset($expected, $actual);
}}}

Whole idea is that your API might change over time (new fields in
response, ...) but this particular test doesn't care about whole
response. It's just checking if $actual variable meets the minimum
requirements.

What do you think? Can you see it useful?
I would love to hear some feedback.

(Continue reading)

Ben Selby | 29 Sep 2011 07:55
Picon
Gravatar

Re: assertIsSubset

Wojtek

Is that not the same as assertContains?

http://www.phpunit.de/manual/3.5/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.assertions.assertContains

Kind Regards
Ben

Sent from my iPhone

On 29 Sep 2011, at 06:26, Wojtek O <wojtek.oledzki <at> gmail.com> wrote:

> Hi chaps,
> I've just forked phpunit and added new assert for checking if a
> $expected value is a subset of $actual.
> Works with objects and arrays.
> 
> Make sense if you want to test you code but don't want to write 10
> lines of assertEquals for individual fields.
> 
> example usage
> {{{
> $expected = array(
> 'id' => 7,
> 'nodes' => array(
>  array('id' => 3),
>  array('id' => 12),
> )
> );
(Continue reading)

Wojtek O | 29 Sep 2011 08:30
Picon
Gravatar

Re: assertIsSubset

2011/9/29 Ben Selby <benmatselby <at> gmail.com>:
> Is that not the same as assertContains?

Assert contains doesn't work for nested arrays / objects

It's for tests like
{{{
$a = array(1,2,3);
$this->assertContains($a, 1);
}}}

$haystack needs to be array or instance of Traversable.
It might be worth extending assertContains instead, but It think
that's a different assertion.

assertIsSubset takes nested arrays/objects. So you can assert
{{{
$e = array(1, 'sub' => array(3));
$a = array(1, 2, 'sub' => array(3, 4, 5));
$this->assertIsSubset($e, $a);
}}}
..and more complicated arrays/objects

--

-- 
Wojtek Oledzki
hoborglabs.com


Gmane