Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function testAddActionRedirectsAfterValidPost()
- {
- $postObject = new Post();
- $postServiceMock = $this->getMockBuilder('BlogServicePostServiceInterface')
- ->disableOriginalConstructor()->getMock();
- $postServiceMock->expects($this->once())->method('savePost')
- ->will($this->returnValue($postObject));
- $postFormMock = $this->getMockBuilder('BlogFormPostForm')
- ->disableOriginalConstructor()->getMock();
- $postFormMock->expects($this->once())->method('isValid')
- ->will($this->returnValue(true));
- $postFormMock->expects($this->once())->method('getData')
- ->will($this->returnValue($postObject));
- $serviceManager = $this->getApplicationServiceLocator();
- $serviceManager->setAllowOverride(true);
- $serviceManager->setService('BlogServicePostServiceInterface', $postServiceMock);
- $serviceManager->setService('BlogFormPostForm', $postFormMock);
- $postData = array(
- 'id' => '',
- 'title' => 'My title',
- 'text' => 'My text'
- );
- $this->dispatch('/blog/add', 'POST', $postData);
- $this->assertModuleName('Blog');
- $this->assertControllerName('BlogControllerWrite'); //Controller name was defined in the module.config.php
- $this->assertControllerClass('WriteController');
- $this->assertMatchedRouteName('blog/add');
- $this->assertResponseStatusCode(302);
- $this->assertRedirectTo('/blog');
- }
- $postFormMock->expects($this->once())->method('isValid')
- ->will($this->returnValue(false));
- public function addAction()
- {
- $request = $this->getRequest();
- if ($request->isPost()) {
- $this->postForm->setData($request->getPost());
- if ($this->postForm->isValid()) {
- try { //TODO create private method?
- $this->postService->savePost($this->postForm->getData());
- return $this->redirect()->toRoute('blog');
- } catch (Exception $e) {
- die($e->getMessage());
- //TODO Some DB Error happened, log it and let the user know
- }
- }
- }
- return new ViewModel(array(
- 'form' => $this->postForm
- ));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement