9

I'm writing some unit tests using phpunit and I now want to assert that an array contains a certain value, but the only function I can find is assertArrayHasKey(). There is no assertArrayHasValue whatsoever.

So to be clear I want something like this:

$a = [5, 8, 16];
assertArrayHasValue(8, $a);

All tips are welcome!

2

2 Answers 2

12

You can do it with function assertContains() of PHPUnit:

try this:

$a = [5, 8, 16];
$this->assertContains(8, $a);

ASSERT_CONTAINS

Sign up to request clarification or add additional context in comments.

Comments

2

You can use ouzo goodies library.

 $animals = ['cat', 'dog', 'pig'];
 Assert::thatArray($animals)->hasSize(3)->contains('cat');

There are a lot of interesting assertions that can be chained e.g.

Assert::thatArray($users)->onMethod('getAge')->containsOnly(35, 24);

See http://ouzo.readthedocs.org/en/latest/documentation/tests.html#array-assertions

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.