test if array contains value using PHPUnit

PhpPhpunit

Php Problem Overview


I created this array of objects:

$ad_1 = new AdUnit(array('id' => '1', 'name' => 'Ad_1', 'description' => 'great ad', 'code' => 'alpha', 'widget_id' => '123'));
$ad_2 = new AdUnit(array('id' => '2', 'name' => 'Ad_2', 'description' => 'good ad', 'code' => 'delta', 'widget_id' => '456'));
$ad_3 = new AdUnit(array('id' => '3', 'name' => 'Ad_3', 'description' => 'bad ad', 'code' => 'sigma', 'widget_id' => '789'));
$adUnitArr = array($ad_1, $ad_2, $ad_3);

and i want to check that a random ad i got from a function exists in the array. the code to get the ad looks like this:

		$fixture = new AdGroup();
$fixture->setAds($adUnitArr);
$randad = $fixture->getRandomAd();

now i want to check if the array contains the random ad i received, what i was able to do like this:

$this->assertEquals(in_array($randad, $adUnitArr), 1); //check if ad in array

but my question is, is there an assert or some other way to check this thing better than the way i did it?? i tried using assertArrayHasKey but i got the following error:

PHPUnit_Framework_Exception: Argument #1 (No Value) of PHPUnit_Framework_Assert::assertArrayHasKey() must be a integer or string

any idea please? thx

Php Solutions


Solution 1 - Php

Try the assertContains method:

$this->assertContains( $randad, $adUnitArr );

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDonoven RallyView Question on Stackoverflow
Solution 1 - Phpuser2182349View Answer on Stackoverflow