Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ jobs:

strategy:
matrix:
php: [8.*, 7.4, 7.3]
php: [8.x, 7.4, 7.3]
dependency-version: [prefer-lowest, prefer-stable]

name: P${{ matrix.php }} - ${{ matrix.dependency-version }}
@@ -34,7 +34,7 @@ jobs:
php-version: ${{ matrix.php }}
coverage: none
tools: composer

- name: Install dependencies
run: |
composer update --${{ matrix.dependency-version }} --prefer-dist --no-progress
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -4,7 +4,8 @@
"type": "library",
"require": {
"phpunit/phpunit": "^9.0",
"php": "^7.3|^8.0"
"php": "^7.3|^7.4|^8.0",
"dms/coding-standard": "^8"
},
"license": "MIT",
"authors": [
@@ -14,7 +15,6 @@
}
],
"require-dev": {
"dms/coding-standard": "^1.0",
"squizlabs/php_codesniffer": "^3.4"
},
"autoload": {
1,000 changes: 607 additions & 393 deletions composer.lock

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -10,9 +10,11 @@
<file>src</file>
<file>tests</file>

<rule ref="DMS"/>
<rule ref="DMS">
<exclude name="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint"/>
</rule>

<rule ref="SlevomatCodingStandard.ControlStructures.DisallowEqualOperators.DisallowedEqualOperator">
<rule ref="SlevomatCodingStandard.Operators.DisallowEqualOperators.DisallowedEqualOperator">
<exclude-pattern>src/Constraint/ArraySubset.php</exclude-pattern>
</rule>
</ruleset>
4 changes: 4 additions & 0 deletions src/ArrayAccessible.php
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@
use ArrayIterator;
use IteratorAggregate;
use Traversable;

use function array_key_exists;

class ArrayAccessible implements ArrayAccess, IteratorAggregate
@@ -63,6 +64,9 @@ public function offsetUnset($offset): void
unset($this->array[$offset]);
}

/**
* @return mixed[]
*/
public function getIterator(): Traversable
{
return new ArrayIterator($this->array);
3 changes: 3 additions & 0 deletions src/ArraySubsetAsserts.php
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@
use PHPUnit\Framework\Assert as PhpUnitAssert;
use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\InvalidArgumentException;

use function is_array;

trait ArraySubsetAsserts
@@ -31,12 +32,14 @@ public static function assertArraySubset($subset, $array, bool $checkForObjectId
'array or ArrayAccess'
);
}

if (! (is_array($array) || $array instanceof ArrayAccess)) {
throw InvalidArgumentException::create(
2,
'array or ArrayAccess'
);
}

$constraint = new ArraySubset($subset, $checkForObjectIdentity);
PhpUnitAssert::assertThat($array, $constraint, $message);
}
10 changes: 9 additions & 1 deletion src/Constraint/ArraySubset.php
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@
use SebastianBergmann\Comparator\ComparisonFailure;
use SebastianBergmann\RecursionContext\InvalidArgumentException;
use Traversable;

use function array_replace_recursive;
use function is_array;
use function iterator_to_array;
@@ -27,6 +28,7 @@ final class ArraySubset extends Constraint
* @var iterable|mixed[]
*/
private $subset;

/**
* @var bool
*/
@@ -52,7 +54,8 @@ public function __construct(iterable $subset, bool $strict = false)
* failure.
*
* @param mixed[]|ArrayAccess $other
* @return mixed[]|null|bool
*
* @return mixed[]|bool|null
*
* @throws ExpectationFailedException
* @throws InvalidArgumentException
@@ -69,9 +72,11 @@ public function evaluate($other, string $description = '', bool $returnResult =
} else {
$result = $other == $patched;
}

if ($returnResult) {
return $result;
}

if ($result) {
return null;
}
@@ -120,12 +125,15 @@ private function toArray(iterable $other): array
if (is_array($other)) {
return $other;
}

if ($other instanceof ArrayObject) {
return $other->getArrayCopy();
}

if ($other instanceof Traversable) {
return iterator_to_array($other);
}

// Keep BC even if we know that array would not be the expected one
return (array) $other;
}
5 changes: 3 additions & 2 deletions tests/Unit/Constraint/ArraySubsetTest.php
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@
use ReflectionClass;
use SebastianBergmann\RecursionContext\InvalidArgumentException;
use Traversable;

use function sprintf;

final class ArraySubsetTest extends TestCase
@@ -53,13 +54,13 @@ public static function evaluateDataProvider(): array
/**
* @param array|Traversable|mixed[] $subset
* @param array|Traversable|mixed[] $other
* @param bool $strict
*
* @throws ExpectationFailedException
* @throws InvalidArgumentException
*
* @dataProvider evaluateDataProvider
*/
public function testEvaluate(bool $expected, $subset, $other, $strict): void
public function testEvaluate(bool $expected, $subset, $other, bool $strict): void
{
$constraint = new ArraySubset($subset, $strict);