public function testUpdateExistingCustomer() { $customerFilter = new CustomerInputFilter(); $customerTable = new CustomerTable($this->adapter); $customerHydrator = new CustomerHydrator(); $customerService = new CustomerService(); $customerService->setCustomerFilter($customerFilter); $customerService->setCustomerTable($customerTable); $customerEntity = $customerService->fetchSingleById(42); $customerEntity->setFirstname('Monika'); $customerEntity->setLastname('Musterfrau'); $data = $customerHydrator->extract($customerEntity); $customerEntity = $customerService->save($data, $customerEntity->getId()); $queryTable = $this->getConnection()->createQueryTable('loadCustomersOrderedByLastname', 'SELECT * FROM customers WHERE id = "42";'); $expectedRow = $queryTable->getRow(0); $customerRow = $customerHydrator->extract($customerEntity); $this->assertEquals($expectedRow, $customerRow); }
/** * Update existing customer * * @param CustomerEntity $customerEntity * @return integer */ public function updateCustomer(CustomerEntity $customerEntity) { $hydrator = new CustomerHydrator(); $updateData = $hydrator->extract($customerEntity); return $this->update($updateData, array('id' => $customerEntity->getId())); }
/** * Handle customer update */ public function updateAction() { $id = $this->params()->fromRoute('id'); $customerForm = $this->getCustomerForm(); $request = $this->getRequest(); if ($request->isPost()) { $customerEntity = $this->getCustomerService()->save($request->getPost()->toArray(), $id); if ($customerEntity) { return $this->redirect()->toRoute('customer/action', array('action' => 'update', 'id' => $customerEntity->getId())); } $customerForm->setMessages($this->getCustomerService()->getCustomerFilter()->getMessages()); $customerForm->setData($this->getCustomerService()->getCustomerFilter()->getValues()); } else { $customerEntity = $this->getCustomerService()->fetchSingleById($id); $customerHydrator = new CustomerHydrator(); $customerForm->setData($customerHydrator->extract($customerEntity)); } return new ViewModel(array('customerForm' => $customerForm)); }
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); }
/** * 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'); }
/** * 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; }