dms / phpunit-arraysubset-asserts
This package provides ArraySubset and related asserts once deprecated in PHPUnit 8
Installs: 19 877 465
Dependents: 303
Suggesters: 0
Security: 0
Stars: 135
Watchers: 11
Forks: 23
Open Issues: 7
Requires
- php: ^5.4 || ^7.0 || ^8.0
- phpunit/phpunit: ^4.8.36 || ^5.7.21 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0
Requires (Dev)
- dev-master
- v0.5.0
- v0.4.0
- v0.3.1
- v0.3.0
- v0.2.1
- v0.2.0
- v0.1.1
- v0.1.0
- dev-feature/ghactions-update-for-php-8.4-release
- dev-dependabot/composer/phpunit/phpunit-tw-4.8.36or-tw-5.7.21or-tw-6.0or-tw-7.0or-tw-8.0or-tw-9.0or-tw-10.0or-tw-11.0
- dev-task/coding-standard
- dev-dependabot/composer/dms/coding-standard-tw-9or-tw-12
- dev-test/phpunit10
- dev-ci/merge-me
- dev-feature/add-support-for-phpunit-phar
This package is auto-updated.
Last update: 2024-11-22 00:48:57 UTC
README
In PHPUnit 8 the function assertArraySubset
was deprecated. This function was often misunderstood and thus removed, but it still holds true as a very useful tool, hence it was extracted here.
Disclaimer: The initial version contained here is copied over from phpunit and is heavily based on the original work by Márcio Almada.
Installation
Simply use it by importing it with Composer
composer require --dev dms/phpunit-arraysubset-asserts
💡 The package can be safely required on PHP 5.4 to current in combination with PHPUnit 4.8.36/5.7.21 to current.
When the PHPUnit
assertArraySubset()
method is natively available and not deprecated (PHPUnit 4.x - 7.x), the PHPUnit native functionality will be used. For PHPUnit 8 and higher, the extension will kick in and polyfill the functionality which was removed from PHPUnit.
Usage
You have two options to use this in your classes: either directly as a static call or as a trait if you wish to keep existing references working.
Trait use example
<?php namespace Your\Package\Tests; use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts; use PHPUnit\Framework\TestCase; final class ExampleTest extends TestCase { use ArraySubsetAsserts; public function testWithTrait(): void { $expectedSubset = ['bar' => 0]; $content = ['bar' => '0']; self::assertArraySubset($expectedSubset, $content, true); $content = ['foo' => '1']; $this->assertArraySubset($expectedSubset, $content, true); } }
Static class method example
<?php namespace Your\Package\Tests; use DMS\PHPUnitExtensions\ArraySubset\Assert; use PHPUnit\Framework\TestCase; final class ExampleTest extends TestCase { public function testWithDirectCall(): void { $expectedSubset = ['bar' => 0]; $content = ['bar' => '0']; Assert::assertArraySubset($expectedSubset, $content, true); } }