Search code examples
phpunitcode-coverage

How would I write a test to cover lines of code that fall through the cracks, sample provided


I am trying to acheive 100% code coverage, but I cannot determine how to cover lines like #57 below. It is simply an ending }. I have these lines throughout all of my code, as well as lines like, } else {, but they are not covered, nor marked as dead code.

How would I write a test to cover these lines?

  34                 :     public function addAction() {                                                              
  35                 :                                                                                                
  36               2 :         $form = new Roles_Form_Add();                                                          
  37                 :                                                                                                
  38               2 :         if ($this->getRequest()->isPost()) {                                                   
  39                 :                                                                                                
  40               1 :             if ($form->isValid($this->getRequest()->getPost())) {                              
  41                 :                                                                                                
  42               1 :                 $clean = $form->getValues();                                                   
  43                 :                                                                                                
  44               1 :                 $roleService = new Roles_Service_Role();                                       
  45                 :                                                                                                
  46               1 :                 $role = $roleService->fetchNew();                                              
  47               1 :                 $role->setFromArray($clean)                                                    
  48               1 :                     ->save();                                                                  
  49                 :                                                                                                
  50               1 :                 $this->_helper->flashMessenger('A new role has been added.');                  
  51                 :                                                                                                
  52               1 :                 $this->_helper->redirector('view','role','roles',array('id'=>$role->id));      
  53               1 :                 return;                                                                        
  54                 :                                                                                                
  55                 :             }                                                                                  
  56                 :                                                                                                
  57               0 :         }                                                                                      
  58                 :                                                                                                
  59               1 :         $form->setAction($this->_helper->url('add','role','roles'));                           
  60                 :                                                                                                
  61               1 :         $this->view->addRoleForm = $form;                                                      
  62                 :                                                                                                
  63               1 :     }                                                                                          
  64                 :              


public function testAddAction() {

    $this->dispatch('/roles/role/add');

    $this->assertModule('roles');
    $this->assertController('role');
    $this->assertAction('add');

    $this->assertQuery('form#addRole input#name');

}

public function testAddActionWithPost() {

    $this->getRequest()->setMethod('POST')
        ->setPost('name','Test');

    $this->dispatch('/roles/role/add');

    $this->assertRedirectTo('/roles/role/view/id/1');

}

Solution

  • From a first glimpse of the code, I'd say that line 40 (if ($form->isValid($this->getRequest()->getPost())) {) always evaluates to true in your tests and therefore the function addAction is always left with the return; statement in line 53.