Reputation: 17282
I'm brand new to phpunit testing. Can anyone help me on how to test the lines below in the image.
So far my test is:
public function testCanSendEmail()
{
$formData = array(
'subject' => 'test subject',
'email' => '[email protected]',
'message' => 'test message',
'name' => 'test name');
$this->request
->setMethod('POST')
->setPost($formData);
$this->dispatch('/contact');
$this->assertAction('win');
I was under the impression that if the validation succeeded it would follow through the whole action? Can anyone please explain what is happening here, and also what a correct test would be for such an action.
Upvotes: 3
Views: 570
Reputation: 316999
The obvious explanation is that $form->isValid
returns FALSE
.
The coverage report shows that the if
block for a valid form never got executed. Instead, the else block was executed. The thing you need to find out is why and eliminate the cause. Use a Debugger and step through the execution flow to see what happens at runtime.
As an alternative, you can stub the contact form to return TRUE
. Because the form is hardcoded into the controller action, have a look at http://sebastian-bergmann.de/archives/885-Stubbing-Hard-Coded-Dependencies.html on how to do that.
Yet another alternative: make the code to send the contact form into a Service Layer to allow testing this without having to make an actual request.
Upvotes: 1