Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility with FrameworkBundle Test Traits #39

Closed
chapterjason opened this issue Jul 18, 2021 · 2 comments · Fixed by #44
Closed

Compatibility with FrameworkBundle Test Traits #39

chapterjason opened this issue Jul 18, 2021 · 2 comments · Fixed by #44

Comments

@chapterjason
Copy link
Contributor

chapterjason commented Jul 18, 2021

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.

@Nyholm
Copy link
Member

Nyholm commented Jul 29, 2021

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.

@chapterjason
Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants