jakzal / phpunit-injector

Injects services from a PSR-11 dependency injection container to PHPUnit test cases
MIT License
63 stars 4 forks source link

Deprecation notices #33

Open gjuric opened 4 years ago

gjuric commented 4 years ago

1x: The "Zalas\Injector\PHPUnit\TestListener\ServiceInjectorListener" class implements "PHPUnit\Framework\TestListener" that is deprecated Use the TestHook interfaces instead.

1x: The "Zalas\Injector\PHPUnit\TestListener\ServiceInjectorListener" class uses "PHPUnit\Framework\TestListenerDefaultImplementation" that is deprecated The TestListener interface is deprecated.

I am getting these deprecation notices with PHPUnit 9.2.2.

jakzal commented 4 years ago

@gjuric yes, I'm afraid this extension will stop working in PHPUnit 10 due to BC incompatible changes in PHPUnit.

gjuric commented 3 years ago

I've been digging around this a little bit and it seems like the TestHooks in PHPUnit 9.5 are currently not a suitable replacement for TestListeners since they provide only the class name of a test being run and not the actual Test.

I've managed to create a PoC that works by putting the code that is currently in ServiceInjectorListener into a class that is extending PHPUnit\Framework\TestListener, running the code that is in ServiceInjectorListener::startTest() in a setUp() method of this class and making my tests extend this.

I am not really a fan of this approach since one has to remember to call parent::setUp() otherwise this will not work.

Do you have any other ideas/plans on how to approach or do you even plan to work on this? Also, do you need any help?

jakzal commented 3 years ago

@gjuric I don't have the capacity to work on this at the moment, so feel free to take over! I'll be happy to review your POC too.

gjuric commented 3 years ago

OK, looking into it. Unfortunately my idea falls apart if you already need to extend another TestCase class (not the original, PHPUnit one). I've opened an issue on https://github.com/sebastianbergmann/phpunit/issues/4541 but it looks like there will be no suitable replacement for the current implementation.

jakzal commented 3 years ago

@gjuric looks like they won't support it "by design". All's left is to wait and see what kind of support PHPUnit will get for real annotations.

jakzal commented 3 years ago

There's one more thing to check. In the past, I played with AOP to do similar kind of injections. However, with PHPUnit it wasn't possible as AOP frameworks usually hook into the autoloading process and PHPUnit wasn't using composer autloading. Mind checking if that's still the case or have they switched to autoload test classes with composer?

jakzal commented 2 years ago

Perhaps the new event system will somehow enable this: https://github.com/sebastianbergmann/phpunit/issues/4676

jakzal commented 1 year ago

I have migrated another extension to PHPUnit 10: https://github.com/jakzal/phpunit-globals/pull/31

jakzal commented 9 months ago

For anyone who'd like to pick this up, the PHPUnit globals extension also supports attributes now: https://github.com/jakzal/phpunit-globals/blob/main/src/GlobalsAttributeReader.php

TomasLudvik commented 5 months ago

I have found a pretty straightforward solution for me, but I am not sure if it is suitable for everyone:

I already have this class in the Symfony project:

abstract class WebTestCase extends SymfonyWebTestCase implements ServiceContainerTestCase

Then everything I needed was to update my setUp method like this:

protected function setUp(): void
{
    parent::setUp();

    $injector = new Injector(new TestCaseContainerFactory($this), new DefaultExtractorFactory([TestCase::class, Assert::class]));
    $injector->inject($this);
}
jakzal commented 5 months ago

@TomasLudvik yes, this is what the extension is doing for you effectively.

This bit of code could be placed in a trait with a [Before] hook and be used instead of the extension.

The point of the extension is to do it automatically. If anyone's interested in helping with migration I've migrated another extension to the new phpunit extension system already: https://github.com/jakzal/phpunit-globals/pull/31

TomasLudvik commented 5 months ago

@jakzal I understand, I have tried the trait way, but the method from trait is not called, if I move it to the Test class, it works correctly.

https://github.com/TomasLudvik/phpunit-injector/commit/56f4398a0c728820472dbe2215c59c1993f5baa0