Ver código fonte

Merge pull request #1892 from jpetitcolas/no_public_non_action

Remove public non action methods in controllers
Thomas 11 anos atrás
pai
commit
e46820b992

+ 8 - 8
Controller/CRUDController.php

@@ -40,7 +40,7 @@ class CRUDController extends Controller
      *
      * @return Response with json encoded data
      */
-    public function renderJson($data, $status = 200, $headers = array())
+    protected function renderJson($data, $status = 200, $headers = array())
     {
         // fake content-type so browser does not show the download popup when this
         // response is rendered through an iframe (used by the jquery.form.js plugin)
@@ -59,7 +59,7 @@ class CRUDController extends Controller
      *
      * @return boolean true if the request is done by an ajax like query
      */
-    public function isXmlHttpRequest()
+    protected function isXmlHttpRequest()
     {
         return $this->get('request')->isXmlHttpRequest() || $this->get('request')->get('_xml_http_request');
     }
@@ -98,7 +98,7 @@ class CRUDController extends Controller
      * @throws \RuntimeException
      * @return void
      */
-    public function configure()
+    protected function configure()
     {
         $adminCode = $this->container->get('request')->get('_sonata_admin');
 
@@ -133,7 +133,7 @@ class CRUDController extends Controller
      *
      * @return string the template name
      */
-    public function getBaseTemplate()
+    protected function getBaseTemplate()
     {
         if ($this->isXmlHttpRequest()) {
             return $this->admin->getTemplate('ajax');
@@ -357,7 +357,7 @@ class CRUDController extends Controller
      *
      * @return Response
      */
-    public function redirectTo($object)
+    protected function redirectTo($object)
     {
         $url = false;
 
@@ -835,7 +835,7 @@ class CRUDController extends Controller
      * @param string $type
      * @param string $message
      */
-    public function addFlash($type, $message)
+    protected function addFlash($type, $message)
     {
         $this->get('session')
              ->getFlashBag()
@@ -849,7 +849,7 @@ class CRUDController extends Controller
      *
      * @throws \RuntimeException
      */
-    public function validateCsrfToken($intention)
+    protected function validateCsrfToken($intention)
     {
         if (!$this->container->has('form.csrf_provider')) {
             return;
@@ -865,7 +865,7 @@ class CRUDController extends Controller
      *
      * @return string
      */
-    public function getCsrfToken($intention)
+    protected function getCsrfToken($intention)
     {
         if (!$this->container->has('form.csrf_provider')) {
             return false;

+ 4 - 4
Controller/CoreController.php

@@ -23,7 +23,7 @@ class CoreController extends Controller
     /**
      * @return \Sonata\AdminBundle\Admin\Pool
      */
-    public function getAdminPool()
+    protected function getAdminPool()
     {
         return $this->container->get('sonata.admin.pool');
     }
@@ -31,7 +31,7 @@ class CoreController extends Controller
     /**
      * @return \Sonata\AdminBundle\Search\SearchHandler
      */
-    public function getSearchHandler()
+    protected function getSearchHandler()
     {
         return $this->get('sonata.admin.search.handler');
     }
@@ -39,7 +39,7 @@ class CoreController extends Controller
     /**
      * @return string
      */
-    public function getBaseTemplate()
+    protected function getBaseTemplate()
     {
         if ($this->getRequest()->isXmlHttpRequest()) {
             return $this->getAdminPool()->getTemplate('ajax');
@@ -115,4 +115,4 @@ class CoreController extends Controller
             'groups'        => $this->getAdminPool()->getDashboardGroups()
         ));
     }
-}
+}

+ 31 - 17
Tests/Controller/CRUDControllerTest.php

@@ -84,6 +84,11 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
      */
     private $template;
 
+    /**
+     * @var array
+     */
+    private $protectedTestedMethods;
+
     /**
      * {@inheritDoc}
      */
@@ -261,6 +266,14 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
 
         $this->controller = new CRUDController();
         $this->controller->setContainer($this->container);
+
+        // Make some methods public to test them
+        $testedMethods = array('renderJson', 'isXmlHttpRequest', 'configure', 'getBaseTemplate', 'redirectTo', 'addFlash');
+        foreach ($testedMethods as $testedMethod) {
+            $method = new \ReflectionMethod('Sonata\\AdminBundle\\Controller\\CRUDController', $testedMethod);
+            $method->setAccessible(true);
+            $this->protectedTestedMethods[$testedMethod] = $method;
+        }
     }
 
     public function testRenderJson1()
@@ -268,7 +281,7 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
         $data = array('example'=>'123', 'foo'=>'bar');
 
         $this->request->headers->set('Content-Type', 'application/x-www-form-urlencoded');
-        $response = $this->controller->renderJson($data);
+        $response = $this->protectedTestedMethods['renderJson']->invoke($this->controller, $data);
 
         $this->assertEquals($response->headers->get('Content-Type'), 'application/json');
         $this->assertEquals(json_encode($data), $response->getContent());
@@ -279,7 +292,7 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
         $data = array('example'=>'123', 'foo'=>'bar');
 
         $this->request->headers->set('Content-Type', 'multipart/form-data');
-        $response = $this->controller->renderJson($data);
+        $response = $this->protectedTestedMethods['renderJson']->invoke($this->controller, $data);
 
         $this->assertEquals($response->headers->get('Content-Type'), 'application/json');
         $this->assertEquals(json_encode($data), $response->getContent());
@@ -291,7 +304,7 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
 
         $this->request->attributes->set('_xml_http_request', true);
         $this->request->headers->set('Content-Type', 'multipart/form-data');
-        $response = $this->controller->renderJson($data);
+        $response = $this->protectedTestedMethods['renderJson']->invoke($this->controller, $data);
 
         $this->assertEquals($response->headers->get('Content-Type'), 'text/plain');
         $this->assertEquals(json_encode($data), $response->getContent());
@@ -299,16 +312,17 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
 
     public function testIsXmlHttpRequest()
     {
-        $this->assertFalse($this->controller->isXmlHttpRequest());
+        $this->assertFalse($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller));
 
         $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
-        $this->assertTrue($this->controller->isXmlHttpRequest());
+
+        $this->assertTrue($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller));
 
         $this->request->headers->remove('X-Requested-With');
-        $this->assertFalse($this->controller->isXmlHttpRequest());
+        $this->assertFalse($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller));
 
         $this->request->attributes->set('_xml_http_request', true);
-        $this->assertTrue($this->controller->isXmlHttpRequest());
+        $this->assertTrue($this->protectedTestedMethods['isXmlHttpRequest']->invoke($this->controller));
     }
 
     public function testConfigure()
@@ -322,7 +336,7 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
                 }));
 
         $this->request->query->set('uniqid', 123456);
