respect-validation/tests/unit/Rules/OptionalTest.php
Henrique Moody ef8a8f4b27
Turn LICENSE file into plain/text
There is no need for that file to be a Markdown, and it can be a plain
text file.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2019-05-23 16:21:34 +02:00

173 lines
3.7 KiB
PHP

<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/
declare(strict_types=1);
namespace Respect\Validation\Rules;
use Respect\Validation\Test\TestCase;
use Respect\Validation\Validatable;
use stdClass;
/**
* @group rule
*
* @covers \Respect\Validation\Rules\Optional
*
* @author Gabriel Caruso <carusogabriel34@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
final class OptionalTest extends TestCase
{
/**
* @return mixed[][]
*/
public function providerForOptional(): array
{
return [
[null],
[''],
];
}
/**
* @return mixed[][]
*/
public function providerForNotOptional(): array
{
return [
[1],
[[]],
[' '],
[0],
['0'],
[0],
['0.0'],
[false],
[['']],
[[' ']],
[[0]],
[['0']],
[[false]],
[[[''], [0]]],
[new stdClass()],
];
}
/**
* @dataProvider providerForOptional
*
* @test
*
* @param mixed $input
*/
public function shouldNotValidateRuleWhenInputIsOptional($input): void
{
$validatable = $this->createMock(Validatable::class);
$validatable
->expects(self::never())
->method('validate');
$rule = new Optional($validatable);
self::assertTrue($rule->validate($input));
}
/**
* @dataProvider providerForNotOptional
*
* @test
*
* @param mixed $input
*/
public function shouldValidateRuleWhenInputIsNotOptional($input): void
{
$validatable = $this->createMock(Validatable::class);
$validatable
->expects(self::once())
->method('validate')
->with($input)
->will(self::returnValue(true));
$rule = new Optional($validatable);
self::assertTrue($rule->validate($input));
}
/**
* @test
*/
public function shouldNotAssertRuleWhenInputIsOptional(): void
{
$validatable = $this->createMock(Validatable::class);
$validatable
->expects(self::never())
->method('assert');
$rule = new Optional($validatable);
$rule->assert('');
}
/**
* @test
*/
public function shouldAssertRuleWhenInputIsNotOptional(): void
{
$input = 'foo';
$validatable = $this->createMock(Validatable::class);
$validatable
->expects(self::once())
->method('assert')
->with($input)
->will(self::returnValue(true));
$rule = new Optional($validatable);
$rule->assert($input);
}
/**
* @test
*/
public function shouldNotCheckRuleWhenInputIsOptional(): void
{
$validatable = $this->createMock(Validatable::class);
$validatable
->expects(self::never())
->method('check');
$rule = new Optional($validatable);
$rule->check('');
}
/**
* @test
*/
public function shouldCheckRuleWhenInputIsNotOptional(): void
{
$input = 'foo';
$validatable = $this->createMock(Validatable::class);
$validatable
->expects(self::once())
->method('check')
->with($input)
->will(self::returnValue(true));
$rule = new Optional($validatable);
$rule->check($input);
}
}