Last active
March 7, 2019 19:54
-
-
Save kunicmarko20/ae89b9f3c5c6229a7e81cfae44d9a3b7 to your computer and use it in GitHub Desktop.
Symfony Test Case with Conatiner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Tests; | |
| use Doctrine\ORM\EntityManagerInterface; | |
| use Symfony\Component\DependencyInjection\ResettableContainerInterface; | |
| use Symfony\Component\Form\PreloadedExtension; | |
| use Symfony\Component\Form\Test\TypeTestCase; | |
| use Symfony\Bridge\Doctrine\Form\Type\EntityType; | |
| abstract class AbstractTypeTestCase extends TypeTestCase | |
| { | |
| /** | |
| * @var \Kernel | |
| */ | |
| protected static $kernel; | |
| protected static $container; | |
| protected function get(string $serviceId) | |
| { | |
| return $this->getContainer()->get($serviceId); | |
| } | |
| protected function getContainer() | |
| { | |
| if (!isset(self::$kernel)) { | |
| self::$kernel = new \Kernel($_ENV['APP_ENV'], $_ENV['APP_DEBUG']); | |
| self::$kernel->boot(); | |
| } | |
| if (!isset(self::$container)) { | |
| self::$container = self::$kernel->getContainer(); | |
| } | |
| return self::$container; | |
| } | |
| protected function getManager(): EntityManagerInterface | |
| { | |
| return $this->getContainer()->get('doctrine')->getManager(); | |
| } | |
| protected function tearDown() | |
| { | |
| if (null !== static::$kernel) { | |
| $container = static::$kernel->getContainer(); | |
| static::$kernel->shutdown(); | |
| if ($container instanceof ResettableContainerInterface) { | |
| $container->reset(); | |
| } | |
| } | |
| } | |
| protected function getExtensions() | |
| { | |
| return [ | |
| new PreloadedExtension( | |
| [new EntityType($this->get('doctrine'))], | |
| [] | |
| ), | |
| ]; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment