Giter VIP home page Giter VIP logo

phpunit-arraysubset-asserts's Introduction

PHPUnit AssertArraySubset Extension

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);
    }
}

phpunit-arraysubset-asserts's People

Contributors

barryvdh avatar bcremer avatar colinodell avatar deleugpn avatar dependabot[bot] avatar dereuromark avatar jrfnl avatar mnapoli avatar rdarcy1 avatar rdohms avatar winkbrace avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

phpunit-arraysubset-asserts's Issues

Support for PHPUnit 11

Version 11 has been released so we should add compatibility.

Possibly its as easy as just allowing ^11.0 without any further changes? 🤔 I can take a look.

Allow empty subset arrays

I have several integration tests where I have to test an array is a subset of another array. These tests are implemented in a generic way for a specific case. The test is fed by data providers. The mentioned subset arrays can be empty in several data sets of the data provider.

Actually your library doesn't support empty subsets and throws an exception. So I could have an conditional assert on "if subset !== empty then assertArraySubset()". But I'd prefer the possibility to test for empty subsets while the subset should always be assumed as known if it's empty or whatever values it contains by the writer of the test. So an error on an empty subset restricts the testing abilities.

Edit

This discovery makes it more a bug report over a feature request.

If I test an empty subset array against an empty array no exception will be thrown which makes its behaviour inconsequent.

PHPUnit 10 Compatibility

Is there a timeline that can be expected for PHPUnit ^10.0 compatibility? Currently, we use Pest for unit testing and Pest 2.0 requires phpunit ^10.0 and this library doesn't support that yet. Is there a technical thing maybe I can lend a hand to, or is it as simple as the requirements have not been updated?

I think that PHPUnit 8.5.2 introduced changes that broke phpunit-arraysubset-asserts

Hi,

When my test (which uses Assert::assertArraySubset()) fails (does not pass), I get the following error:

TypeError: Argument 3 passed to PHPUnit\Framework\Constraint\Constraint::fail() must be an instance of PHPUnit\SebastianBergmann\Comparator\ComparisonFailure or null, instance of SebastianBergmann\Comparator\ComparisonFailure given, called in /Users/leo/sandbox/div/marvin/compigram/vendor/dms/phpunit-arraysubset-asserts/src/Constraint/ArraySubset.php on line 85

I'm able to fix this error by changing all occurrences of:

use SebastianBergmann\...

to:

use PHPUnit\SebastianBergmann\...

There are two (2) such "use" statements in Constraint\ArraySubset.php, and one (1) in ArraySubsetAsserts.

I'm running PHPUnit version 8.5.2, and my test code is as follows:

use DMS\PHPUnitExtensions\ArraySubset\Assert;

Assert::assertArraySubset([.........], $myArray);

I guess that it's PHPUnit 8.5.x which introduced a breaking change in these package names.

Major necessary?

Just a small question, not necessarily something wrong:

We are using your library and follow semver 2.0.
Was there any BC break or what was the reason for 0.3 release after 0.2?
As that is a (semantic) major release and thus will not work to pull without a major constraint update on our side.
Or in other words: 0.2.2 as semantic minor wouldn't have worked?
Thank you

Either way, we will update our dependency constraints accordingly.
But if there was indeed a mistake, maybe we can go for more minors then in the future for example.

Does not work on PHPUnit 8

This library makes the (wrong) assumption that we should not want to use it (even though we installed it for some reason) on PHPUnit 8, with its custom autoloader that makes the ArraySubsetAsserts trait do nothing. In that case, PHPUnit 8's implementation will run as normal and issue a deprecation warning, which will also cause the tests to fail if failOnWarning="true" is being used in the PHPUnit config. I should think that if this library is installed, it's a fair bet we want to use it.

Backwards phpunit compatibility

For (app) projects, it's probably fine to upgrade to phpunit 8 and above.

But some php packages will require at least phpunit 7 compatibility though.

I've forked your project and tried downgrading to phpunit 7. Running tests results in these issues:

There was 1 error:

1) DMS\PHPUnitExtensions\ArraySubset\Tests\Unit\Constraint\ArraySubsetTest::testEvaluateFailMessage
Error: Call to undefined method DMS\PHPUnitExtensions\ArraySubset\Constraint\ArraySubset::exporter()

