public function indexAction() { /** @var \Zend\Http\Request $request */ $request = $this->getRequest(); $id = $this->params()->fromRoute('id', null); try { $bookEntity = $this->service->getById($id); if ($request->isPost()) { $this->form->setData($request->getPost()->toArray()); if ($this->form->isValid()) { $this->flashMessenger()->addSuccessMessage('Books deleted successfully'); $this->service->delete($bookEntity); return $this->redirect()->toRoute('library/books'); } else { $this->flashMessenger()->addErrorMessage('Please fill form correctly'); } } else { $this->form->get('id')->setValue($bookEntity->getId()); } return ['form' => $this->form, 'book' => $bookEntity]; } catch (\Exception $e) { $this->flashMessenger()->addSuccessMessage($e->getMessage()); return $this->redirect()->toRoute('library/books'); } }
public function testConstructor() { $this->assertInstanceOf(Form::class, $this->testedObj); $this->assertEquals($this->testedObj->count(), 3); $this->assertTrue($this->testedObj->has('id')); $this->assertTrue($this->testedObj->has('csrf')); $this->assertTrue($this->testedObj->has('submit')); $idInput = $this->testedObj->get('id'); $this->assertInstanceOf(Hidden::class, $idInput); $csrfInput = $this->testedObj->get('csrf'); $this->assertInstanceOf(Csrf::class, $csrfInput); $submitInput = $this->testedObj->get('submit'); $this->assertInstanceOf(Submit::class, $submitInput); $this->assertEquals('Delete', $submitInput->getValue()); }