I am working on my first extension that uses TDD principles and I am having trouble figuring out how best to write this particular test. In the module's database table, I have a column called parent_id which references the table's entity_id column. My goal is to have this type of parent-child relationship: grandparent > parent > child > grand-child > etc. To test this, I need to create some sort of dataset that includes the parent and child records. I am not sure if I need to use a fixture, expectation, or something else for this. I also need to know how to assert that the relationship was defined correctly. Can anybody give me some recommendations?


I think I have a plan of attack for this and I will post the answer later if my idea works. I think if I create a fixture with the parent record's data, a provider with the child record, and an expectation with the relationship I may be able to use assertSame() to test it.


As promised, here is the solution to my problem (extra unnecessary fields and code have been excluded for clarity):

The Fixture:

        - id: 1
          parent_id: 0
          name: Parent
          created: 0000-00-00 00:00:00

The Provider:

    - parent_child_relationship
    - id: 2
      parent_id: 1
      name: Child
      created: 0000-00-00 00:00:00

The Expectation:

        id: 1
        parent_id: 0
        name: Parent
        created: 0000-00-00 00:00:00

The test:

 * Test Parent-Child relationship
 * @dataProvider dataProvider
 * @loadFixture
 * @param array $dataSet The expected data
 * @param array $data    The actual data
 * @return void
public function testGetParent($dataSet, $data)

    $result = $this->_model->getParent()->getData();

    $this->assertEquals($this->expected($dataSet)->getResult(), $result);

Note: I had to use assertEquals() instead of assertSame() because the integers in the resulting array are getting wrapped as strings somewhere in the process.

This solution has been overly simplified but I hope that it helps someone in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.