/** * Test if delete action can be accessed */ public function testDeleteActionCanBeAccessed() { $data = array('id' => 42, 'firstname' => 'Manfred', 'lastname' => 'Mustermann', 'street' => 'Am Testen 123', 'postcode' => '54321', 'city' => 'Musterhausen', 'country' => 'de'); $expectedEntity = new CustomerEntity(); $customerHydrator = new CustomerHydrator(); $customerHydrator->hydrate($data, $expectedEntity); $mockCustomerService = $this->getMockBuilder('Customer\\Service\\CustomerService')->getMock(); $mockCustomerService->expects($this->any())->method('fetchSingleById')->will($this->returnValue($expectedEntity)); $serviceManager = $this->getApplicationServiceLocator(); $serviceManager->setAllowOverride(true); $serviceManager->setService('Customer\\Service\\Customer', $mockCustomerService); $this->dispatch('/customer/delete/42'); $this->assertResponseStatusCode(302); $this->assertRedirectTo('/customer'); }
public function testDeleteCustomer() { $data = array('id' => 42, 'firstname' => 'Manfred', 'lastname' => 'Mustermann', 'street' => 'Am Testen 123', 'postcode' => '54321', 'city' => 'Musterhausen', 'country' => 'de'); $expectedCustomerEntity = new CustomerEntity(); $customerHydrator = new CustomerHydrator(); $customerHydrator->hydrate($data, $expectedCustomerEntity); $mockDbStatement = $this->getMock('Zend\\Db\\Adapter\\Driver\\StatementInterface'); $mockDbStatement->expects($this->any())->method('execute')->will($this->returnValue($data)); $mockDbDriver = $this->getMock('Zend\\Db\\Adapter\\Driver\\DriverInterface'); $mockDbDriver->expects($this->any())->method('createStatement')->will($this->returnValue($mockDbStatement)); $mockDbAdapter = $this->getMock('Zend\\Db\\Adapter\\Adapter', null, array($mockDbDriver)); $mockCustomerTable = $this->getMock('Customer\\Table\\CustomerTable', array('deleteCustomer', 'getAffectedRows', 'fetchSingleById'), array($mockDbAdapter)); $mockCustomerTable->expects($this->any())->method('deleteCustomer')->will($this->returnValue(true)); $mockCustomerTable->expects($this->any())->method('getAffectedRows')->will($this->returnValue(1)); $mockCustomerTable->expects($this->any())->method('fetchSingleById')->will($this->returnValue($expectedCustomerEntity)); $customerService = new CustomerService(); $customerService->setCustomerTable($mockCustomerTable); $result = $customerService->delete(42); $this->assertInternalType('boolean', $result); }
/** * Save a customer * * @param array $data input data * @param integer $id id of customer entry * * @return CustomerEntity|false */ public function save(array $data, $id = null) { // get mode $mode = !$id ? 'insert' : 'update'; // get customer $customerEntity = $mode == 'insert' ? new CustomerEntity() : $this->fetchSingleById($id); // get filter and set data $filter = $this->getCustomerFilter(); if ($mode == 'insert') { $filter->remove('id'); } $filter->setData($data); // check for invalid data if (!$filter->isValid()) { return false; } /** @var CustomerHydrator $hydrator */ $hydrator = new CustomerHydrator(); $hydrator->hydrate($data, $customerEntity); // insert new customer try { if ($mode == 'insert') { $this->getCustomerTable()->insertCustomer($customerEntity); // get last insert value $id = $this->getCustomerTable()->getLastInsertValue(); } else { $this->getCustomerTable()->updateCustomer($customerEntity); } } catch (InvalidQueryException $e) { return false; } // reload customer $customerEntity = $this->fetchSingleById($id); // return entity return $customerEntity; }