Browse Source

Merge pull request #4043 from ahmetakbn/code-improve

changed injection of container to injection of pool
Grégoire Paris 8 years ago
parent
commit
531bbf9146
4 changed files with 140 additions and 8 deletions
  1. 1 1
      Resources/config/core.xml
  2. 107 0
      Tests/Twig/GlobalVariablesTest.php
  3. 28 7
      Twig/GlobalVariables.php
  4. 4 0
      UPGRADE-3.x.md

+ 1 - 1
Resources/config/core.xml

@@ -66,7 +66,7 @@
         </service>
         <!-- twig -->
         <service id="sonata.admin.twig.global" class="Sonata\AdminBundle\Twig\GlobalVariables">
-            <argument type="service" id="service_container"/>
+            <argument type="service" id="sonata.admin.pool"/>
         </service>
     </services>
 </container>

+ 107 - 0
Tests/Twig/GlobalVariablesTest.php

@@ -0,0 +1,107 @@
+<?php
+
+/*
+ * This file is part of the Sonata Project package.
+ *
+ * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Sonata\AdminBundle\Tests\Twig;
+
+use Sonata\AdminBundle\Twig\GlobalVariables;
+
+/**
+ * @author Ahmet Akbana <ahmetakbana@gmail.com>
+ */
+class GlobalVariablesTest extends \PHPUnit_Framework_TestCase
+{
+    private $code;
+    private $action;
+    private $admin;
+    private $pool;
+
+    public function setUp()
+    {
+        $this->code = 'sonata.page.admin.page|sonata.page.admin.snapshot';
+        $this->action = 'list';
+        $this->admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
+        $this->pool = $this->getMockBuilder('Sonata\AdminBundle\Admin\Pool')->disableOriginalConstructor()->getMock();
+    }
+
+    public function testUrl()
+    {
+        $this->admin->expects($this->once())
+            ->method('generateUrl')
+            ->with('sonata.page.admin.page|sonata.page.admin.snapshot.list', array('foo'), false)
+            ->willReturn(true);
+
+        $this->pool->expects($this->once())
+            ->method('getAdminByAdminCode')
+            ->with('sonata.page.admin.page')
+            ->willReturn($this->admin);
+
+        $globalVariables = new GlobalVariables($this->pool);
+
+        $globalVariables->url($this->code, $this->action, array('foo'));
+    }
+
+    public function testObjectUrl()
+    {
+        $this->admin->expects($this->once())
+            ->method('generateObjectUrl')
+            ->with('sonata.page.admin.page|sonata.page.admin.snapshot.list', 'foo', array('bar'), false)
+            ->willReturn(true);
+
+        $this->pool->expects($this->once())
+            ->method('getAdminByAdminCode')
+            ->with('sonata.page.admin.page')
+            ->willReturn($this->admin);
+
+        $globalVariables = new GlobalVariables($this->pool);
+
+        $globalVariables->objectUrl($this->code, $this->action, 'foo', array('bar'));
+    }
+
+    /**
+     * @group legacy
+     * NEXT_MAJOR: remove this method
+     */
+    public function testWithContainer()
+    {
+        $this->admin->expects($this->once())
+            ->method('generateUrl')
+            ->with('sonata.page.admin.page|sonata.page.admin.snapshot.list', array('foo'), false)
+            ->willReturn(true);
+
+        $this->pool->expects($this->once())
+            ->method('getAdminByAdminCode')
+            ->with('sonata.page.admin.page')
+            ->willReturn($this->admin);
+
+        $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
+        $container->expects($this->once())
+            ->method('get')
+            ->with('sonata.admin.pool')
+            ->willReturn($this->pool);
+
+        $globalVariables = new GlobalVariables($container);
+
+        $globalVariables->url($this->code, $this->action, array('foo'));
+    }
+
+    /**
+     * NEXT_MAJOR: remove this method.
+     */
+    public function testInvalidArgumentException()
+    {
+        $this->setExpectedException(
+            'InvalidArgumentException',
+            '$adminPool should be an instance of Sonata\AdminBundle\Admin\Pool'
+        );
+
+        new GlobalVariables('foo');
+    }
+}

+ 28 - 7
Twig/GlobalVariables.php

@@ -15,23 +15,44 @@ use Sonata\AdminBundle\Admin\Pool;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 
 /**
- * Class GlobalVariables.
- *
  * @author  Thomas Rabaix <thomas.rabaix@sonata-project.org>
  */
 class GlobalVariables
 {
     /**
      * @var ContainerInterface
+     *
+     * @deprecated Since version 3.x, will be removed in 4.0.
+     * NEXT_MAJOR : remove this property
      */
     protected $container;
 
     /**
-     * @param ContainerInterface $container
+     * @var Pool
      */
-    public function __construct(ContainerInterface $container)
+    protected $adminPool;
+
+    /**
+     * @param ContainerInterface|Pool $adminPool
+     */
+    public function __construct($adminPool)
     {
-        $this->container = $container;
+        // NEXT_MAJOR : remove this block and set adminPool from parameter.
+        if ($adminPool instanceof ContainerInterface) {
+            @trigger_error(
+                'Using an instance of Symfony\Component\DependencyInjection\ContainerInterface is deprecated since 
+                version 3.x and will be removed in 4.0. Use Sonata\AdminBundle\Admin\Pool instead.',
+                E_USER_DEPRECATED
+            );
+
+            $this->adminPool = $adminPool->get('sonata.admin.pool');
+        } elseif ($adminPool instanceof Pool) {
+            $this->adminPool = $adminPool;
+        } else {
+            throw new \InvalidArgumentException(
+                '$adminPool should be an instance of Sonata\AdminBundle\Admin\Pool'
+            );
+        }
     }
 
     /**
@@ -39,7 +60,7 @@ class GlobalVariables
      */
     public function getAdminPool()
     {
-        return $this->container->get('sonata.admin.pool');
+        return $this->adminPool;
     }
 
     /**
@@ -81,7 +102,7 @@ class GlobalVariables
      */
     private function getCodeAction($code, $action)
     {
-        if ($pipe = strpos('|', $code)) {
+        if ($pipe = strpos($code, '|')) {
             // convert code=sonata.page.admin.page|sonata.page.admin.snapshot, action=list
             // to => sonata.page.admin.page|sonata.page.admin.snapshot.list
             $action = $code.'.'.$action;

+ 4 - 0
UPGRADE-3.x.md

@@ -1,6 +1,10 @@
 UPGRADE 3.x
 ===========
 
+## Deprecated injection of container to GlobalVariables
+
+The `$container` property in `Twig/GlobalVariables` is deprecated.
+
 ## Deprecated ModelTypeList for rename
 
 The `Sonata\AdminBundle\Form\Type\ModelTypeList` class is now deprecated.