jdog
jdog

Reputation: 2539

phpunit mock expectation failed for method name is equal

I have the following subject to test:

class ReportTable_Renderer_Html_Decorator_AddRecord extends ReportTable_Renderer_Html_Decorator_CallParent
{
    public function renderAddItem(ReportTable $table)
    {
        $newRow = array();

        $masterIDColumn = $this->getMasterIDColumn();
        if (!empty($masterIDColumn)) {
            $newRow[$masterIDColumn] = $this->getOwner()->getMasterID();
        }

        foreach ($table->getColumns() as $name => $column) {
            $newRow[$name] = '';
        }
        $newRow['id']               = '0';
        if (!empty($newRow[$masterIDColumn])) $newRow['id'] .= '_' . $newRow[$masterIDColumn];
        $newRow[$this->getColumn()] = $this->getText();

        $this->getRowStyle()->getGroupStyles()->add('do_not_print grey');

        return $this->getParent()->renderRowContent($table, $newRow);
    }

and also this (indirect) parent class, whose functions I need to stub for the test

class ReportTable_Renderer_Html_Decorator_Base extends ReportTable_Renderer_Html
{
    public function renderRowContent(ReportTable $table, array $row) {}
    public function renderRowSetFooter(ReportTable $table) {}
}

My test:

public function testRenderRowSetFooter()
{
    $table = new ReportTable('a','b');
    $table->addColumn( new ReportTable_Column( 'one', 'one' ));
    $table->addColumn( new ReportTable_Column( 'two', 'two' ));
    $table->addColumn( new ReportTable_Column( 'three', 'three' ));

    $testText = 'test text';
    $parentFooterText = 'parent.parent';
    $groupID = 234;
    $addText = 'Add me. Add me now!';
    $newRow = array('one' => $addText, 'two' => $groupID, 'three' => '', 'id' => 0 );
    $parent = $this->getMock('ReportTable_Renderer_Html_Base', array( 'renderRowContent', 'renderRowSetFooter' ));
    $parent->expects($this->any())->method('renderRowContent')->with($table, $newRow)->will($this->returnValue($testText));
    $parent->expects($this->any())->method('renderRowSetFooter')->with($table)->will($this->returnValue($parentFooterText));
    $subject = $this->getSubject($parent, array( 'text' => $addText, 'column' => 'one', 'masterIDColumn' => 'two' ));
    $subject->getOwner()->setMasterID($groupID);
    $this->assertEquals($parentFooterText . $testText, $subject->renderRowSetFooter($table));
}

I'm stuck with this error message which happens for both mocked functions:

PHPUnit_Framework_ExpectationFailedException : Expectation failed for method name is     equal to <string:renderRowContent> when invoked zero or more times
Parameter 1 for invocation     Herkt_ReportTable_Renderer_Html_Base::renderRowContent(Herkt_ReportTable Object (...), Array     (...)) does not match expected value.
Failed asserting that two arrays are equal.

One of the arrays shows is $newRow, the other one obviously the resut of the function. But I did not add an assertEquals for these arrays? How does this come about and how can I fix my test?

Upvotes: 2

Views: 9720

Answers (1)

jdog
jdog

Reputation: 2539

Ok, figured it out. I inherited this test and am adapting it to changed functionality. What happens is that because of the mock functions, the actual testing happens by passing $newRow into the mock function renderRowContent

My test failed because I didn't adapt the expected parameters to the change in my tested function

it should be:

$newRow = array(
            'masterColumn' => $groupID, 
            'one' => $addText, 
            'two' => '', 
            'three' => '', 
            'id' => '0_234'
          );

Upvotes: 4

Related Questions