2015-10-08 18:54:42 +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.md"
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2017-11-19 19:01:31 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2015-10-08 18:54:42 +02:00
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
|
2017-11-04 11:21:40 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2015-10-08 18:54:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group rule
|
2017-02-04 14:01:14 +01:00
|
|
|
* @covers \Respect\Validation\Exceptions\KeyValueException
|
2018-07-23 20:35:14 +02:00
|
|
|
* @covers \Respect\Validation\Rules\KeyValue
|
2018-11-25 17:56:04 +01:00
|
|
|
*
|
|
|
|
* @author Gabriel Caruso <carusogabriel34@gmail.com>
|
|
|
|
* @author Henrique Moody <henriquemoody@gmail.com>
|
|
|
|
* @author Ian Nisbet <ian@glutenite.co.uk>
|
2015-10-08 18:54:42 +02:00
|
|
|
*/
|
2017-11-04 11:21:40 +01:00
|
|
|
class KeyValueTest extends TestCase
|
2015-10-08 18:54:42 +02:00
|
|
|
{
|
2018-07-23 20:35:14 +02:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function shouldDefineValuesOnConstructor(): void
|
2015-10-08 18:54:42 +02:00
|
|
|
{
|
|
|
|
$comparedKey = 'foo';
|
|
|
|
$ruleName = 'equals';
|
|
|
|
$baseKey = 'bar';
|
|
|
|
|
|
|
|
$rule = new KeyValue($comparedKey, $ruleName, $baseKey);
|
|
|
|
|
2017-11-12 14:33:39 +01:00
|
|
|
self::assertSame($comparedKey, $rule->comparedKey);
|
|
|
|
self::assertSame($ruleName, $rule->ruleName);
|
|
|
|
self::assertSame($baseKey, $rule->baseKey);
|
2015-10-08 18:54:42 +02:00
|
|
|
}
|
|
|
|
|
2018-07-23 20:35:14 +02:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function shouldNotValidateWhenComparedKeyDoesNotExist(): void
|
2015-10-08 18:54:42 +02:00
|
|
|
{
|
|
|
|
$rule = new KeyValue('foo', 'equals', 'bar');
|
|
|
|
|
2017-11-12 14:33:39 +01:00
|
|
|
self::assertFalse($rule->validate(['bar' => 42]));
|
2015-10-08 18:54:42 +02:00
|
|
|
}
|
|
|
|
|
2018-07-23 20:35:14 +02:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function shouldNotValidateWhenBaseKeyDoesNotExist(): void
|
2015-10-08 18:54:42 +02:00
|
|
|
{
|
|
|
|
$rule = new KeyValue('foo', 'equals', 'bar');
|
|
|
|
|
2017-11-12 14:33:39 +01:00
|
|
|
self::assertFalse($rule->validate(['foo' => true]));
|
2015-10-08 18:54:42 +02:00
|
|
|
}
|
|
|
|
|
2018-07-23 20:35:14 +02:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function shouldNotValidateRuleIsNotValid(): void
|
2015-10-08 18:54:42 +02:00
|
|
|
{
|
|
|
|
$rule = new KeyValue('foo', 'probably_not_a_rule', 'bar');
|
|
|
|
|
2017-11-12 14:33:39 +01:00
|
|
|
self::assertFalse($rule->validate(['foo' => true, 'bar' => false]));
|
2015-10-08 18:54:42 +02:00
|
|
|
}
|
|
|
|
|
2018-07-23 20:35:14 +02:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function shouldValidateWhenDefinedValuesMatch(): void
|
2015-10-08 18:54:42 +02:00
|
|
|
{
|
|
|
|
$rule = new KeyValue('foo', 'equals', 'bar');
|
|
|
|
|
2017-11-12 14:33:39 +01:00
|
|
|
self::assertTrue($rule->validate(['foo' => 42, 'bar' => 42]));
|
2015-10-08 18:54:42 +02:00
|
|
|
}
|
|
|
|
|
2018-07-23 20:35:14 +02:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function shouldValidateWhenDefinedValuesDoesNotMatch(): void
|
2015-10-08 18:54:42 +02:00
|
|
|
{
|
|
|
|
$rule = new KeyValue('foo', 'equals', 'bar');
|
|
|
|
|
2017-11-12 14:33:39 +01:00
|
|
|
self::assertFalse($rule->validate(['foo' => 43, 'bar' => 42]));
|
2015-10-08 18:54:42 +02:00
|
|
|
}
|
|
|
|
|
2018-01-28 17:16:53 +01:00
|
|
|
/**
|
|
|
|
* @doesNotPerformAssertions
|
2018-07-23 20:35:14 +02:00
|
|
|
*
|
|
|
|
* @test
|
2018-01-28 17:16:53 +01:00
|
|
|
*/
|
2018-07-23 20:35:14 +02:00
|
|
|
public function shouldAssertWhenDefinedValuesMatch(): void
|
2015-10-08 18:54:42 +02:00
|
|
|
{
|
|
|
|
$rule = new KeyValue('foo', 'equals', 'bar');
|
2018-01-28 17:16:53 +01:00
|
|
|
$rule->assert(['foo' => 42, 'bar' => 42]);
|
2015-10-08 18:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-02-04 14:01:14 +01:00
|
|
|
* @expectedException \Respect\Validation\Exceptions\AllOfException
|
2015-10-08 18:54:42 +02:00
|
|
|
* @expectedExceptionMessage All of the required rules must pass for foo
|
2018-07-23 20:35:14 +02:00
|
|
|
*
|
|
|
|
* @test
|
2015-10-08 18:54:42 +02:00
|
|
|
*/
|
2018-07-23 20:35:14 +02:00
|
|
|
public function shouldAssertWhenDefinedValuesDoesNotMatch(): void
|
2015-10-08 18:54:42 +02:00
|
|
|
{
|
|
|
|
$rule = new KeyValue('foo', 'equals', 'bar');
|
2015-10-18 03:44:47 +02:00
|
|
|
$rule->assert(['foo' => 43, 'bar' => 42]);
|
2015-10-08 18:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-02-04 14:01:14 +01:00
|
|
|
* @expectedException \Respect\Validation\Exceptions\KeyValueException
|
2015-10-08 18:54:42 +02:00
|
|
|
* @expectedExceptionMessage "bar" must be valid to validate "foo"
|
2018-07-23 20:35:14 +02:00
|
|
|
*
|
|
|
|
* @test
|
2015-10-08 18:54:42 +02:00
|
|
|
*/
|
2018-07-23 20:35:14 +02:00
|
|
|
public function shouldNotAssertWhenRuleIsNotValid(): void
|
2015-10-08 18:54:42 +02:00
|
|
|
{
|
|
|
|
$rule = new KeyValue('foo', 'probably_not_a_rule', 'bar');
|
2015-10-18 03:44:47 +02:00
|
|
|
$rule->assert(['foo' => 43, 'bar' => 42]);
|
2015-10-08 18:54:42 +02:00
|
|
|
}
|
|
|
|
|
2018-01-28 16:14:00 +01:00
|
|
|
/**
|
|
|
|
* @doesNotPerformAssertions
|
2018-07-23 20:35:14 +02:00
|
|
|
*
|
|
|
|
* @test
|
2018-01-28 16:14:00 +01:00
|
|
|
*/
|
2018-07-23 20:35:14 +02:00
|
|
|
public function shouldCheckWhenDefinedValuesMatch(): void
|
2015-10-08 18:54:42 +02:00
|
|
|
{
|
|
|
|
$rule = new KeyValue('foo', 'equals', 'bar');
|
2018-01-28 16:14:00 +01:00
|
|
|
$rule->check(['foo' => 42, 'bar' => 42]);
|
2015-10-08 18:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-19 19:01:31 +01:00
|
|
|
* @expectedException \Respect\Validation\Exceptions\EqualsException
|
2016-08-26 10:53:29 +02:00
|
|
|
* @expectedExceptionMessage foo must equal "bar"
|
2018-07-23 20:35:14 +02:00
|
|
|
*
|
|
|
|
* @test
|
2015-10-08 18:54:42 +02:00
|
|
|
*/
|
2018-07-23 20:35:14 +02:00
|
|
|
public function shouldCheckWhenDefinedValuesDoesNotMatch(): void
|
2015-10-08 18:54:42 +02:00
|
|
|
{
|
|
|
|
$rule = new KeyValue('foo', 'equals', 'bar');
|
2015-10-18 03:44:47 +02:00
|
|
|
$rule->check(['foo' => 43, 'bar' => 42]);
|
2015-10-08 18:54:42 +02:00
|
|
|
}
|
|
|
|
}
|