-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to make assertions with "max" prefix
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
- Loading branch information
1 parent
ebd53d7
commit 6e4026d
Showing
5 changed files
with
319 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Respect/Assertion. | ||
* | ||
* (c) Henrique Moody <henriquemoody@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Respect\Assertion\Assertor; | ||
|
||
use BadMethodCallException; | ||
use Respect\Assertion\Assertion; | ||
use Respect\Assertion\Assertor; | ||
use Respect\Validation\Exceptions\ValidationException; | ||
use function is_array; | ||
use function is_iterable; | ||
use function iterator_to_array; | ||
use function max; | ||
use function str_replace; | ||
|
||
final class MaxAssertor implements Assertor | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getName(): string | ||
{ | ||
return 'max'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function execute(Assertion $assertion, $input): void | ||
{ | ||
if (!is_iterable($input)) { | ||
throw new BadMethodCallException('Assertion with "max" prefix must be iterable'); | ||
} | ||
|
||
try { | ||
$assertion->assert($this->getMax($input)); | ||
} catch (ValidationException $exception) { | ||
throw $this->getCustomizedException($input, $exception); | ||
} | ||
} | ||
|
||
private function getMax(iterable $input) | ||
{ | ||
if (is_array($input)) { | ||
return max($input); | ||
} | ||
|
||
return $this->getMax(iterator_to_array($input)); | ||
} | ||
|
||
private function getCustomizedException($asserted, ValidationException $exception): ValidationException | ||
{ | ||
$exception->setParam('asserted', $asserted); | ||
if ($exception->hasCustomTemplate()) { | ||
return $exception; | ||
} | ||
|
||
$exception->setTemplate( | ||
str_replace( | ||
'{{name}}', '{{name}}, the maximum of {{asserted}},', | ||
$exception->getTemplate() | ||
) | ||
); | ||
|
||
return $exception; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Respect/Assertion. | ||
* | ||
* (c) Henrique Moody <henriquemoody@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Respect\Test\Unit\Assertion\Assertor; | ||
|
||
use ArrayObject; | ||
use BadMethodCallException; | ||
use DomainException; | ||
use PHPUnit\Framework\TestCase; | ||
use Respect\Assertion\Assertion; | ||
use Respect\Assertion\Assertor\MaxAssertor; | ||
use Respect\Validation\Exceptions\AlwaysInvalidException; | ||
use function range; | ||
|
||
/** | ||
* @covers \Respect\Assertion\Assertor\MaxAssertor | ||
* | ||
* @author Henrique Moody <henriquemoody@gmail.com> | ||
*/ | ||
final class MaxAssertorTest extends TestCase | ||
{ | ||
/** | ||
* @var MaxAssertor | ||
*/ | ||
private $sut; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function setUp(): void | ||
{ | ||
$this->sut = new MaxAssertor(); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function itShouldReturnTheAssertorName(): void | ||
{ | ||
self::assertEquals('max', $this->sut->getName()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function itShouldThrowAnExceptionWhenInputIsNotIterable(): void | ||
{ | ||
$this->expectException(BadMethodCallException::class); | ||
|
||
$this->sut->execute($this->createMock(Assertion::class), 42); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function itShouldAssertTheMaxValueOfTheInputWhenItIsAnArray(): void | ||
{ | ||
$input = range(100, 105); | ||
|
||
$assertion = $this->createMock(Assertion::class); | ||
$assertion | ||
->expects($this->once()) | ||
->method('assert') | ||
->with(105); | ||
|
||
$this->sut->execute($assertion, $input); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function itShouldAssertTheMaxValueOfTheInputWhenItIsAnIterableValue(): void | ||
{ | ||
$input = new ArrayObject(range(100, 200)); | ||
|
||
$assertion = $this->createMock(Assertion::class); | ||
$assertion | ||
->expects($this->once()) | ||
->method('assert') | ||
->with(200); | ||
|
||
$this->sut->execute($assertion, $input); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function itShouldThrowCustomExceptionsWhenAssertionFails(): void | ||
{ | ||
$input = [1, 2, 3]; | ||
|
||
$exception = new DomainException('Custom exception'); | ||
|
||
$assertion = $this->createMock(Assertion::class); | ||
$assertion | ||
->expects($this->once()) | ||
->method('assert') | ||
->with(3) | ||
->willThrowException($exception); | ||
|
||
$this->expectExceptionObject($exception); | ||
|
||
$this->sut->execute($assertion, $input); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function itShouldThrowAndModifyValidationExceptionsWhenAssertionFails(): void | ||
{ | ||
$input = [1, 2, 3]; | ||
|
||
$exception = new AlwaysInvalidException(); | ||
$exception->configure('something'); | ||
|
||
$assertion = $this->createMock(Assertion::class); | ||
$assertion | ||
->expects($this->once()) | ||
->method('assert') | ||
->with(3) | ||
->willThrowException($exception); | ||
|
||
self::assertEquals('something is always invalid', $exception->getMessage()); | ||
|
||
$this->expectException(AlwaysInvalidException::class); | ||
$this->expectExceptionMessage('something, the maximum of { 1, 2, 3 }, is always invalid'); | ||
|
||
$this->sut->execute($assertion, $input); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function itShouldThrowAndNotModifyValidationExceptionsWhenAssertionFailsAndExceptionHasCustomTemplate(): void | ||
{ | ||
$input = [1, 2, 3]; | ||
|
||
$exception = new AlwaysInvalidException(); | ||
$exception->configure('something'); | ||
$exception->setTemplate('{{input}} is something not cool'); | ||
|
||
$assertion = $this->createMock(Assertion::class); | ||
$assertion | ||
->expects($this->once()) | ||
->method('assert') | ||
->with(3) | ||
->willThrowException($exception); | ||
|
||
$this->expectExceptionObject($exception); | ||
|
||
$this->sut->execute($assertion, $input); | ||
} | ||
} |