Created
December 1, 2017 23:55
-
-
Save adamwathan/c1e10a0ec564e2cf4b4c91294f9886ab to your computer and use it in GitHub Desktop.
Unit Testing Custom Validation Rules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Rules; | |
use Illuminate\Contracts\Validation\Rule; | |
class Uppercase implements Rule | |
{ | |
public function passes($attribute, $value) | |
{ | |
return strtoupper($value) === $value; | |
} | |
public function message() | |
{ | |
return 'The :attribute must be uppercase.'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Tests\Feature\Rules; | |
use Tests\TestCase; | |
use App\Rules\Uppercase; | |
use Illuminate\Foundation\Testing\RefreshDatabase; | |
class UppercaseTest extends TestCase | |
{ | |
/** @test */ | |
function uppercase_strings_pass() | |
{ | |
$rule = new Uppercase; | |
$this->assertTrue($rule->passes('attribute', 'SHOULDPASS')); | |
} | |
/** @test */ | |
function mixed_strings_fail() | |
{ | |
$rule = new Uppercase; | |
$this->assertFalse($rule->passes('attribute', 'ShouldNotPass')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example for custom boolean validator
Test case with Pest