DB-Test: Read

It’s time to read the first data from the database. Let’s develop a repository and retrieve the created test data from it after configuration is in place. The repository is just standard (i.e. an empty child of Tx_Extbase_Persistence_Repository.

The (empty) Repository ClassNow, if the data mapper configuration is fine and the entity object does it’s job, everything should be alright. Of course I just defined a “read from the database” testcase first:


public function canReadAppointmentFromRepository() {
    $testId = 1876;
    $uid = $this->setupTestData($testId);

    $appointmentsQueryResult = $this->appointmentRepository->findAll();
    $this->assertNotNull($appointmentsQueryResult, 'appointmentsQueryResult is not set');
    $this->assertInstanceOf(Tx_Extbase_Persistence_QueryResultInterface, $appointmentsQueryResult, 'appointmentsQueryResult is of wrong type');

    $appointment = $this->appointmentRepository->findByUid($uid);
    $this->assertNotNull($appointment);
    $this->assertInstanceOf(Tx_Csevents_Domain_Model_Appointment, $appointment);

    $this->assertEquals('Test Title #' . $testId, $appointment->getInternalId(), 'internalId does not match');
    $this->assertEquals('List as #' . $testId, $appointment->getListAs(), 'listAs does not match');
    $this->assertEquals($appointment->getEventStartTime() - 15*60, $appointment->getEventLetInTime(), 'letInTime does not match to startTime');
    $this->assertEquals($appointment->getEventStartTime() + 90*60, $appointment->getEventEndTime(), 'startTime does not match to endTime');
    $this->assertEquals(70, $appointment->getNumOfSeatsAvailable(), 'numOfSeatsAvailable does not match');
    $this->assertFalse($appointment->getIsSoldOut(), 'isSoldOut does not match');

    $this->markTestIncomplete('Repository is not completly tested yet. Link to Description is missing.');
}

So in the first place I create a DB test record and afterwards I just re-read it from the database and compare the entity object’s content with the data that was written to the database in the beginning.

You can of course use another approach by creating the database content through an newly created entity object that’s stored to the database by the repository itself. This means testing the writing (i.e. actually creating) and reading back in one step. I didn’t choose this way, because a) it would mix up two tests and b) I wouldn’t be sure if the data really comes from the database and not from an internal cache of the repository. To prevent b) I wanted to use physically existing data from the database and to ensure this without having a prepared set of test data stored in my database, I just created it on the fly using the phpunit testing framework.

Leave a Reply