2011-04-28 01:41:12 +02:00
|
|
|
<?php
|
2015-06-08 16:47:14 +02:00
|
|
|
|
2015-06-08 17:09:25 +02:00
|
|
|
/*
|
2023-02-17 05:13:51 +01:00
|
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
|
|
* SPDX-License-Identifier: MIT
|
2015-06-08 17:09:25 +02:00
|
|
|
*/
|
|
|
|
|
2017-11-19 19:01:31 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2011-04-28 01:41:12 +02:00
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
|
2020-08-26 09:47:59 +02:00
|
|
|
use Respect\Validation\Exceptions\ValidationException;
|
2018-12-05 08:36:38 +01:00
|
|
|
use Respect\Validation\Test\TestCase;
|
2019-02-07 21:22:10 +01:00
|
|
|
use Respect\Validation\Validatable;
|
2012-05-17 21:16:45 +02:00
|
|
|
use Respect\Validation\Validator;
|
|
|
|
|
2015-08-16 20:00:05 +02:00
|
|
|
/**
|
|
|
|
* @group rule
|
2017-02-04 14:01:14 +01:00
|
|
|
* @covers \Respect\Validation\Exceptions\NotException
|
2018-07-23 20:35:14 +02:00
|
|
|
* @covers \Respect\Validation\Rules\Not
|
2018-11-25 17:56:04 +01:00
|
|
|
*
|
2023-02-13 05:59:06 +01:00
|
|
|
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
|
2018-11-25 17:56:04 +01:00
|
|
|
* @author Caio César Tavares <caiotava@gmail.com>
|
|
|
|
* @author Gabriel Caruso <carusogabriel34@gmail.com>
|
|
|
|
* @author Henrique Moody <henriquemoody@gmail.com>
|
2015-08-16 20:00:05 +02:00
|
|
|
*/
|
2019-02-09 14:31:56 +01:00
|
|
|
final class NotTest extends TestCase
|
2011-04-28 01:41:12 +02:00
|
|
|
{
|
|
|
|
/**
|
2018-01-28 17:16:53 +01:00
|
|
|
* @doesNotPerformAssertions
|
|
|
|
*
|
2011-04-28 01:41:12 +02:00
|
|
|
* @dataProvider providerForValidNot
|
2018-07-23 20:35:14 +02:00
|
|
|
*
|
|
|
|
* @test
|
2019-02-07 21:22:10 +01:00
|
|
|
*
|
|
|
|
* @param mixed $input
|
2011-04-28 01:41:12 +02:00
|
|
|
*/
|
2019-02-07 21:22:10 +01:00
|
|
|
public function not(Validatable $rule, $input): void
|
2011-04-28 01:41:12 +02:00
|
|
|
{
|
2019-02-07 21:22:10 +01:00
|
|
|
$not = new Not($rule);
|
2018-01-28 17:16:53 +01:00
|
|
|
$not->assert($input);
|
2011-04-28 01:41:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerForInvalidNot
|
2018-07-23 20:35:14 +02:00
|
|
|
*
|
|
|
|
* @test
|
2019-02-07 21:22:10 +01:00
|
|
|
*
|
|
|
|
* @param mixed $input
|
2011-04-28 01:41:12 +02:00
|
|
|
*/
|
2019-02-07 21:22:10 +01:00
|
|
|
public function notNotHaha(Validatable $rule, $input): void
|
2011-04-28 01:41:12 +02:00
|
|
|
{
|
2019-02-07 21:22:10 +01:00
|
|
|
$not = new Not($rule);
|
2020-08-26 09:47:59 +02:00
|
|
|
|
|
|
|
$this->expectException(ValidationException::class);
|
|
|
|
|
2018-01-28 17:16:53 +01:00
|
|
|
$not->assert($input);
|
2011-04-28 01:41:12 +02:00
|
|
|
}
|
|
|
|
|
2016-01-29 22:50:27 +01:00
|
|
|
/**
|
|
|
|
* @dataProvider providerForSetName
|
2018-07-23 20:35:14 +02:00
|
|
|
*
|
|
|
|
* @test
|
2016-01-29 22:50:27 +01:00
|
|
|
*/
|
2019-02-07 21:22:10 +01:00
|
|
|
public function notSetName(Validatable $rule): void
|
2016-01-29 22:50:27 +01:00
|
|
|
{
|
2019-02-07 21:22:10 +01:00
|
|
|
$not = new Not($rule);
|
2016-01-29 22:50:27 +01:00
|
|
|
$not->setName('Foo');
|
|
|
|
|
2017-11-12 14:33:39 +01:00
|
|
|
self::assertEquals('Foo', $not->getName());
|
2019-04-05 22:44:42 +02:00
|
|
|
self::assertEquals('Foo', $not->getNegatedRule()->getName());
|
2016-01-29 22:50:27 +01:00
|
|
|
}
|
|
|
|
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
|
|
|
* @return mixed[][]
|
|
|
|
*/
|
|
|
|
public function providerForValidNot(): array
|
2011-04-28 01:41:12 +02:00
|
|
|
{
|
2015-10-18 03:44:47 +02:00
|
|
|
return [
|
|
|
|
[new IntVal(), ''],
|
|
|
|
[new IntVal(), 'aaa'],
|
|
|
|
[new AllOf(new NoWhitespace(), new Digit()), 'as df'],
|
|
|
|
[new AllOf(new NoWhitespace(), new Digit()), '12 34'],
|
|
|
|
[new AllOf(new AllOf(new NoWhitespace(), new Digit())), '12 34'],
|
2016-11-06 18:45:44 +01:00
|
|
|
[new AllOf(new NoneOf(new NumericVal(), new IntVal())), 13.37],
|
|
|
|
[new NoneOf(new NumericVal(), new IntVal()), 13.37],
|
|
|
|
[Validator::noneOf(Validator::numericVal(), Validator::intVal()), 13.37],
|
2015-10-18 03:44:47 +02:00
|
|
|
];
|
2011-04-28 01:41:12 +02:00
|
|
|
}
|
|
|
|
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
|
|
|
* @return mixed[][]
|
|
|
|
*/
|
|
|
|
public function providerForInvalidNot(): array
|
2011-04-28 01:41:12 +02:00
|
|
|
{
|
2015-10-18 03:44:47 +02:00
|
|
|
return [
|
|
|
|
[new IntVal(), 123],
|
2017-02-05 12:23:22 +01:00
|
|
|
[new AllOf(new AnyOf(new NumericVal(), new IntVal())), 13.37],
|
|
|
|
[new AnyOf(new NumericVal(), new IntVal()), 13.37],
|
|
|
|
[Validator::anyOf(Validator::numericVal(), Validator::intVal()), 13.37],
|
2015-10-18 03:44:47 +02:00
|
|
|
];
|
2011-04-28 01:41:12 +02:00
|
|
|
}
|
2016-01-29 22:50:27 +01:00
|
|
|
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
|
|
|
* @return Validatable[][]
|
|
|
|
*/
|
|
|
|
public function providerForSetName(): array
|
2016-01-29 22:50:27 +01:00
|
|
|
{
|
|
|
|
return [
|
2019-04-05 22:44:42 +02:00
|
|
|
'non-allOf' => [new IntVal()],
|
|
|
|
'allOf' => [new AllOf(new NumericVal(), new IntVal())],
|
|
|
|
'not' => [new Not(new Not(new IntVal()))],
|
|
|
|
'allOf with name' => [Validator::intVal()->setName('Bar')],
|
|
|
|
'noneOf' => [Validator::noneOf(Validator::numericVal(), Validator::intVal())],
|
2016-01-29 22:50:27 +01:00
|
|
|
];
|
|
|
|
}
|
2012-05-17 20:51:49 +02:00
|
|
|
}
|