1

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'     => '[email protected]',
        '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'     => '[email protected]',
        '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.

1 Answer 1

6
$response = $this->route('POST', 'guest.report_issue', array(
    'full_name' => 'Foo',
    'email' => '[email protected]',
    'issue' => 'FooBar'));

You can pass parameters as an array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.