使用PHPunit测试代码时遇到麻烦

Sushant Bajracharya

我正在使用PHPUnit测试代码,我们如何测试该函数是否在循环内从同一类中调用另一个函数?这是我的dashboardmanager.php

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]);
    }
吉恩

您要问的是使用模拟对象完成的。模拟对象是您想要的类的伪造版本,它仅在调用函数时才看起来像您需要测试的样子。

有很多模拟库,或者您可以直接使用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();
    }
}
?>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用指针时遇到麻烦

来自分类Dev

使用Javascript要求AWS时遇到麻烦

来自分类Dev

在getElementById内使用变量时遇到麻烦

来自分类Dev

使用多个对象时遇到麻烦

来自分类Dev

使用fmod设置setMusicSpeed()时遇到麻烦

来自分类Dev

在使用javascript sort()命令时遇到麻烦

来自分类Dev

Laravel PHP:使用nest()时遇到麻烦

来自分类Dev

使用Jsoup登录网站时遇到麻烦,

来自分类Dev

我在使用BaseAdapter时遇到麻烦

来自分类Dev

我在理解一段代码中使用的语法时遇到麻烦

来自分类Dev

在Laravel中使用PHPUnit测试HTTP动词时遇到问题

来自分类Dev

尝试构建opengl精美示例代码时遇到麻烦

来自分类Dev

Python:在编写类代码时遇到麻烦

来自分类Dev

从源代码编译Linux内核时遇到麻烦

来自分类Dev

分页时遇到麻烦

来自分类Dev

使用Github遇到麻烦

来自分类Dev

使用XPATH遇到麻烦

来自分类Dev

在使用解析时使用nil可选遇到麻烦

来自分类Dev

初学者级。用C编写代码。遇到“ if else”时遇到麻烦。

来自分类Dev

Tilde〜在Mac上使用fstream时遇到麻烦

来自分类Dev

使用Swift初始化属性时遇到麻烦

来自分类Dev

使用多索引处理日期索引时遇到麻烦

来自分类Dev

使用ActionListener更改变量时遇到麻烦

来自分类Dev

使用Crypto ++解密格式正确的密文时遇到麻烦

来自分类Dev

使用类型变量显式指定类型时遇到麻烦

来自分类Dev

我在使用doGet和doPost方法时遇到麻烦

来自分类Dev

在使用umano android slideuppanel 2时遇到麻烦

来自分类Dev

使用div创建盒子阴影类型效果时遇到麻烦

来自分类Dev

使用lm函数回归时遇到麻烦