Using
- phalcon(3.4)
- phalcon/incubator(3.4)
- phpunit/phpunit(6.5.8)
- phpunit/dbunit(3.0.3)
How to do unit testing of related tables?
in my database, tableB belongs to tableA. and tableC belongs to tableB Like This
class TableA extends \Base\Model
{
public $id:
.
.
.
public function initialize()
{
parent::initialize();
$this->belongsTo('TableB', '\TableB', 'id', ['alias' => 'TableB']);
}
and
class TableB extends \Base\Model
{
public $id:
.
.
.
public function initialize()
{
parent::initialize();
$this->belongsTo('TableC', '\TableC', 'id', ['alias' => 'TableC']);
}
Code to be tested
I wanna create unit test which another func is called.
public function TestAction()
{
$testA = \TestA::findFirst("id = {$id}");
$testB = $TestA->TestB;
.
.
.
if ($testB->save()) {
.
.
.
$testA->update();
// execute another func(private function)
$this->anotherFunc(
$testB->testC->toArray(),
$testB->toArray(),
$params
);
}
}
}
Unit Test
class TestControllerTest extends \UnitTestCase
{
protected function getDataSet()
{
return new YamlDataSet(../TestControllerTestDataSet.yml");
}
public function testsSampleAction()
{
$mockTestController = new \App\Controllers\TestController;
// mock testA model
$testAMock = Mockery::mock('overload:\testA);
// define data that findFirst function return
$testAData = $getDataSet->getTable("testA")->getRow(0);
// define testB data that TestA related
$testBData = $getDataSet->getTable("testB")->getRow(0);
// define testC data that testB related
$testCData = $getDataSet->getTable("testC")->getRow(0);
// array_merge(testB and testC)
$testAData = array_merge($testBData, array("testC" => $testCData));
$testAData = array_merge($testAData, array("testB" => $testBData));
// change from array to object
$data = (object) $testAData;
$testA->shouldReceive("findFirst")
->andReturn($testAData);
$testA->testB = new class {
public function save()
{
return true;
}
};
$mockTestController->registerAction();
$mockTestController->expects($this->once())->method('anotherFunc');
}
current status
The details of the error are here.. I do not know what and how to complete the unit test. I ask for support.
Error: Call to a member function save() on array