/Users/sander/apps/phpunit-arraysubset-asserts/src/Constraint/ArraySubset.php:95
/Users/sander/apps/phpunit-arraysubset-asserts/src/Constraint/ArraySubset.php:110
/Users/sander/apps/phpunit-arraysubset-asserts/src/Constraint/ArraySubset.php:85
/Users/sander/apps/phpunit-arraysubset-asserts/tests/Unit/Constraint/ArraySubsetTest.php:83

--

There were 2 failures:

1) DMS\PHPUnitExtensions\ArraySubset\Tests\Unit\AssertTest::testAssertArraySubsetPassesStrictConfig
Failed asserting that exception of type "PHPUnit\Framework\Error\Warning" matches expected exception "PHPUnit\Framework\ExpectationFailedException". Message was: "Declaration of DMS\PHPUnitExtensions\ArraySubset\Constraint\ArraySubset::evaluate($other, string $description = '', bool $returnResult = false) should be compatible with PHPUnit\Framework\Constraint\Constraint::evaluate($other, $description = '', $returnResult = false)" at
/Users/sander/apps/phpunit-arraysubset-asserts/src/Constraint/ArraySubset.php:24
/Users/sander/apps/phpunit-arraysubset-asserts/src/ArraySubsetAsserts.php:41
/Users/sander/apps/phpunit-arraysubset-asserts/tests/Unit/AssertTest.php:16
.

2) DMS\PHPUnitExtensions\ArraySubset\Tests\Unit\AssertTest::testAssertArraySubsetThrowsExceptionForInvalidSubset
Failed asserting that exception of type "Error" matches expected exception "PHPUnit\Framework\ExpectationFailedException". Message was: "Call to undefined method DMS\PHPUnitExtensions\ArraySubset\Constraint\ArraySubset::exporter()" at
/Users/sander/apps/phpunit-arraysubset-asserts/src/Constraint/ArraySubset.php:95
/Users/sander/apps/phpunit-arraysubset-asserts/src/Constraint/ArraySubset.php:110
/Users/sander/apps/phpunit-arraysubset-asserts/src/Constraint/ArraySubset.php:85
/Users/sander/apps/phpunit-arraysubset-asserts/src/ArraySubsetAsserts.php:42
/Users/sander/apps/phpunit-arraysubset-asserts/tests/Unit/AssertTest.php:22

Any ideas here?

License

Hi @rdohms,

Thanks for providing this! ❤️

Could you perhaps include a license (my preference goes to MIT)?

Cheers,

Sander

GH Actions: remove cron job or limit to own repo

Hi @rdohms

I noticed that the unit test workflow has a cron schedule to run daily.
I'm not sure why this has been added, but there is a downside to it: workflows which run cron jobs will be automatically disabled after two months of inactivity in a repository.

As this repository is not that active, that means that the run-tests workflow will generally be disabled and would need to be re-enabled whenever any work gets done in this repo or when a PR is submitted to this repo (which will then have to be force pushed again to trigger the build).

It also means that - presuming the last merge build passed - this cron job will only ever fail when:

  • One of the actions runner has changed and broken something;
  • When there is a new PHPUnit release which would break something, but considering this repo is not using the PHPUnit native functionality, this is unlikely;
  • Or when the PHP 8.1 run would fail, but as that one is marked as experimental, it wouldn't fail the build, so you still wouldn't get notified of it.

Also note that cron jobs also run on forks.

Having said all this, there are a couple of directions to go in:

  • Either remove the cron schedule completely.
    This would prevent the workflow from being automatically disabled and preserve resources (x16 - this repo + currently 15 forks).
  • Split the workflow into two duplicate scripts, one for PR CI and one with the cron job.
    If the cron job then gets disabled, at least it would not block PRs from having CI run and getting merged.
  • Add a condition to the script to only run the cron job event for the main repo and not run it on forks.

I'd be happy to send in a PR to fix this, but would like to understand your reason for adding the cron job and know which solution would be preferred before I do so.

Support older versions of PHPUnit

My symfony-based project depends on symfony/phpunit-bridge which download & install phpunit dynamically at runtime.
I'd rather prefer not to pull phpunit/phpunit when requiring phpunit-arraysubset-asserts.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.