Created
September 16, 2019 00:38
-
-
Save allaniftrue/38864556fbc4470cea72af5ed84c4fa0 to your computer and use it in GitHub Desktop.
Testing Laravel Rules
This file contains hidden or 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 DateTime; | |
use Illuminate\Contracts\Validation\Rule; | |
use Illuminate\Validation\Concerns\ValidatesAttributes; | |
class DateOrPresent implements Rule | |
{ | |
use ValidatesAttributes; | |
private $sampleDate; | |
public $error; | |
/** | |
* Create a new rule instance. | |
*/ | |
public function __construct($dateFormat) | |
{ | |
$this->dateFormat = $dateFormat; | |
$this->error = ucfirst(':attribute must be in MMM YYYY (e.g. '.$this->sampleDate.') format or the word present'); | |
} | |
/** | |
* Determine if the validation rule passes. | |
* | |
* @param string $attribute | |
* @param mixed $value | |
* | |
* @return bool | |
*/ | |
public function passes($attribute, $value) | |
{ | |
$this->sampleDate = now()->format($this->dateFormat); | |
$date = DateTime::createFromFormat('!'.$this->dateFormat, $value); | |
$now = DateTime::createFromFormat('!'.$this->dateFormat, $this->sampleDate); | |
if ($date > $now) { | |
$this->error = ucfirst(':attribute must not exceed '.ucfirst($this->sampleDate)); | |
return false; | |
} | |
if (strtolower($value) === 'present' || ($date && $date->format($this->dateFormat) == $value)) { | |
return true; | |
} | |
return false; | |
} | |
/** | |
* Get the validation error message. | |
* | |
* @return string | |
*/ | |
public function message() | |
{ | |
return $this->error; | |
} | |
} |
This file contains hidden or 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\Unit; | |
use Tests\TestCase; | |
use App\Rules\DateOrPresent; | |
class DateOrPresentValidationTest extends TestCase | |
{ | |
public function setUp() | |
{ | |
parent::setUp(); | |
} | |
public function test_word_present() | |
{ | |
$rules = [ | |
'field1' => [new DateOrPresent('M Y')], | |
]; | |
$data = [ | |
'field1' => 'present', | |
]; | |
$v = $this->app['validator']->make($data, $rules); | |
$this->assertTrue($v->passes()); | |
} | |
public function test_wrong_date_format() | |
{ | |
$rules = [ | |
'field1' => [new DateOrPresent('M Y')], | |
]; | |
$data = [ | |
'field1' => date('F d Y'), | |
]; | |
$v = $this->app['validator']->make($data, $rules); | |
$this->assertFalse($v->passes()); | |
} | |
public function test_greater_date() | |
{ | |
$rules = [ | |
'field1' => [new DateOrPresent('M Y')], | |
]; | |
$data = [ | |
'field1' => now()->addMonth(1)->format('M Y'), | |
]; | |
$v = $this->app['validator']->make($data, $rules); | |
$this->assertFalse($v->passes()); | |
} | |
public function test_equal_date() | |
{ | |
$rules = [ | |
'field1' => [new DateOrPresent('M Y')], | |
]; | |
$data = [ | |
'field1' => now()->format('M Y'), | |
]; | |
$v = $this->app['validator']->make($data, $rules); | |
$this->assertTrue($v->passes()); | |
} | |
public function test_less_date() | |
{ | |
$rules = [ | |
'field1' => [new DateOrPresent('M Y')], | |
]; | |
$data = [ | |
'field1' => now()->subDay(1)->format('M Y'), | |
]; | |
$v = $this->app['validator']->make($data, $rules); | |
$this->assertTrue($v->passes()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment