I am creating a Laravel 4 app and i have run into a little snag. While writing tests for my controller i noticed for some strange reason it can never seem to validate. Heres my (stripped down) controller codes.
<?php use Controllers\Base\PublicController;
class GuestController extends PublicController {
/**
* Display the report issue form.
*
* @return null
*/
public function getReportIssue()
{
$this->layout->title = Lang::get('app.report_issue');
$this->layout->content = View::make('guest.report_issue');
}
public function postReportIssue()
{
$rules = [
'full_name' => 'required|min:2|max:100',
'email' => 'required|email',
'issue' => 'required|min:10|max:1000',
];
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails())
{
return Redirect::route('guest.report_issue')
->withInput()
->withErrors($validator->messages());
}
return Redirect::route('guest.reported_issue')
->with('msg', 'Okay');
}
}
Now he tests ive created for the two methods above are...
public function testHandleFailReportIssue()
{
Input::replace([
'full_name' => '',
'email' => '',
'issue' => '',
]);
$this->call('POST', 'report-issue');
$this->assertRedirectedToRoute('guest.report_issue');
$this->assertSessionHasErrors(['full_name', 'email', 'issue']);
}
public function testHandlePassReportIssue()
{
Input::replace([
'full_name' => 'John Doe',
'email' => 'john@localhost.dev',
'issue' => 'Lorem ipsum idom lola singel tudor reopmatica loesn dolor gotar.
Lorem ipsum idom lola singel tudor reopmatica loesn dolor gotar'
]);
$this->call('POST', 'report-issue');
$this->assertRedirectedToRoute('guest.reported_issue', [], ['msg']);
}
The initial test passes successfully, but the second one fails. After a little investigation, it shows that the Validation was not passing, which means that the Input::replace()
method is not doing its job, because i injected valid request values. Maybe im missing something please?
[EDIT]
I decided to do this
public function testHandlePassReportIssue()
{
Input::replace([
'full_name' => 'John Doe',
'email' => 'john@flashdp.com',
'issue' => 'Lorem ipsum idom lola singel tudor reopmatica loesn dolor gotar.
Lorem ipsum idom lola singel tudor reopmatica loesn dolor gotar',
]);
$response = $this->route('POST', 'guest.report_issue');
dd($this->app['session']->get('errors'));
$this->assertRedirectedToRoute('guest.reported_issue', [], ['msg']);
}
on the test to debug by inspecting the session and like i suspected, the Input was not getting populated, any reason this could have happened? The validation messages were returned.
$response = $this->route('POST', 'guest.report_issue', array(
'full_name' => 'Foo',
'email' => 'Man@Chu.com',
'issue' => 'FooBar'));
You can pass parameters as an array.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.