Mauro Baptista

I'm a software developer! I'm comfortable with PHP and Javascript. Currently developing this blog and a limo reservation app using Laravel and TDD!

Always learning something new!

January 24, 2017 at 00:00 / By Mauro Baptista Laravel PHP

How test a custom validation on Laravel

On this post we went through the creation of custom validations, but how to test this validations?

On my approach, I like to create a Unit test, and add all my custom validations tests there. To test the even and odd number, I would write a simple test like that:


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

class CustomValidatorTest extends TestCase
{
    /** @test */
    function trying_to_validate_an_even_number_using_even_validator()
    {
        $request = [
            'number' => 2,
        ];

        $validator = Validator::make($request, [
            'number' => 'even',
        ]);

        $this->assertFalse($validator->fails());
    }

    /** @test */
    function trying_to_validate_an_odd_number_using_even_validator()
    {
        $request = [
            'number' => 1,
        ];

        $validator = Validator::make($request, [
            'number' => 'even',
        ]);

        $this->assertTrue($validator->fails());
    }

    /** @test */
    function trying_to_validate_an_odd_number_using_odd_validator()
    {
        $request = [
            'number' => 1,
        ];

        $validator = Validator::make($request, [
            'number' => 'odd',
        ]);

        $this->assertFalse($validator->fails());
    }

    /** @test */
    function trying_to_validate_an_even_number_using_odd_validator()
    {
        $request = [
            'number' => 2,
        ];

        $validator = Validator::make($request, [
            'number' => 'odd',
        ]);

        $this->assertTrue($validator->fails());
    }
}

Whoops, looks like something went wrong.

1/1 ErrorException in Filesystem.php line 111: file_put_contents(): Only 0 of 300 bytes written, possibly out of free disk space

  1. in Filesystem.php line 111
  2. at HandleExceptions->handleError('2', 'file_put_contents(): Only 0 of 300 bytes written, possibly out of free disk space', '/opt/bitnami/apps/carnou.com/blog/vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php', '111', array('path' => '/opt/bitnami/apps/carnou.com/blog/storage/framework/sessions/kRjgG6hRNmKNxtZhEhv2eX0HaOKMecctXNtNtzZe', 'contents' => 'a:4:{s:6:"_token";s:40:"9D5PVV5cyVLMhtcIROpIykda7QhYGdMhrviqUrXR";s:9:"_previous";a:1:{s:3:"url";s:62:"http://carnou.com/post/how-test-a-custom-validation-on-laravel";}s:9:"_sf2_meta";a:3:{s:1:"u";i:1519159922;s:1:"c";i:1519159922;s:1:"l";s:1:"0";}s:6:"_flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}}', 'lock' => true))
  3. at file_put_contents('/opt/bitnami/apps/carnou.com/blog/storage/framework/sessions/kRjgG6hRNmKNxtZhEhv2eX0HaOKMecctXNtNtzZe', 'a:4:{s:6:"_token";s:40:"9D5PVV5cyVLMhtcIROpIykda7QhYGdMhrviqUrXR";s:9:"_previous";a:1:{s:3:"url";s:62:"http://carnou.com/post/how-test-a-custom-validation-on-laravel";}s:9:"_sf2_meta";a:3:{s:1:"u";i:1519159922;s:1:"c";i:1519159922;s:1:"l";s:1:"0";}s:6:"_flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}}', '2') in Filesystem.php line 111
  4. at Filesystem->put('/opt/bitnami/apps/carnou.com/blog/storage/framework/sessions/kRjgG6hRNmKNxtZhEhv2eX0HaOKMecctXNtNtzZe', 'a:4:{s:6:"_token";s:40:"9D5PVV5cyVLMhtcIROpIykda7QhYGdMhrviqUrXR";s:9:"_previous";a:1:{s:3:"url";s:62:"http://carnou.com/post/how-test-a-custom-validation-on-laravel";}s:9:"_sf2_meta";a:3:{s:1:"u";i:1519159922;s:1:"c";i:1519159922;s:1:"l";s:1:"0";}s:6:"_flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}}', true) in FileSessionHandler.php line 83
  5. at FileSessionHandler->write('kRjgG6hRNmKNxtZhEhv2eX0HaOKMecctXNtNtzZe', 'a:4:{s:6:"_token";s:40:"9D5PVV5cyVLMhtcIROpIykda7QhYGdMhrviqUrXR";s:9:"_previous";a:1:{s:3:"url";s:62:"http://carnou.com/post/how-test-a-custom-validation-on-laravel";}s:9:"_sf2_meta";a:3:{s:1:"u";i:1519159922;s:1:"c";i:1519159922;s:1:"l";s:1:"0";}s:6:"_flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}}') in Store.php line 263
  6. at Store->save() in StartSession.php line 88
  7. at StartSession->terminate(object(Request), object(Response)) in Kernel.php line 177
  8. at Kernel->terminate(object(Request), object(Response)) in index.php line 56