Skip to content

Commit 115db64

Browse files
committed
feat():Add code
1 parent da82ffd commit 115db64

File tree

10 files changed

+723
-1
lines changed

10 files changed

+723
-1
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ composer.phar
33

44
# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
55
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
6-
# composer.lock
6+
composer.lock
7+
8+
/.idea
9+
/.phpunit.result.cache

composer.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "alezhu/phpunit-array-contains-asserts",
3+
"type": "library",
4+
"description": "Provides PHPUnit assertions to test array contains data or structure",
5+
"keywords": [
6+
"php",
7+
"phpunit",
8+
"phpunit-extension",
9+
"phpunit-assertions"
10+
],
11+
"license": "MIT",
12+
"authors": [
13+
{
14+
"name": "Alexandr Zhuravlev",
15+
"homepage": "https://github.com/alezhu"
16+
}
17+
],
18+
"support": {
19+
"docs": "https://github.com/alezhu/phpunit-array-contains-asserts",
20+
"issues": "https://github.com/alezhu/phpunit-array-contains-assertsissues",
21+
"source": "https://github.com/alezhu/phpunit-array-contains-asserts"
22+
},
23+
"autoload": {
24+
"psr-4": {
25+
"Alezhu\\PHPUnitArrayContainsAsserts\\": "src"
26+
}
27+
},
28+
"autoload-dev": {
29+
"psr-4": {
30+
"Alezhu\\PHPUnitArrayContainsAsserts\\Tests\\": "tests"
31+
}
32+
},
33+
"require": {
34+
"php": ">=7.2"
35+
},
36+
"require-dev": {
37+
"phpunit/phpunit": "^8"
38+
}
39+
}

src/ArrayContainsTrait.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Alezhu\PHPUnitArrayContainsAsserts;
4+
5+
use Alezhu\PHPUnitArrayContainsAsserts\Constraint\ArrayContains;
6+
use Alezhu\PHPUnitArrayContainsAsserts\Constraint\ArrayContainsOnly;
7+
use ArrayAccess;
8+
use Iterator;
9+
use PHPUnit\Framework\Assert as PhpUnitAssert;
10+
use PHPUnit\Framework\InvalidArgumentException;
11+
12+
trait ArrayContainsTrait
13+
{
14+
public static function assertArrayContains($subset, $array, bool $strict = false, string $message = '')
15+
{
16+
if (!(is_array($subset) || $subset instanceof ArrayAccess || $subset instanceof Iterator)) {
17+
throw InvalidArgumentException::create(
18+
1,
19+
'array or ArrayAccess or Iterator'
20+
);
21+
}
22+
if (!(is_array($array) || $array instanceof ArrayAccess || $array instanceof Iterator)) {
23+
throw InvalidArgumentException::create(
24+
2,
25+
'array or ArrayAccess or Iterator'
26+
);
27+
}
28+
29+
$constraint = new ArrayContains($subset, $strict);
30+
PhpUnitAssert::assertThat($array, $constraint, $message);
31+
}
32+
33+
public static function assertArrayContainsOnly($subset, $array, bool $strict = false, string $message = '')
34+
{
35+
if (!(is_array($subset) || $subset instanceof ArrayAccess || $subset instanceof Iterator)) {
36+
throw InvalidArgumentException::create(
37+
1,
38+
'array or ArrayAccess or Iterator'
39+
);
40+
}
41+
if (!(is_array($array) || $array instanceof ArrayAccess || $array instanceof Iterator)) {
42+
throw InvalidArgumentException::create(
43+
2,
44+
'array or ArrayAccess or Iterator'
45+
);
46+
}
47+
48+
$constraint = new ArrayContainsOnly($subset, $strict);
49+
PhpUnitAssert::assertThat($array, $constraint, $message);
50+
}
51+
}

src/Assert.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Alezhu\PHPUnitArrayContainsAsserts;
4+
5+
final class Assert
6+
{
7+
use ArrayContainsTrait;
8+
}
9+

