respect-validation/tests/unit/Rules/OptionalTest.php

173 lines
3.7 KiB
PHP
Raw Normal View History

2015-10-07 07:41:38 +02:00
<?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.
2015-10-07 07:41:38 +02:00
*/
declare(strict_types=1);
2015-10-07 07:41:38 +02:00
namespace Respect\Validation\Rules;
2018-12-05 08:36:38 +01:00
use Respect\Validation\Test\TestCase;
2016-10-30 20:07:28 +01:00
use Respect\Validation\Validatable;
use stdClass;
2015-10-07 07:41:38 +02:00
/**
* @group rule
*
2017-02-04 14:01:14 +01:00
* @covers \Respect\Validation\Rules\Optional
*
* @author Gabriel Caruso <carusogabriel34@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
2015-10-07 07:41:38 +02:00
*/
final class OptionalTest extends TestCase
2015-10-07 07:41:38 +02:00
{
/**
* @return mixed[][]
*/
public function providerForOptional(): array
2015-10-07 07:41:38 +02:00
{
2015-10-18 03:44:47 +02:00
return [
[null],
[''],
];
2015-10-07 07:41:38 +02:00
}
/**
* @return mixed[][]
*/
public function providerForNotOptional(): array
2015-10-07 07:41:38 +02:00
{
2015-10-18 03:44:47 +02:00
return [
[1],
[[]],
[' '],
[0],
['0'],
[0],
['0.0'],
[false],
[['']],
[[' ']],
[[0]],
[['0']],
[[false]],
[[[''], [0]]],
[new stdClass()],
];
2015-10-07 07:41:38 +02:00
}
/**
* @dataProvider providerForOptional
*
* @test
*
* @param mixed $input
*/
public function shouldNotValidateRuleWhenInputIsOptional($input): void
2015-10-07 07:41:38 +02:00
{
2016-10-30 20:07:28 +01:00
$validatable = $this->createMock(Validatable::class);
2015-10-07 07:41:38 +02:00
$validatable
->expects(self::never())
2015-10-07 07:41:38 +02:00
->method('validate');
$rule = new Optional($validatable);
self::assertTrue($rule->validate($input));
2015-10-07 07:41:38 +02:00
}
/**
* @dataProvider providerForNotOptional
*
* @test
*
* @param mixed $input
*/
public function shouldValidateRuleWhenInputIsNotOptional($input): void
2015-10-07 07:41:38 +02:00
{
2016-10-30 20:07:28 +01:00
$validatable = $this->createMock(Validatable::class);
2015-10-07 07:41:38 +02:00
$validatable
->expects(self::once())
2015-10-07 07:41:38 +02:00
->method('validate')
->with($input)
->will(self::returnValue(true));
2015-10-07 07:41:38 +02:00
$rule = new Optional($validatable);
self::assertTrue($rule->validate($input));
2015-10-07 07:41:38 +02:00
}
/**
* @test
*/
public function shouldNotAssertRuleWhenInputIsOptional(): void
2015-10-07 07:41:38 +02:00
{
2016-10-30 20:07:28 +01:00
$validatable = $this->createMock(Validatable::class);
2015-10-07 07:41:38 +02:00
$validatable
->expects(self::never())
2015-10-07 07:41:38 +02:00
->method('assert');
$rule = new Optional($validatable);
$rule->assert('');
2015-10-07 07:41:38 +02:00
}
/**
* @test
*/
public function shouldAssertRuleWhenInputIsNotOptional(): void
2015-10-07 07:41:38 +02:00
{
$input = 'foo';
2016-10-30 20:07:28 +01:00
$validatable = $this->createMock(Validatable::class);
2015-10-07 07:41:38 +02:00
$validatable
->expects(self::once())
2015-10-07 07:41:38 +02:00
->method('assert')
->with($input)
->will(self::returnValue(true));
2015-10-07 07:41:38 +02:00
$rule = new Optional($validatable);
$rule->assert($input);
2015-10-07 07:41:38 +02:00
}
/**
* @test
*/
public function shouldNotCheckRuleWhenInputIsOptional(): void
2015-10-07 07:41:38 +02:00
{
2016-10-30 20:07:28 +01:00
$validatable = $this->createMock(Validatable::class);
2015-10-07 07:41:38 +02:00
$validatable
->expects(self::never())
2015-10-07 07:41:38 +02:00
->method('check');
$rule = new Optional($validatable);
$rule->check('');
2015-10-07 07:41:38 +02:00
}
/**
* @test
*/
public function shouldCheckRuleWhenInputIsNotOptional(): void
2015-10-07 07:41:38 +02:00
{
$input = 'foo';
2016-10-30 20:07:28 +01:00
$validatable = $this->createMock(Validatable::class);
2015-10-07 07:41:38 +02:00
$validatable
->expects(self::once())
2015-10-07 07:41:38 +02:00
->method('check')
->with($input)
->will(self::returnValue(true));
2015-10-07 07:41:38 +02:00
$rule = new Optional($validatable);
$rule->check($input);
2015-10-07 07:41:38 +02:00
}
}