2017 © Pedro Peláez
 

library counterpart

A set of generic matchers -- for seeing if one value matches another

image

counterpart/counterpart

A set of generic matchers -- for seeing if one value matches another

  • Friday, August 1, 2014
  • by chrisguitarguy
  • Repository
  • 1 Watchers
  • 2 Stars
  • 8,357 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 2 Open issues
  • 9 Versions
  • 0 % Grown

The README.md

Counterpart

Build Status, (*1)

Counterpart is an object matching framework for PHP inspired by PHPUnit's constraints and Hamcrest., (*2)

Where to Use?

As part of a testing framework or someplace you'd need to assert a given value matches some expected constraint., (*3)

Examples

Two classes act as factories and are, by the far, the easiest way to get started., (*4)

You can see some pretty high-level usage of of the Assert and Matchers API., (*5)

Creating Matchers

use Counterpart\Matchers;

$isTrue = Matchers::isTrue();
$isTrue->matches(false); // false
$isTrue->matches(true); // true

Assertions

All of the assert* methods in Counterpart\Assert will throw a Counterpart\Exception\AssertionFailed exception., (*6)

use Counterpart\Assert;

Assert::assertMatchesRegex(
    '/here/',
    'nope nope nope',
    "some message about business results here so you understand why it's important"
);

Creating Custom Matchers

One of the strengths of Counterpart is that it's very easy to create new, custom Matcher implementations. Doing so let's you clarify your business logic and create more readable code., (*7)

For example: let's say we needed to assert that the value returned from a number between 1 and 100., (*8)

This could be done with a LogicalAnd matcher and a few other matchers., (*9)

use Counterpart\Assert;
use Counterpart\Matchers;

Assert::assertThat(
    Matchers::logicalAnd(
        Matchers::isType('integer'),
        Matchers::greaterThanOrEqual(1),
        Matchers::lessThanOrEqual(100)
    )
    $theRealNumber
);

There's absolutely nothing wrong with that. It's a bit wordy, especially if you have to write it out a lot. And if you do have to write it a lot, there's a good chance it's because it's part of some core business logic. Maybe you hit an API that only accepts values between 1 and 100., (*10)

This is great use case for a custom matcher., (*11)

use Counterpart\Matcher;
use Counterpart\Assert;

class ValidApiValueMatcher implements Matcher
{
    /**
     * {@inheritdoc}
     */
    public function matches($actual)
    {
        return is_int($actual) && $actual >= 1 && $actual <= 100;
    }

    /**
     * {@inheritdoc}
     */
    public function __toString()
    {
        return 'is a valid API value: an integer between 1 and 100';
    }
}

Assert::assertThat(new ValidApiValueMatcher(), $theActualValue);

This is now a nicely self contained, reusable piece of code, the name of which reinforces it's purpose., (*12)

License

Apache 2.0 -- please see the LICENSE file., (*13)

The Versions

01/08 2014

1.x-dev

1.9999999.9999999.9999999-dev http://docs.counterpartphp.org

A set of generic matchers -- for seeing if one value matches another

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

testing assert matcher

01/08 2014

dev-master

9999999-dev http://docs.counterpartphp.org

A set of generic matchers -- for seeing if one value matches another

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

testing assert matcher

30/07 2014

1.5.0

1.5.0.0 http://docs.counterpartphp.org

A set of generic matchers -- for seeing if one value matches another

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

testing assert matcher

23/06 2014

1.4.0

1.4.0.0 http://docs.counterpartphp.org

A set of generic matchers -- for seeing if one value matches another

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

testing assert matcher

01/06 2014

1.3.0

1.3.0.0

A set of generic matchers -- for seeing if one value matches another

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

20/05 2014

1.2

1.2.0.0

A set of generic matchers -- for seeing if one value matches another

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

17/05 2014

1.1.1

1.1.1.0

A set of generic matchers -- for seeing if one value matches another

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

17/05 2014

1.1.0

1.1.0.0

A set of generic matchers -- for see if one value matches another

  Sources   Download

The Requires

 

The Development Requires

05/05 2014

1.0.0

1.0.0.0

A set of generic matchers -- for see if one value matches another

  Sources   Download

The Requires

 

The Development Requires