简体   繁体   English

Phpunit声明两个数组是否相等

[英]Phpunit asserting if two array are equal

I am writing a Test for my class but I am getting this error: 我正在为我的班级写一个测试,但是我收到了这个错误:

PHPUnit_Framework_MockObject_Builder_InvocationMocker Object (...) does not match expected type "array".

My Test Class: 我的测试班:

    <?php

namespace User\UserBundle\Tests\Dto\Template;

use User\UserBundle\Dto\Template\GenerateReportsTemplate;
use User\UserBundle\Doctrine\DatabaseRepository;
use User\UserBundle\Validation\ValidationClass;

class GenerateReportsTemplateTest extends \PHPUnit_Framework_TestCase
{

    public static $reportData = array
    (
        'rowid' => '',
        'emailaddress' => '',
        'firstname' => '',
        'surname' => '',
        'contact_number' => '',
    );

    protected $object;

    public $validate;

    public $db;

    protected function setUp()
    {

        $this->validate = $this->getMockBuilder('User\UserBundle\Validation\ValidationClass')
            ->disableOriginalConstructor()
            ->getMock();

        $this->db = $this->getMockBuilder('User\UserBundle\UFODoctrine\DatabaseRepository')
            ->disableOriginalConstructor()
            ->getMock();

        $this->object = $this->createGenerateReportsTemplateInstance();
    }

    public function createGenerateReportsTemplateInstance()
    {
        return new GenerateReportsTemplate
        (
            $this->validate,
            $this->db
        );
    }

    public function reportTestData()
    {

    }

    public function testGenerateReports()
    {

        $data = array
        (
            'rowid' => '',
            'emailaddress' => '',
            'firstname' => '',
            'surname' => '',
            'contact_number' => '',
        );

        $report = $this->db->expects($this->any())
                    ->method('getReportData')
                    ->will($this->returnValue(self::$reportData));

        $this->assertEquals($data, $report);

    }

}

What I want to check if the sample data equals to the data returned by the stub method getReportData 我想检查样本数据是否等于存根方法getReportData返回的数据

I belief the stub returns object and i want to compare it against a array, if thats the case how can I Assert the two...? 我相信存根返回对象,我想将它与数组进行比较,如果是这样,我怎么能断言这两个...?

Ps. PS。 I am using Symfony2 and Phpunit 我正在使用Symfony2和Phpunit

$this->assertEquals($data, $report);

you are comparing an array to an object, actually ;) 正在将一个数组与一个对象进行比较,实际上;)

$this->assertEquals($data, $this->object->getReportData());

will do what you want 会做你想做的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM