From 16badcdf6caa7f4c5cbf67ac64e315f44eb9f1c1 Mon Sep 17 00:00:00 2001 From: JohnKrovitch Date: Fri, 27 Jan 2017 23:40:40 +0100 Subject: [PATCH 1/3] fix adding a global variable in Twig when it is not initialized --- Event/Subscriber/TemplateSubscriber.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Event/Subscriber/TemplateSubscriber.php b/Event/Subscriber/TemplateSubscriber.php index c08f427a1..e111ea094 100644 --- a/Event/Subscriber/TemplateSubscriber.php +++ b/Event/Subscriber/TemplateSubscriber.php @@ -38,14 +38,10 @@ public function __construct(Twig_Environment $twig, ConfigurationFactory $config public static function getSubscribedEvents() { return [ - KernelEvents::REQUEST => 'kernelView', + KernelEvents::CONTROLLER => 'kernelView', ]; } - - /** - * - * @return null - */ + public function kernelView() { $this->twig->addGlobal('config', $this->configuration); From 64fe8edab99a841d96739abacc83b61dacedd241 Mon Sep 17 00:00:00 2001 From: JohnKrovitch Date: Sat, 28 Jan 2017 15:31:39 +0100 Subject: [PATCH 2/3] fix potential bug in DoctrineRepository when saving linked entities --- Repository/DoctrineRepository.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Repository/DoctrineRepository.php b/Repository/DoctrineRepository.php index ccf7d8ecc..d319d5389 100644 --- a/Repository/DoctrineRepository.php +++ b/Repository/DoctrineRepository.php @@ -17,7 +17,7 @@ abstract class DoctrineRepository extends EntityRepository implements Repository public function save($entity) { $this->_em->persist($entity); - $this->_em->flush($entity); + $this->_em->flush(); } /** * Delete an entity. @@ -27,6 +27,6 @@ public function save($entity) public function delete($entity) { $this->_em->remove($entity); - $this->_em->flush($entity); + $this->_em->flush(); } } From a747e315dfa5a80169a861f54e79825aa7bd702b Mon Sep 17 00:00:00 2001 From: JohnKrovitch Date: Sat, 28 Jan 2017 16:36:15 +0100 Subject: [PATCH 3/3] fixing tests with after dependencies update --- .../LAGAdminExtensionTest.php | 34 +++++++++------ Tests/AdminBundle/Field/ActionTest.php | 42 ++++++++++++++----- Tests/AdminBundle/Field/DateTest.php | 12 ++++-- Tests/AdminBundle/Field/LinkTest.php | 37 +++++++++++----- 4 files changed, 87 insertions(+), 38 deletions(-) diff --git a/Tests/AdminBundle/DependencyInjection/LAGAdminExtensionTest.php b/Tests/AdminBundle/DependencyInjection/LAGAdminExtensionTest.php index 601e56d25..9637732a5 100644 --- a/Tests/AdminBundle/DependencyInjection/LAGAdminExtensionTest.php +++ b/Tests/AdminBundle/DependencyInjection/LAGAdminExtensionTest.php @@ -25,6 +25,7 @@ use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\HttpKernel\KernelInterface; +use Twig_Loader_Array; class LAGAdminExtensionTest extends AdminTestBase { @@ -40,7 +41,7 @@ public function testLoad() 'application' => [] ] ], $container); - $this->assertCount(28, $container->getDefinitions()); + $this->assertCount(29, $container->getDefinitions()); $eventDispatcherExtension = new FrameworkExtension(); $eventDispatcherExtension->load([], $container); @@ -107,18 +108,23 @@ protected function assertServices(ContainerBuilder $container) */ protected function getWorkingContainer() { - $generic = new Definition(); - $generic->setClass(stdClass::class); - $fileLocator = new Definition(); - $fileLocator->setClass(FileLocator::class); - $templateNameParser = new Definition(); - $templateNameParser->setClass(TemplateNameParser::class); - $templateNameParser->addArgument($this->createMock(KernelInterface::class)); - $logger = new Definition(); - $logger->setClass(Logger::class); - $logger->addArgument('default'); - $session= new Definition(); - $session->setClass(Session::class); + $kernel = $this + ->getMockBuilder(KernelInterface::class) + ->disableOriginalConstructor() + ->getMock() + ; + $generic = new Definition(stdClass::class); + $fileLocator = new Definition(FileLocator::class); + + $templateNameParser = new Definition(TemplateNameParser::class, [ + $kernel, + ]); + + $logger = new Definition(Logger::class, [ + 'default' + ]); + $session= new Definition(Session::class); + $twigLoader = new Definition(Twig_Loader_Array::class); $entityManager = new Definition(); $entityManager->setClass(FakeEntityManager::class); @@ -130,6 +136,7 @@ protected function getWorkingContainer() $container->setParameter('kernel.charset', 'utf8'); $container->setParameter('kernel.secret', 'MyLittleSecret'); $container->setParameter('kernel.bundles', []); + $container->setParameter('kernel.bundles_metadata', []); $container->setParameter('kernel.environment', 'prod'); $container->setDefinitions([ @@ -141,6 +148,7 @@ protected function getWorkingContainer() 'form.factory' => $generic, 'templating.locator' => $fileLocator, 'templating.name_parser' => $templateNameParser, + 'twig.loader' => $twigLoader, ]); return $container; diff --git a/Tests/AdminBundle/Field/ActionTest.php b/Tests/AdminBundle/Field/ActionTest.php index 170684ce4..50d47b579 100644 --- a/Tests/AdminBundle/Field/ActionTest.php +++ b/Tests/AdminBundle/Field/ActionTest.php @@ -1,12 +1,13 @@ setApplicationConfiguration($this->createApplicationConfiguration()); $linkField->configureOptions($resolver); - + // an url or a route SHOULD be provided $this->assertExceptionRaised(InvalidOptionsException::class, function() use ($linkField, $resolver, $options) { $linkField->setOptions($resolver->resolve($options)); }); - + $options = [ - 'url' => 'http:/test.fr/' + 'url' => 'http:/test.fr/', + 'title' => 'MyAction' ]; - + + $twig = $this + ->getMockBuilder(Twig_Environment::class) + ->disableOriginalConstructor() + ->getMock() + ; + $twig + ->expects($this->once()) + ->method('render') + ->with('LAGAdminBundle:Render:link.html.twig', [ + 'text' => 'MyAction', + 'url' => $options['url'], + 'route' => '', + 'parameters' => [], + 'target' => '_self', + 'title' => 'MyAction', + 'icon' => '', + ]) + ->willReturn('

a string yeah !!!

') + ; + $linkField->setOptions($resolver->resolve($options)); $linkField->setTranslator(new IdentityTranslator()); - $linkField->setTwig($this->createTwigEnvironment()); - + $linkField->setTwig($twig); + $result = $linkField->render('test'); - - $this->assertEquals('LAGAdminBundle:Render:link.html.twig', $result['template']); - $this->assertEquals($options['url'], $result['parameters']['url']); + $this->assertInternalType('string', $result); } } diff --git a/Tests/AdminBundle/Field/DateTest.php b/Tests/AdminBundle/Field/DateTest.php index 870f4022a..c0abb7784 100644 --- a/Tests/AdminBundle/Field/DateTest.php +++ b/Tests/AdminBundle/Field/DateTest.php @@ -1,6 +1,6 @@ 'd/m/Y', ]; $resolver = new OptionsResolver(); - + + $twig = $this + ->getMockBuilder(\Twig_Environment::class) + ->disableOriginalConstructor() + ->getMock() + ; + $linkField = new Date(); $linkField->setApplicationConfiguration($this->createApplicationConfiguration()); $linkField->configureOptions($resolver); $linkField->setOptions($resolver->resolve($options)); $linkField->setTranslator(new IdentityTranslator()); - $linkField->setTwig($this->createTwigEnvironment()); + $linkField->setTwig($twig); $now = new DateTime(); diff --git a/Tests/AdminBundle/Field/LinkTest.php b/Tests/AdminBundle/Field/LinkTest.php index 6f8b77f74..918104b4c 100644 --- a/Tests/AdminBundle/Field/LinkTest.php +++ b/Tests/AdminBundle/Field/LinkTest.php @@ -7,6 +7,7 @@ use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Translation\IdentityTranslator; use Test\TestBundle\Entity\TestEntity; +use Twig_Environment; class LinkTest extends AdminTestBase { @@ -19,26 +20,40 @@ public function testRender() ], 'target' => '_blank', 'title' => 'MyTitle', - 'icon' => 'fa-test' + 'icon' => 'fa-test', + 'template' => 'LAGAdminBundle:Render:link.html.twig', ]; $resolver = new OptionsResolver(); - + + $twig = $this + ->getMockBuilder(Twig_Environment::class) + ->disableOriginalConstructor() + ->getMock() + ; + $twig + ->expects($this->once()) + ->method('render') + ->with('LAGAdminBundle:Render:link.html.twig', [ + 'text' => 'test', + 'route' => 'route_test', + 'parameters' => $options['parameters'], + 'target' => '_blank', + 'url' => '', + 'title' => 'MyTitle', + 'icon' => 'fa-test', + ]) + ->willReturn('

lol man

') + ; + $linkField = new Link(); $linkField->setApplicationConfiguration($this->createApplicationConfiguration()); $linkField->configureOptions($resolver); $linkField->setOptions($resolver->resolve($options)); $linkField->setTranslator(new IdentityTranslator()); - $linkField->setTwig($this->createTwigEnvironment()); + $linkField->setTwig($twig); $linkField->setEntity(new TestEntity()); $result = $linkField->render('test'); - - $this->assertEquals('LAGAdminBundle:Render:link.html.twig', $result['template']); - $this->assertEquals('test', $result['parameters']['text']); - $this->assertEquals('route_test', $result['parameters']['route']); - $this->assertEquals($options['parameters'], $result['parameters']['parameters']); - $this->assertEquals('_blank', $result['parameters']['target']); - $this->assertEquals('MyTitle', $result['parameters']['title']); - $this->assertEquals('fa-test', $result['parameters']['icon']); + $this->assertInternalType('string', $result); } }