Monday, 24 August 2020

PHP unit test Mock fetchAll object

can someone help me with mocking ZF1 fetchAll() method, it returns Zend_Db_Table_Rowset object

So tried to do mock in this way, here is my code.

$rowSet = $this->getMockBuilder(Zend_Db_Table_Rowset::class)
            ->disableOriginalConstructor()
            ->getMock();
and set private property tru reflaction class

$dataArray = [['id' => 1, 'name' => 'test_name'], ['id' => 2, 'name'=>'test2']];

$this->setProtectedFieldValue($rowSet, '_data', $dataArray);


protected function setProtectedFieldValue($entity, string $propertyName, $value) : void
{
    $class = new ReflectionClass($entity);
    $property = $class->getProperty($propertyName);
    $property->setAccessible(true);
    $property->setValue($entity, $value);
}

at the end in won't enter $rows set loop, i don't have idea how to loop or set data in this Zend_Db_Table_Rowset object.

$rows = $table->fetchAll($tableSelect);
      foreach ($rows as $row) {  

Small update i need something like this https://hermanradtke.com/2010/02/08/mocking-zend-frameworks-row-and-rowset-objects.html, but in my version that My_ZendDbTable_Row_TestMockRow class doesn't exist

 array(
                'first_name' => 'Herman',
                'last_name' => 'Radtke',
                'email' => 'herman@example.com'
            )
        );

        $row = new My_ZendDbTable_Row_TestMockRow($data);
        $this->assertEquals('Herman', $row->first_name);
        $this->assertEquals('Radtke', $row->last_name);
        $this->assertEquals('herman@example.com', $row->email);
    }
}


from PHP unit test Mock fetchAll object

No comments:

Post a Comment