2015-07-26 16 views
26

Ho creato questo array di oggetti:test se matrice contiene valore utilizzando PHPUnit

$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); 

e voglio verificare che un annuncio a caso ho ottenuto da una funzione esiste nella matrice. il codice per ottenere l'annuncio appare così:

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

Ora voglio verificare se la matrice contiene l'annuncio a caso ho ricevuto, quello che ero in grado di fare in questo modo:

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

ma il mio la domanda è, c'è un assert o qualche altro modo per controllare questa cosa meglio di come l'ho fatta ?? Ho provato ad utilizzare assertArrayHasKey ma ho ottenuto il seguente errore:

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

qualche idea per favore? thx

+1

'$ this-> assertEquals ((int) in_array ($ randad, $ adUnitArr), 1); // controlla se un annuncio nella matrice ' –

+0

' $ this-> assertTrue (in_array ($ randad, $ adUnitArr)); ' – vivoconunxino

risposta

48

provare il metodo assertContains:

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

Attenzione ai' bool'. '$ this-> assertContains ('foo', ['abc' => true]);' sarà vero. – GeekMagus

Problemi correlati