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

117 lines
3.3 KiB
PHP
Raw Normal View History

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.
*/
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\Rules\KeyValue
* @covers \Respect\Validation\Exceptions\KeyValueException
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
{
public function testShouldDefineValuesOnConstructor(): void
2015-10-08 18:54:42 +02:00
{
$comparedKey = 'foo';
$ruleName = 'equals';
$baseKey = 'bar';
$rule = new KeyValue($comparedKey, $ruleName, $baseKey);
self::assertSame($comparedKey, $rule->comparedKey);
self::assertSame($ruleName, $rule->ruleName);
self::assertSame($baseKey, $rule->baseKey);
2015-10-08 18:54:42 +02:00
}
public function testShouldNotValidateWhenComparedKeyDoesNotExist(): void
2015-10-08 18:54:42 +02:00
{
$rule = new KeyValue('foo', 'equals', 'bar');
self::assertFalse($rule->validate(['bar' => 42]));
2015-10-08 18:54:42 +02:00
}
public function testShouldNotValidateWhenBaseKeyDoesNotExist(): void
2015-10-08 18:54:42 +02:00
{
$rule = new KeyValue('foo', 'equals', 'bar');
self::assertFalse($rule->validate(['foo' => true]));
2015-10-08 18:54:42 +02:00
}
public function testShouldNotValidateRuleIsNotValid(): void
2015-10-08 18:54:42 +02:00
{
$rule = new KeyValue('foo', 'probably_not_a_rule', 'bar');
self::assertFalse($rule->validate(['foo' => true, 'bar' => false]));
2015-10-08 18:54:42 +02:00
}
public function testShouldValidateWhenDefinedValuesMatch(): void
2015-10-08 18:54:42 +02:00
{
$rule = new KeyValue('foo', 'equals', 'bar');
self::assertTrue($rule->validate(['foo' => 42, 'bar' => 42]));
2015-10-08 18:54:42 +02:00
}
public function testShouldValidateWhenDefinedValuesDoesNotMatch(): void
2015-10-08 18:54:42 +02:00
{
$rule = new KeyValue('foo', 'equals', 'bar');
self::assertFalse($rule->validate(['foo' => 43, 'bar' => 42]));
2015-10-08 18:54:42 +02:00
}
public function testShouldAssertWhenDefinedValuesMatch(): void
2015-10-08 18:54:42 +02:00
{
$rule = new KeyValue('foo', 'equals', 'bar');
self::assertTrue($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
*/
public function testShouldAssertWhenDefinedValuesDoesNotMatch(): 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"
*/
public function testShouldNotAssertWhenRuleIsNotValid(): 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
}
public function testShouldCheckWhenDefinedValuesMatch(): void
2015-10-08 18:54:42 +02:00
{
$rule = new KeyValue('foo', 'equals', 'bar');
self::assertTrue($rule->check(['foo' => 42, 'bar' => 42]));
2015-10-08 18:54:42 +02:00
}
/**
* @expectedException \Respect\Validation\Exceptions\EqualsException
2016-08-26 10:53:29 +02:00
* @expectedExceptionMessage foo must equal "bar"
2015-10-08 18:54:42 +02:00
*/
public function testShouldCheckWhenDefinedValuesDoesNotMatch(): 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
}
}