-        $this->controller->configure();
+        $this->protectedTestedMethods['configure']->invoke($this->controller);
 
         $this->assertEquals(123456, $uniqueId);
         $this->assertAttributeEquals($this->admin, 'admin', $this->controller);
@@ -348,7 +362,7 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
             ->will($this->returnValue($adminParent));
 
         $this->request->query->set('uniqid', 123456);
-        $this->controller->configure();
+        $this->protectedTestedMethods['configure']->invoke($this->controller);
 
         $this->assertEquals(123456, $uniqueId);
         $this->assertAttributeEquals($adminParent, 'admin', $this->controller);
@@ -359,7 +373,7 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
         $this->setExpectedException('RuntimeException', 'There is no `_sonata_admin` defined for the controller `Sonata\AdminBundle\Controller\CRUDController`');
 
         $this->request->attributes->remove('_sonata_admin');
-        $this->controller->configure();
+        $this->protectedTestedMethods['configure']->invoke($this->controller);
     }
 
     public function testConfigureWithException2()
@@ -367,21 +381,21 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
         $this->setExpectedException('RuntimeException', 'Unable to find the admin class related to the current controller (Sonata\AdminBundle\Controller\CRUDController)');
 
         $this->request->attributes->set('_sonata_admin', 'nonexistent.admin');
-        $this->controller->configure();
+        $this->protectedTestedMethods['configure']->invoke($this->controller);
     }
 
     public function testGetBaseTemplate()
     {
-        $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->controller->getBaseTemplate());
+        $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller));
 
         $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
-        $this->assertEquals('SonataAdminBundle::ajax_layout.html.twig', $this->controller->getBaseTemplate());
+        $this->assertEquals('SonataAdminBundle::ajax_layout.html.twig', $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller));
 
         $this->request->headers->remove('X-Requested-With');
-        $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->controller->getBaseTemplate());
+        $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller));
 
         $this->request->attributes->set('_xml_http_request', true);
-        $this->assertEquals('SonataAdminBundle::ajax_layout.html.twig', $this->controller->getBaseTemplate());
+        $this->assertEquals('SonataAdminBundle::ajax_layout.html.twig', $this->protectedTestedMethods['getBaseTemplate']->invoke($this->controller));
     }
 
     public function testRender()
@@ -620,7 +634,7 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
             $this->request->query->set($key, $value);
         }
 
-        $response = $this->controller->redirectTo($object);
+        $response = $this->protectedTestedMethods['redirectTo']->invoke($this->controller, $object);
         $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
         $this->assertEquals($expected, $response->getTargetUrl());
     }
@@ -638,7 +652,7 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
 
     public function testAddFlash()
     {
-        $this->controller->addFlash('foo', 'bar');
+        $this->protectedTestedMethods['addFlash']->invoke($this->controller, 'foo', 'bar');
         $this->assertSame(array('bar'), $this->session->getFlashBag()->get('foo'));
     }