public function deleteEndpoint(\EndpointLocation $endpoint, \User $user) { require_once __DIR__ . '/../DAOs/ServiceDAO.php'; //Check the portal is not in read only mode, throws exception if it is $this->checkPortalIsNotReadOnlyOrUserIsAdmin($user); $service = $endpoint->getService(); // check user has permission to edit endpoint's service $this->validateAddEditDeleteActions($user, $service); $this->em->getConnection()->beginTransaction(); try { $serviceDAO = new \ServiceDAO(); $serviceDAO->setEntityManager($this->em); $serviceDAO->removeEndpoint($endpoint); $this->em->flush(); $this->em->getConnection()->commit(); } catch (\Exception $e) { $this->em->getConnection()->rollback(); $this->em->close(); throw $e; } }
/** * Test the <code>$serviceDAO->removeEndpoint($endpoint)</code> method. */ public function testServiceDAORemoveEndpoint() { print __METHOD__ . "\n"; // create a linked entity graph as beow: // // se -----| (1 service) // / | \ | // el1 el2 el3 | (3 endpoints) // | \ | / | // dt0 dt1 ----| (1 downtime) $dt1 = new Downtime(); $dt1->setDescription('downtime description'); $dt1->setSeverity("WARNING"); $dt1->setClassification("UNSCHEDULED"); $dt0 = new Downtime(); $dt0->setDescription('downtime description'); $dt0->setSeverity("WARNING"); $dt0->setClassification("UNSCHEDULED"); $se = TestUtil::createSampleService('service1'); $el1 = TestUtil::createSampleEndpointLocation(); $el2 = TestUtil::createSampleEndpointLocation(); $el3 = TestUtil::createSampleEndpointLocation(); $se->addEndpointLocationDoJoin($el1); $se->addEndpointLocationDoJoin($el2); $se->addEndpointLocationDoJoin($el3); //$se->_addDowntime($dt1); // we don't call this in client code ! $dt1->addEndpointLocation($el1); $dt1->addEndpointLocation($el2); $dt1->addEndpointLocation($el3); $dt1->addService($se); $dt0->addEndpointLocation($el1); //$dt0->addService($se); // note we don't add this relationship for purposes of the test // persist and flush $this->em->persist($se); $this->em->persist($el1); $this->em->persist($el2); $this->em->persist($el3); $this->em->persist($dt1); $this->em->persist($dt0); $this->em->flush(); // create DAO to test $serviceDao = new ServiceDAO(); $serviceDao->setEntityManager($this->em); // remove first endpoint causing dt0 to be orphaned $serviceDao->removeEndpoint($el1); $this->em->flush(); // Assert expected object graph // se -----| (1 service) // | \ | // el2 el3 | (2 endpoints) // | / | // dt0 dt1-----| (2 downtime) // $testConn = $this->getConnection(); $result = $testConn->createQueryTable('results_table', "SELECT * FROM EndpointLocations"); $this->assertTrue($result->getRowCount() == 2); $result = $testConn->createQueryTable('results_table', "SELECT * FROM Downtimes"); $this->assertTrue($result->getRowCount() == 2); $result = $testConn->createQueryTable('results_table', "SELECT * FROM Downtimes_EndpointLocations"); $this->assertTrue($result->getRowCount() == 2); // Assert expected object graph in ORM Mem $this->assertEquals(1, count($el2->getDowntimes())); $this->assertEquals(1, count($el3->getDowntimes())); $this->assertEquals(2, count($se->getEndpointLocations())); $this->assertEquals(1, count($se->getDowntimes())); $this->assertEquals(2, count($dt1->getEndpointLocations())); $this->assertEquals(1, count($dt1->getServices())); // remove another el $serviceDao->removeEndpoint($el2); $this->em->flush(); // Assert expected object graph in DB // se -----| (1 service) // \ | // el3 | (1 endpoints) // / | // dt0 dt1-----| (2 downtime) // $testConn = $this->getConnection(); $result = $testConn->createQueryTable('results_table', "SELECT * FROM EndpointLocations"); $this->assertTrue($result->getRowCount() == 1); $result = $testConn->createQueryTable('results_table', "SELECT * FROM Downtimes"); $this->assertTrue($result->getRowCount() == 2); $result = $testConn->createQueryTable('results_table', "SELECT * FROM Downtimes_EndpointLocations"); $this->assertTrue($result->getRowCount() == 1); // Assert expected object graph in ORM Mem $this->assertEquals(1, count($el3->getDowntimes())); $this->assertEquals(1, count($se->getEndpointLocations())); $this->assertEquals(1, count($se->getDowntimes())); $this->assertEquals(1, count($dt1->getEndpointLocations())); $this->assertEquals(1, count($dt1->getServices())); // remove another el $serviceDao->removeEndpoint($el3); $this->em->flush(); // Assert expected object graph in DB // se -----| (1 service) // | // | (1 endpoints) // | // dt0 dt1-----| (2 downtime) // $testConn = $this->getConnection(); $result = $testConn->createQueryTable('results_table', "SELECT * FROM EndpointLocations"); $this->assertTrue($result->getRowCount() == 0); $result = $testConn->createQueryTable('results_table', "SELECT * FROM Downtimes"); $this->assertTrue($result->getRowCount() == 2); $result = $testConn->createQueryTable('results_table', "SELECT * FROM Downtimes_EndpointLocations"); $this->assertTrue($result->getRowCount() == 0); // Assert expected object graph in ORM Mem $this->assertEquals(0, count($se->getEndpointLocations())); $this->assertEquals(1, count($se->getDowntimes())); $this->assertEquals(0, count($dt1->getEndpointLocations())); $this->assertEquals(1, count($dt1->getServices())); }