2015-04-20 7 views
1

Я тестирую коды с помощью PHPUnit. Как мы проверяем, вызывает ли функция другую функцию из того же класса внутри цикла? Это мой dashboardmanager.phpПроблемы с тестированием кода с использованием PHPunit

public function getZoneOrderValue($businessUnitId, $fromDate, $toDate) 
    { 
     $repository = $this->getRepository(); 
     $query = $repository->getZoneCount($businessUnitId, $fromDate, $toDate); 
     $results = $this->type->search($query)->getAggregations('by_zone'); 

     $results = $results['by_zone']['buckets']; 
     $i = 0; 
     foreach ($results as $result) { 
      $zoneOrderValue[$i] = array(); 
      $zoneValue = $result['order_value']['value']; 
      $zoneId = $result['key']; 
      $zoneName = $this->getZoneName($zoneId); 
      array_push($zoneOrderValue[$i], $zoneName, $zoneValue); 
      $i++; 
     } 

     return $zoneOrderValue; 
    } 

public function getZoneName($zoneId) 
    { 
     $repository = $this->getRepository(); 
     $query = $repository->getZoneName($zoneId); 
     $zone = $this->type->search($query)->getResults(); 
     $zone = $zone[0]->getFields(); 
     $zoneName = $zone['zone'][0]; 

     return $zoneName; 
    } 

Это мой тестовый файл. У меня возникли проблемы в test_getZoneOrderValue(). Это мой dashboardmanagertest.php

protected $dash; 
    protected $ob; 
    protected $container; 
    protected $prophet; 
    protected $elasticManager; 
    protected $elasticIndexManager; 
    protected $repoManager; 
    protected $resultSet; 
    protected $type; 

    public function setup() 
    { 
     $this->prophet = new \Prophecy\Prophet(); 
     $this->ob = $this->prophet->prophesize('Doctrine\Common\Persistence\ObjectManager'); 
     $this->container = $this->prophet->prophesize('Symfony\Component\DependencyInjection\Container'); 
     $this->elasticManager = $this->prophet->prophesize('FOS\ElasticaBundle\Doctrine\RepositoryManager'); 
     $this->elasticIndexManager = $this->prophet->prophesize('Elastica\Type'); 
     $this->repoManager = $this->prophet->prophesize('UDN\Bundle\DataTransferBundle\Entity\SearchRepository\OrderReportRepository'); 
     $this->resultSet = $this->prophet->prophesize('Elastica\ResultSet'); 
     $this->type = $this->createIndex(); 
     $this->type = $this->prophet->prophesize('Elastica\Type'); 

     $type = $this->createIndex(); 


     $index = $this->createIndex(); 

     $this->dash = new DashBoardManager($this->ob->reveal(), $this->container->reveal(), $this->type->reveal()); 



    } 



    public function test_getZoneOrderValue() 
    { 
     $queryResult = array('by_zone' => array('buckets' => array(array('key_as_string' => '','key' => '','doc_count' => '','order_value' => array('value' => ''))))); 

     $this->container->get('fos_elastica.manager')->willReturn($this->elasticManager->reveal()); 
     $this->elasticManager->getRepository('UDNDataTransferBundle:OrderReport')->willReturn($this->repoManager->reveal()); 

     $this->repoManager->getZoneCount(2, '2015-02-22', '2015-02-22')->willReturn([]); 

     $this->container->get('fos_elastica.index.rosia_test.orders_test')->willReturn($this->type->reveal()); 

     $this->type->search([])->willReturn($this->resultSet->reveal()); 
     $this->resultSet->getAggregations('by_zone')->willReturn($queryResult); 

     $this 
     $this->repoManager->getZoneName(2)->willReturn([]); 

     $result = $this->dash->getZoneOrderValue(2, '2015-02-22', '2015-02-22'); 

     //$this->assertCount(2, $result[0]); 
    } 

ответ

-1

Что вы спрашиваете, это делается с помощью фиктивного объекта. Макет-объект - это поддельная версия вашего предполагаемого класса, которая выглядит только в том случае, если функция вызывается так, как вам нужно протестировать.

Есть много насмешливых библиотек, или вы можете сделать это напрямую с помощью PHPUnit. Это пример, взятый из PHPUnit docs:

<?php 
class SubjectTest extends PHPUnit_Framework_TestCase 
{ 
    public function testObserversAreUpdated() 
    { 
     // Create a mock for the Observer class, 
     // only mock the update() method. 
     $observer = $this->getMockBuilder('Observer') 
         ->setMethods(array('update')) 
         ->getMock(); 

     // Set up the expectation for the update() method 
     // to be called only once and with the string 'something' 
     // as its parameter. 
     $observer->expects($this->once()) 
       ->method('update') 
       ->with($this->equalTo('something')); 

     // Create a Subject object and attach the mocked 
     // Observer object to it. 
     $subject = new Subject('My subject'); 
     $subject->attach($observer); 

     // Call the doSomething() method on the $subject object 
     // which we expect to call the mocked Observer object's 
     // update() method with the string 'something'. 
     $subject->doSomething(); 
    } 
} 
?> 
+0

Если честно, этот ответ походит на 'go read the docs', не отвечая на вопрос. –

+0

@AlexanderGuz, потому что это базовое тестирование, и ответ хорошо освещен в документах. – Jean