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

153 lines
3.8 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;
2018-12-05 08:36:38 +01:00
use Respect\Validation\Test\TestCase;
2015-10-08 18:54:42 +02:00
/**
* @group rule
2017-02-04 14:01:14 +01:00
* @covers \Respect\Validation\Exceptions\KeyValueException
* @covers \Respect\Validation\Rules\KeyValue
*
* @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
*/
final class KeyValueTest extends TestCase
2015-10-08 18:54:42 +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);
self::assertAttributeSame($comparedKey, 'comparedKey', $rule);
self::assertAttributeSame($ruleName, 'ruleName', $rule);
self::assertAttributeSame($baseKey, 'baseKey', $rule);
2015-10-08 18:54:42 +02:00
}
/**
* @test
*/
public function shouldNotValidateWhenComparedKeyDoesNotExist(): 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
}
/**
* @test
*/
public function shouldNotValidateWhenBaseKeyDoesNotExist(): 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
}
/**
* @test
*/
public function shouldNotValidateRuleIsNotValid(): 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
}
/**
* @test
*/
public function shouldValidateWhenDefinedValuesMatch(): 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
}
/**
* @test
*/
public function shouldValidateWhenDefinedValuesDoesNotMatch(): 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
}
/**
* @doesNotPerformAssertions
*
* @test
*/
public function shouldAssertWhenDefinedValuesMatch(): void
2015-10-08 18:54:42 +02:00
{
$rule = new KeyValue('foo', 'equals', 'bar');
$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
*
* @test
2015-10-08 18:54:42 +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"
*
* @test
2015-10-08 18:54:42 +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
}
/**
* @doesNotPerformAssertions
*
* @test
*/
public function shouldCheckWhenDefinedValuesMatch(): void
2015-10-08 18:54:42 +02:00
{
$rule = new KeyValue('foo', 'equals', 'bar');
$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"
*
* @test
2015-10-08 18:54:42 +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
}
}