We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to do unit testing of related tables

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
edited Mar '19

I consider unit testing with Phalcon quite painstaking in general. When I tried to use the laravel-translatable package, though, I had minor mistakes. I should have selected the whole row instead of individual columns. Besides, there were also some mistakes while saving the translation. Perhaps, you need to check the stackoverflow questions:

Unit testing is good at checking bugs, but there’s still the possibility of missing an error, so I’m still in favor of test automation.