개발! 딱 깔끔하고 센스있게!

PHP 유닛 을 사용 하여 배열 의 값 이 포함 되 는 지 시험 합 니 다. 본문

개발 스크랩 메모/PHP

PHP 유닛 을 사용 하여 배열 의 값 이 포함 되 는 지 시험 합 니 다.

렉사이 2020. 11. 22. 00:07

내 가 이 개체 배열 을 만 들 었 습 니 다:

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

저 는 배열 에 함수 로부터 얻 은 랜 덤 광고 가 있 는 지 확인 하고 싶 습 니 다.

다음 과 같이 광고 가 져 오기:

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

지금 나 는 몇 팀 이 내 가 받 은 랜 덤 광 고 를 포함 하고 있 는 지 를 검사 하고 싶다.

나 는 이렇게 할 수 있다.

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

그런데 내 문 제 는 결론 이나 다른 방법 이 나 보다 더 잘 하 는 것 같 지 않 아?제 가 assertArray HasKey 를 사용 하려 고 시 도 했 지만 다음 과 같은 오류 가 발생 했 습 니 다.

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

그 거 알 아?감사합니다.

대답

assertcontents 방법 을 시도 합 니 다:

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