SymfonyTest/symfony-bundle-test

Compatibility with FrameworkBundle Test Traits

chapterjason opened this issue · 2 comments

I just noticed that the BaseBundleTestCase class is not compatible with the FrameworkBundle AssertionTraits.

In the trait MailerAssertionsTrait the getContainer function is statically called, cause it is expected that the trait will be used in the KernelTestCase which have all the kernel and functions static.

Unfortunately the BaseBundleTestCase uses non-static variables and functions and causes errors like these:

Error : Non-static method Nyholm\BundleTest\BaseBundleTestCase::getContainer() cannot be called statically
 /[...]/vendor/symfony/framework-bundle/Test/MailerAssertionsTrait.php:121
 /[...]/vendor/symfony/framework-bundle/Test/MailerAssertionsTrait.php:25
 /[...]/Tests/ExampleTest.php:159

I see three possible solutions:

  1. We change the functions and variables to static, which would be a breaking change.
  2. We serve custom traits with the same functionality which uses $this, but this also means more maintenance.
  3. Combination of 1 and 2. We first serve custom traits to avoid a breaking change and change to static in version 2.0.

// EDIT
Also noticed that the KernelTextCase returns the test.service_container to allow access to private services.

Hm.. I added that method in the Symfony code base.

What about removing it from BaseBundleTestCase? That will require a new major version, but it is in the correct direction.

I took a closer look and yes, this could be a good idea.

I have already tested how it is usable:

<?php

namespace SoureCode\Bundle\Token\Tests;

use DAMA\DoctrineTestBundle\DAMADoctrineTestBundle;
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Nyholm\BundleTest\AppKernel;
use Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use MyCustomBundle;

abstract class MyAbstractBundleTestCase extends KernelTestCase
{

    protected static function createKernel(array $options = [])
    {
        KernelTestCase::$class = AppKernel::class;

        /**
         * @var AppKernel $kernel
         */
        $kernel = parent::createKernel($options);

        $kernel->addBundle(DoctrineBundle::class);
        $kernel->addBundle(DAMADoctrineTestBundle::class);
        $kernel->addBundle(StofDoctrineExtensionsBundle::class);
        $kernel->addBundle(MyCustomBundle::class);
        $kernel->addConfigFile(__DIR__.'/config.yml');

        return $kernel;
    }
}

To use it like this we need to take some changes in the AppKernel.