watson/testing

Laravel controller and model testing helpers.

Installs: 853

Dependents: 0

Stars: 16

Watchers: 2

Forks: 2

Open Issues: 2

Language: PHP

2.0.0 2015-03-19 00:21 UTC

README

Build Status

Testing contains traits with helpers for testing models and controllers in Laravel. It helps you assert the validity of your models (assuming the use of watson/validating) as well as the relationships of your models. It also allows you to test the responses of your controllers.

Installation

Simply run the following Composer command in your application.

composer require watson/testing

The ~1.0 stream is for Laravel 4, and the ~2.0 stream is for Laravel 5.

Overview

To use the test helpers in your tests, simply bring the trait in to your test file.

class UsersControllerTest extends TestCase {
    use Watson\Testing\ControllerHelpers;
}
class UserTest extends TestCase {
    use Watson\Testing\ModelHelpers;
}

If you'd prefer to use the test helpers globally, just use this trait in your TestCase.php.

class TestCase extends Illuminate\Foundation\Testing\TestCase {
    use Watson\Testing\TestingTrait;
}

Controller testing

assertViewIs($expectedView, $message = null);

// Controller
return View::make('users.index');

// Test
$this->assertViewIs('users.index');

Ensure that the view used in the response is the one you expected.

Model testing

Model validations

If you're using watson/validating on your models it is really easy to test your validations. We will use the following User model for these examples.

$user = new User;
assertValid(Model $model, $message = null)
$user->email = 'example@example.com';

$this->assertValid($user);
assertInvalid(Model $model, $message = null)
$user->email = 'foo';

$this->assertInvalid($user);

If you want to easily check if a model is valid or invalid with or without a certain attribute, there a number of helpers for quickly asserting that this is the case.

assertValidWith(Model $model, $attribute, $value = null, $message = null)
$this->assertValidWith($user, 'email', 'example@example.com');
assertValidWithout(Model $model, $attribute, $message = null)
$this->assertValidWithout($user, 'last_name')
assertInvalidWith(Model $model, $attribute, $value = null, $message = null)
$this->assertInvalidWith($user, 'email', 'foo');
assertInvalidWithout(Model $model, $attribute, $message = null)
$this->assertInvalidWithout($user, 'email');

Specific model validations

If you'd prefer an easier (and more readable) way of asserting the validations on your model you might like to try specific model validations. They work with watson/validating or any other validation trait that complies with Watson\Validating\ValidatingInterface (that is, has a getDefaultRules method).

assertValidatesWith(Model $mode, $attribute, $rule, $message = null)
// Assert that the email attribute is required.
$this->assertValidatesWith($user, 'email', 'required');
$this->assertValidatesRequired($user, 'email');

Here is the list of included Laravel default validation assertions:

  • assertValidatesAccepted(Model $model, $attribute, $message = null)
  • assertValidatesActiveUrl(Model $model, $attribute)
  • assertValidatesAfter(Model $model, $attribute, $date)
  • assertValidatesAlpha(Model $model, $attribute)
  • assertValidatesAlphaDash(Model $model, $attribute)
  • assertValidatesAlphaNum(Model $model, $attribute)
  • assertValidatesArray(Model $model, $attribute)
  • assertValidatesBefore(Model $model, $attribute, $date)
  • assertValidatesBetween(Model $model, $attribute, $min, $max)
  • assertValidatesConfirmed(Model $model, $attribute)
  • assertValidatesDate(Model $model, $attribute)
  • assertValidatesDate(Model $model, $attribute)
  • assertValidatesDifferent(Model $model, $attribute, $field)
  • assertValidatesDigits(Model $model, $attribute, $value)
  • assertValidatesDigitsBetween(Model $model, $attribute, $min, $max)
  • assertValidatesBoolean(Model $model, $attribute)
  • assertValidatesEmail(Model $model, $attribute)
  • assertValidatesExists(Model $model, $attribute, $parameters)
  • assertValidatesImage(Model $model, $attribute)
  • assertValidatesIn(Model $model, $attribute, $values)
  • assertValidatesInteger(Model $model, $attribute)
  • assertValidatesIp(Model $model, $attribute)
  • assertValidatesMax(Model $model, $attribute, $value)
  • assertValidatesMimes(Model $model, $attribute, $values)
  • assertValidatesMin(Model $model, $attribute, $value)
  • assertValidatesNotIn(Model $model, $attribute, $values)
  • assertValidatesNumeric(Model $model, $attribute)
  • assertValidatesRegex(Model $model, $attribute, $pattern)
  • assertValidatesRequired(Model $model, $attribute)
  • assertValidatesRequiredIf(Model $model, $attribute, $field, $value)
  • assertValidatesRequiredWith(Model $model, $attribute, $values)
  • assertValidatesRequiredWithAll(Model $model, $attribute, $values)
  • assertValidatesRequiredWithout(Model $model, $attribute, $values)
  • assertValidatesRequiredWithoutAll(Model $model, $attribute, $values)
  • assertValidatesSame(Model $model, $attribute, $field)
  • assertValidatesSize(Model $model, $attribute, $value)
  • assertValidatesTimezone(Model $model, $attribute)
  • assertValidatesUnique(Model $model, $attribute, $parameters)

Model relationships

You can assert the different relationships exist on your model.

assertBelongsTo($class, $relation)

Ensure that a post belongs to a user.

$this->assertBelongsTo($post, 'user');
assertBelongsToMany($class, $relation)

Ensure that a tag belongs to many posts.

$this->assertBelongsToMany($tag, 'posts');
assertHasOne($class, $relation)

Ensure that a user has one profile.

$this->assertHasOne($user, 'profile');
assertHasMany($class, $relation)

Ensure that a user has many posts.

$this->assertHasMany($user, 'posts');

Credits

This package builds upon the work of the now unmaintained way/laravel-test-helpers and includes code from the unmerged pull requests of SammyK, effi, mrevd and sorora.

I decided to continue the development of these helpers because I prefer testing with the tool that ships with the framework, and I really like PHPUnit.