Demolition Man: Deleting Data

Ok, we read data, we changed data. That’s enough for the constructive way. It’s time to destroy something. The strategy is quite simple: Create data, retrieve it from the database and destroy it, but now, of course, it is very important to check the database, if the data actually was deleted. The code is straight forward:


public function canDeleteAppointmentFromRepository() {
    $testId = 42;
    $uid = $this->setupTestData($testId);
    $appointment = $this->appointmentRepository->findByUid($uid);
    $this->appointmentRepository->remove($appointment);
    unset($appointment);
    $this->persistenceManager->persistAll();
    $appointment = $this->appointmentRepository->findByUid($uid);
    $this->assertNull($appointment, 'Appointment is not NULL after removing it from the repository');
}

Those who are very familiar to software engineering and British SF might find the answer to all questions in line #2. So you don’t need to read on, because this says it all. 🙂

One special thing to remark here is on line #7. In the lines above I just created and removed the Entity Object. Nevertheless the findByUid in line #8 would find the entity object in the database anyway, because it was marked as “deleted” in the repository’s cache, but was not physically removed from the DB. Thus we have to tell the Persistence Manager to update the database. This is done in line #7. What we need for this is an instance of the persistence manager. We get it by a dependency injection:


/**
* @var Tx_Extbase_Persistence_ManagerInterface
*/
protected $persistenceManager;

public function injectPersistenceManager(Tx_Extbase_Persistence_ManagerInterface $persistenceManager) {
    $this->persistenceManager = $persistenceManager;
}

protected function setUp() {
    parent::setUp();
    $this->testingFramework = new Tx_Phpunit_Framework('tx_csevents');
    $this->setupConfiguration();
    $this->appointmentRepository = $this->objectManager->get(Tx_Csevents_Domain_Repository_AppointmentRepository);
    $this->injectPersistenceManager($this->objectManager->get(Tx_Extbase_Persistence_Manager));
}

The first lines are just classic dependency injection: Declare a property to store the reference to the persistence manager and define a function starting with “inject” to inject the actual instance of it.

Now you can argue, if you want to do your injection within the constructor or within the setUp() method. I chose the setUp() method, because there are 3 constructors available, I have to overwrite, but only one setUp() method I can use as a centralized point for setting up all my environment and Testing Fixture. In my opinion it’s the better solution.

Leave a Reply