src/Constraint/ArrayContains.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace Alezhu\PHPUnitArrayContainsAsserts\Constraint;
4+
5+
use PHPUnit\Framework\AssertionFailedError;
6+
use PHPUnit\Framework\Constraint\Constraint;
7+
use PHPUnit\Framework\Constraint\IsEqual;
8+
use PHPUnit\Framework\Constraint\IsIdentical;
9+
use SebastianBergmann\Comparator\ComparisonFailure;
10+
11+
12+
class ArrayContains extends ArrayContainsBase
13+
{
14+
protected $other_array;
15+
16+
public function toString(): string
17+
{
18+
return 'is contains expected values or key-value pairs';
19+
}
20+
21+
protected function _matchAssoc($other): bool
22+
{
23+
foreach ($this->subset as $key => $value) {
24+
if (!array_key_exists($key, $this->other_array)) {
25+
$failure = new ComparisonFailure($this->subset, $other, var_export($this->subset, true), var_export($other, true));
26+
$this->fail($other, "Actual data not contains some expected keys: ($key)", $failure);
27+
}
28+
29+
$actual = $this->other_array[$key];
30+
$constraint = $value instanceof Constraint ? $value : ($this->strict ? new IsIdentical($value) : new IsEqual($value));
31+
$valid = $constraint->evaluate($actual, '', true);
32+
if (!$valid) {
33+
$failure = new ComparisonFailure($this->subset, $other, var_export($this->subset, true), var_export($other, true));
34+
$this->fail($other, "Actual data contains unexpected values: ($key)", $failure);
35+
}
36+
}
37+
return true;
38+
}
39+
40+
protected function _matchUnAssoc($other): bool
41+
{
42+
$flip_fail = false;
43+
set_error_handler(function () use (&$flip_fail) {
44+
$flip_fail = true;
45+
}, E_WARNING);
46+
$assoc_array = array_flip($this->other_array);
47+
restore_error_handler();
48+
49+
foreach ($this->subset as $value) {
50+
if (
51+
(!$flip_fail && !array_key_exists($value, $assoc_array))
52+
||
53+
($flip_fail && !in_array($value, $this->other_array, $this->strict))
54+
) {
55+
$failure = new ComparisonFailure($this->subset, $other, var_export($this->subset, true), var_export($other, true));
56+
$this->fail($other, "Actual data not contains some values: ($value)", $failure);
57+
}
58+
}
59+
return true;
60+
}
61+
62+
protected function matches($other): bool
63+
{
64+
if ($this->_isAssocArray($other)) {
65+
if (!$this->_isAssocArray($this->subset)) {
66+
throw new AssertionFailedError('Actual data is an associative array, but expected data is not');
67+
}
68+
$this->other_array = $this->_toArray($other);
69+
return $this->_matchAssoc($other);
70+
} else {
71+
if ($this->_isAssocArray($this->subset)) {
72+
throw new AssertionFailedError('Actual data is not an associative array, but expected data is that');
73+
}
74+
$this->other_array = $this->_toArray($other);
75+
return $this->_matchUnAssoc($other);
76+
}
77+
}
78+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
namespace Alezhu\PHPUnitArrayContainsAsserts\Constraint;
4+
5+
use ArrayAccess;
6+
use ArrayObject;
7+
use Countable;
8+
use Iterator;
9+
use PHPUnit\Framework\AssertionFailedError;
10+
use PHPUnit\Framework\Constraint\Constraint;
11+
12+
if (!function_exists('array_is_list')) {
13+
function array_is_list($arr): bool
14+
{
15+
if ($arr === []) {
16+
return true;
17+
}
18+
return array_keys($arr) === range(0, count($arr) - 1);
19+
}
20+
}
21+
22+
abstract class ArrayContainsBase extends Constraint
23+
{
24+
/**
25+
* @var iterable|array
26+
*/
27+
protected $subset;
28+
29+
/**
30+
* @var bool
31+
*/
32+
protected $strict;
33+
34+
/**
35+
* @param array $subset
36+
* @param bool $strict
37+
*/
38+
public function __construct(array $subset, bool $strict = false)
39+
{
40+
$this->subset = $subset;
41+
$this->strict = $strict;
42+
}
43+
44+
protected function _isAssocArray($array): bool
45+
{
46+
if (is_array($array)) {
47+
return !array_is_list($array);
48+
}
49+
if ($array instanceof ArrayAccess) {
50+
if ($array instanceof Countable) {
51+
$count = $array->count();
52+
for ($index = 0; $index < $count; ++$index) {
53+
if (!$array->offsetExists($index)) return true;
54+
}
55+
return false;
56+
} else {
57+
throw new AssertionFailedError('Not supported type');
58+
}
59+
}
60+
if ($array instanceof Iterator) {
61+
$index = 0;
62+
for ($array->rewind(); $array->valid(); $array->next()) {
63+
if ($array->key() !== $index++) return true;
64+
}
65+
return false;
66+
}
67+
68+
throw new AssertionFailedError('Not supported type');
69+
}
70+
71+
protected function _toArray($value)
72+
{
73+
if (is_array($value)) return $value;
74+
if ($value instanceof ArrayObject) {
75+
return $value->getArrayCopy();
76+
}
77+
78+
if ($value instanceof Iterator) {
79+
return iterator_to_array($value);
80+
}
81+
82+
if ($value instanceof ArrayAccess) {
83+
if ($value instanceof Countable) {
84+
$count = $value->count();
85+
$result = [];
86+
for ($index = 0; $index < $count; ++$index) {
87+
$result[$index] = $value->offsetGet($index);
88+
}
89+
return $result;
90+
}
91+
}
92+
throw new AssertionFailedError('Not supported type');
93+
}
94+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Alezhu\PHPUnitArrayContainsAsserts\Constraint;
4+
5+
use SebastianBergmann\Comparator\ComparisonFailure;
6+
7+
class ArrayContainsOnly extends ArrayContains
8+
{
9+
protected function _matchAssoc($other): bool
10+
{
11+
$result = parent::_matchAssoc($other);
12+
if ($result) {
13+
$subset = $this->_toArray($this->subset);
14+
foreach ($this->other_array as $key => $value) {
15+
if (!array_key_exists($key, $subset)) {
16+
$failure = new ComparisonFailure($this->subset, $other, var_export($this->subset, true), var_export($other, true));
17+
$this->fail($other, "Actual data contains unexpected keys: ($key)", $failure);
18+
};
19+
}
20+
}
21+
return $result;
22+
}
23+
24+
protected function _matchUnAssoc($other): bool
25+
{
26+
$result = parent::_matchUnAssoc($other);
27+
if ($result) {
28+
$subset = $this->_toArray($this->subset);
29+
$flip_fail = false;
30+
set_error_handler(function () use (&$flip_fail) {
31+
$flip_fail = true;
32+
}, E_WARNING);
33+
$assoc_array = array_flip($subset);
34+
restore_error_handler();
35+
36+
foreach ($other as $value) {
37+
if (
38+
(!$flip_fail && !array_key_exists($value, $assoc_array))
39+
||
40+
($flip_fail && !in_array($value, $subset, $this->strict))
41+
) {
42+
$failure = new ComparisonFailure($this->subset, $other, var_export($this->subset, true), var_export($other, true));
43+
$this->fail($other, "Actual data contains unexpected values: ($value)", $failure);
44+
}
45+
}
46+
}
47+
return $result;
48+
}
49+
50+
public function toString(): string
51+
{
52+
return 'is contains only expected values or key-value pairs';
53+
}
54+
55+
56+
}

0 commit comments

Comments
 (0)