Skip to content

Commit

Permalink
Include asserters to equality
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosV2 committed Jun 12, 2017
1 parent 8cdf458 commit cd6505f
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 1 deletion.
5 changes: 5 additions & 0 deletions docs/ConstraintsExtension/equals_to.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Equals to

This asserter validates if the given data matches the given value.

You can use the "equals to" asserter as `To::beEqualsTo($value)`.
5 changes: 5 additions & 0 deletions docs/ConstraintsExtension/like.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Like

This asserter validates if the given data matches the given value without ensuring both values have the same type.

You can use the "like" asserter as `To::beLike($value)`.
33 changes: 33 additions & 0 deletions spec/Extension/ConstraintsExtension/EqualsToAsserterSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace spec\carlosV2\Can\Extension\ConstraintsExtension;

use PhpSpec\ObjectBehavior;

class EqualsToAsserterSpec extends ObjectBehavior
{
function let()
{
$this->beConstructedWith('123');
}

function it_is_an_Asserter()
{
$this->shouldHaveType('carlosV2\Can\AsserterInterface');
}

function it_returns_true_if_both_values_are_equals()
{
$this->check('123')->shouldReturn(true);
}

function it_returns_false_if_the_values_are_equals_but_have_different_type()
{
$this->check(123)->shouldReturn(false);
}

function it_returns_false_if_the_values_are_not_equals()
{
$this->check('321')->shouldReturn(false);
}
}
33 changes: 33 additions & 0 deletions spec/Extension/ConstraintsExtension/LikeAsserterSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace spec\carlosV2\Can\Extension\ConstraintsExtension;

use PhpSpec\ObjectBehavior;

class LikeAsserterSpec extends ObjectBehavior
{
function let()
{
$this->beConstructedWith('123');
}

function it_is_an_Asserter()
{
$this->shouldHaveType('carlosV2\Can\AsserterInterface');
}

function it_returns_true_if_both_values_are_equals()
{
$this->check('123')->shouldReturn(true);
}

function it_returns_true_if_the_values_are_equals_but_have_different_type()
{
$this->check(123)->shouldReturn(true);
}

function it_returns_false_if_the_values_are_not_equals()
{
$this->check('321')->shouldReturn(false);
}
}
4 changes: 3 additions & 1 deletion src/Extension/ConstraintsExtension/ConstraintsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ public function registerAsserters()
return [
'optionalAnd' => 'carlosV2\Can\Extension\ConstraintsExtension\OptionalAndAsserter',
'allOf' => 'carlosV2\Can\Extension\ConstraintsExtension\AllOfAsserter',
'oneOf' => 'carlosV2\Can\Extension\ConstraintsExtension\OneOfAsserter'
'oneOf' => 'carlosV2\Can\Extension\ConstraintsExtension\OneOfAsserter',
'equalsTo' => 'carlosV2\Can\Extension\ConstraintsExtension\EqualsToAsserter',
'like' => 'carlosV2\Can\Extension\ConstraintsExtension\LikeAsserter',
];
}
}
29 changes: 29 additions & 0 deletions src/Extension/ConstraintsExtension/EqualsToAsserter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace carlosV2\Can\Extension\ConstraintsExtension;

use carlosV2\Can\AsserterInterface;

class EqualsToAsserter implements AsserterInterface
{
/**
* @var mixed
*/
private $value;

/**
* @param mixed $value
*/
public function __construct($value)
{
$this->value = $value;
}

/**
* @inheritDoc
*/
public function check($data)
{
return $data === $this->value;
}
}
29 changes: 29 additions & 0 deletions src/Extension/ConstraintsExtension/LikeAsserter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace carlosV2\Can\Extension\ConstraintsExtension;

use carlosV2\Can\AsserterInterface;

class LikeAsserter implements AsserterInterface
{
/**
* @var mixed
*/
private $value;

/**
* @param mixed $value
*/
public function __construct($value)
{
$this->value = $value;
}

/**
* @inheritDoc
*/
public function check($data)
{
return $data == $this->value;
}
}
4 changes: 4 additions & 0 deletions src/To.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use carlosV2\Can\Exception\AsserterNotFoundException;
use carlosV2\Can\Extension\ConstraintsExtension\AllOfAsserter;
use carlosV2\Can\Extension\ConstraintsExtension\EqualsToAsserter;
use carlosV2\Can\Extension\ConstraintsExtension\LikeAsserter;
use carlosV2\Can\Extension\ConstraintsExtension\OneOfAsserter;
use carlosV2\Can\Extension\ConstraintsExtension\OptionalAndAsserter;
use carlosV2\Can\Extension\PrimitiveTypesExtension\ArrayAsserter;
Expand All @@ -29,6 +31,8 @@
* @method static OptionalAndAsserter beOptionalAnd(AsserterInterface $asserter)
* @method static AllOfAsserter beAllOf(AsserterInterface ...$asserters)
* @method static OneOfAsserter beOneOf(AsserterInterface ...$asserters)
* @method static EqualsToAsserter beEqualsTo(mixed $value)
* @method static LikeAsserter beLike(mixed $value)
*/
class To
{
Expand Down

0 comments on commit cd6505f

Please sign in to comment.