PHPSpec Array Contains Matchers
, (*1)
Provides additional contains matchers for arrays in PHPSpec, (*2)
Requirements
The extensions has been built for PHPSpec 3.x and PHP 5.6 or later., (*3)
Installation
Install the extension via composer:, (*4)
$ composer require jameshalsall/phpspec-array-contains-matchers
You can then add the extension to your phpspec.yml
as follows:, (*5)
extensions:
JamesHalsall\PhpSpecContainsMatchers\Extension: ~
Usage
The extension currently supports the following matchers:, (*6)
containOnly
containAnyOf
containAllOf
containOnly
matcher
The containOnly
matcher is used when you want to ensure that an array value contains only the specified arguments.
It was implemented to save you writing multiple calls to shouldContain()
., (*7)
Consider that ->getArray()
returns [1, 2, 3]
, the following code in your spec would pass:, (*8)
$this->getArray()->shouldContainOnly(1, 2, 3);
If any anything else was in the array would it would cause the spec to fail., (*9)
Similarly this can be negated with shouldNotContainOnly
. Staying with the same example above, the following code
would fail because there aren't any additional values in the array:, (*10)
$this->getArray()->shouldNotContainOnly(1, 2, 3);
However if ->getArray()
was to return [1, 2, 3, 4]
then the above spec would pass., (*11)
containAnyOf
matcher
The containAnyOf
matcher is used when you want to ensure that an array contains at least one of the specified arguments., (*12)
Consider that ->getArray()
returns [1, 2, 3]
as above, the following assertion would hold true:, (*13)
$this->getArray()->shouldContainAnyOf(3, 4, 5);
However the following example would not:, (*14)
$this->getArray()->shouldContainAnyOf(5, 6);
Similarly this can be negated with shouldNotContainAnyOf
. Staying with the sample example, the following code would fail:, (*15)
$this->getArray()->shouldNotContainAnyOf(3, 7);
This is because it contains the value 3, which is one of the values we have said it should not contain., (*16)
containAllOf
matcher
The containAllOf
matcher will evaluate to true when the array contains all of the elements passed to the matcher., (*17)
If ->getArray()
returns [1, 2, 3]
then the following assertion would hold true:, (*18)
$this->getArray()->shouldContainAllOf(2, 3);
However the following example would not:, (*19)
$this->getArray()->shouldContainAllOf(2, 3, 4);
Similarly this can be negated with shouldNotContainAnyOf
. Staying with the same example, the following code would fail:, (*20)
$this->getArray()->shouldNotContainAllOf(2, 3)