Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phpunit asserting if two array are equal

Tags:

php

symfony

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

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. I am using Symfony2 and Phpunit

like image 875
John Avatar asked Dec 15 '22 07:12

John


1 Answers

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

you are comparing an array to an object, actually ;)

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

will do what you want

like image 155
Jojo Avatar answered Dec 17 '22 06:12

Jojo