mardi 5 juillet 2016

PHPUnit test form input and redirect (laravel app)

I am creating a Laravel application. The homepage has a form for 1) name 2) email 3) submit button 'Register'. I already have form validation in my controller code. I want to use PHPUnit tests to ensure that my redirect (based on form input) is working correctly. However, in my PHPUnit test I am not passing in sample input values but rather both fields remain blank. PHPUnit test, Laravel view, Laravel controller, and PHPunit terminal output are below.

mylaravelapp/tests/UserRegistrationTest.php:

<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class UserRegistrationTest extends TestCase
{
    /** @test */
    public function validInput()
    {
      $this->visit('/')
        ->type('bob', 'name')
        ->type('bob@email.com', 'email')
        ->press('Register')
        ->seePageIs('/');
    }
}

Form tag section in view-file:

<form action="" method="post">
    <div class="form-group">
        <input name="name" class="form-control" type="text" placeholder="Name" />
    </div>
    <div class="form-group">
        <input name="email" class="form-control" type="text" placeholder="Email" />
     </div>
     <input type="submit" value="Register" class="btn btn-primary" />
</form>

User registration method in Controller:

public function createUser()
{
    $user = new User();

    $name = $email = "";
    $nameErr = $emailErr = "";

    // W3 schools
    if (empty($_POST["name"])) {
      $nameErr = "Name is required";
      echo($nameErr . ". Please try again. ");
    } else {
      $name = UserController::testInput($_POST["name"]);
      if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
        $nameErr = "Only letters and white space allowed in name";
        echo($nameErr . ". Please try again. ");
      } else {
        $user->name = $name;
      }
    }
    if (empty($_POST["email"])) {
      $emailErr = "Email is required";
      echo($emailErr . ". Please try again. ");
    } else {
      $email = UserController::testInput($_POST["email"]);
      if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $emailErr = "Invalid email format";
        echo($emailErr . ". Please try again. ");
      } else {
        $user->email = $email;
      }
    }

    if (!($emailErr=="") || !($nameErr=="")) {
        // sleep gives user time to read error message on redirect page
        sleep(2);
        return Redirect::route('createUser');
    } else {
        $user->save();
        return Redirect::route('userSuccess');
    }
}

And finally when I type "phpunit" in the terminal:

PHPUnit 5.4.6 by Sebastian Bergmann and contributors.

.. 2 / 2 (100%)Name is required. Please try again. Email is required. Please try again. 

Time: 3.85 seconds, Memory: 22.75MB

OK (2 tests, 7 assertions)

Thus it's clear that the "type()" function is not passing the values into my form inputs 'name' and 'email'. What functions should I use to create sample input values so I can make sure I redirect to the correct page? Thanks!

Aucun commentaire:

Enregistrer un commentaire