jakzal/phpunit-injector

Incompatible definition ok $kernel on SymfonyTestContainer

hugochinchilla opened this issue · 6 comments

The definition of container is not compatible with the one in the framework-bundle package.

A class defined like this causes a "Compile Error":

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as BaseWebTestCase;
use Zalas\Injector\PHPUnit\Symfony\TestCase\SymfonyTestContainer;
use Zalas\Injector\PHPUnit\TestCase\ServiceContainerTestCase;

class WebTestCase extends BaseWebTestCase implements ServiceContainerTestCase
{
    use SymfonyTestContainer;
}
[PHP Fatal error:  Symfony\Bundle\FrameworkBundle\Test\KernelTestCase and Zalas\Injector\PHPUnit\Symfony\TestCase\SymfonyTestContainer define the same property ($kernel) in the composition of App\Tests\WebTestCase. However, the definition differs and is considered incompatible. Class was composed in /code/tests/WebTestCase.php on line 16]
Symfony\Component\ErrorHandler\Error\FatalError^ {#4760
  -error: array:4 [
    "type" => 64
    "message" => "Symfony\Bundle\FrameworkBundle\Test\KernelTestCase and Zalas\Injector\PHPUnit\Symfony\TestCase\SymfonyTestContainer define the same property ($kernel) in the composition of App\Tests\WebTestCase. However, the definition differs and is considered incompatible. Class was composed"
    "file" => "/code/tests/WebTestCase.php"
    "line" => 16
  ]
  #message: "Compile Error: Symfony\Bundle\FrameworkBundle\Test\KernelTestCase and Zalas\Injector\PHPUnit\Symfony\TestCase\SymfonyTestContainer define the same property ($kernel) in the composition of App\Tests\WebTestCase. However, the definition differs and is considered incompatible. Class was composed"
  #code: 0
  #file: "/code/tests/WebTestCase.php"
  #line: 16
}

KernelTestCase does not use type hint, only docblock, this causes the problem, because of php compatibility this is not expected to change in the near future.

abstract class KernelTestCase extends TestCase
{
    /**
     * @var KernelInterface
     */
    protected static $kernel;

I'm not sure if this is a supported use case. I certainly never used this package with the framework bundle's test case. Are you using it this way? Does it work at all?

@hugochinchilla can you send a PR to fix this?

Looking at it again, I don't think it makes sense to extend Symfony's WebTestCase. It duplicates what is already available in the SymfonyTestContainer trait via the SymfonyKernel trait:

trait SymfonyTestContainer
{
    use SymfonyKernel;

    public function createContainer(): ContainerInterface
    {
        return static::bootKernel()->getContainer()->get('test.service_container');
    }
}

Any reason why you need to extend the WebTestCase instead of PHPUnit's TestCase?

Yes, this is an end-to-end test and it's using the provided http client to perform web requests.

I'm sorry for not providing a PR, I meant to do it but the project I was working on has been paused because of COVID and I'm currently working on another project.

Fixed in #32