Brain-WP/BrainMonkey

Provide test case classes for PHPUnit

Closed this issue · 6 comments

In order to use BrainMonkey with PHPUnit I have to create a custom test case class that calls the tearDown() function for each and every project.

Like Mockery does, BrainMonkey could provide these class itself: Brain\Monkey\Integration\PHPUnit\TestCase:

namespace Brain\Monkey\Integration\PhpUnit;

class TestCase extends \PhpUnit\Framework\TestCase
{
    public function tearDown(): void
    {
        Brain\Monkey\tearDown();
        parent::tearDown();
    }

}

And in order to integrate with Mockery itself: Brain\Monkey\Integration\PHPUnit\MockeryTestCase:

namespace Brain\Monkey\Integration\PhpUnit;

class MockeryTestCase extends \Mockery\Adapter\Phpunit\MockeryTestCase
{
    public function tearDown(): void
    {
        Brain\Monkey\tearDown();
        parent::tearDown();
    }

}

This would reduce the redundancy of these test case classes a lot. Another benefit would be to use these classes in your IDE test class templates.

widoz commented

There are situations where you probably want to use, let say https://packagist.org/packages/brain/faker or other tools and probably custom configurations. You'll have to create those classes anyway. Doesn't the classes you suggested just add another layer and much more line of code just to avoid typing two lines?

jrfnl commented

Having just made the tests for BrainMonkey itself compatible with more PHPUnit versions, here are my two cents:

If this gets addressed, it would realistically necessitate the addition of three such TestCase classes:

  1. One TestCase which is compatible with PHPUnit 5 and 6 for testing on PHP 5.6 - 7.0, where the fixture methods don't have type declarations yet.
  2. One TestCase which is compatible with PHPUnit 7, 8 and 9 for testing on PHP 7.1 - 8.0, where the fixture methods would have the void return type declaration.
  3. One Testcase which is cross-version compatible with PHPUnit 5 - 9, which uses different fixture method names and annotations to designate those methods as the fixture methods anyway. (like done for the BrainMonkey native tests in e38e122 )

Depending on the supported PHP versions for the project which uses BrainMonkey, the project would then have to decide which is the correct test case for them to use.

Just saying.

The ticket is just a suggestion for a convenient helper for everyday work. I don't see why making life a bit easier for supported PHP and PHPUnit versions versions implies that this optional helper has also support outdated versions.

@widoz It is all about «don't repeat yourself». After writing these two lines (which are actually a class in the project's namespace all your project's test cases needs to inherit from) over and over again, I came up with https://github.com/inpsyde/monkery-test-case to avoid this annoying step for every library I'm working on (which are a lot). However half of this package is already redundant as Mockery has its on integration to PHPUnit and the other half would make sense to add to BrainMonkey itself. As it reduces maintenance effort.

jrfnl commented

I don't see why making life a bit easier for supported PHP and PHPUnit versions versions implies that this optional helper has also support outdated versions.

Because BrainMonkey supports PHP 5.6 - current, so new features in the public API should as well.

jrfnl commented

FYI: for those people using BrainMonkey in the context of WordPress, you may want to have a look at WP Test Utils, which provides a PHPUnit cross-version compatible TestCase for BrainMonkey based tests (as well as some other utilities for testing in the WP sphere).

Not a fan of this. This add maintenance to the project. Considering the existence of WP Test Utils, @jrfnl I will close this.