Browse Source

The getShortDescription can now return json data

Thomas Rabaix 11 years ago
parent
commit
1ecfa62c05
2 changed files with 22 additions and 22 deletions
  1. 15 18
      Controller/HelperController.php
  2. 7 4
      Tests/Controller/HelperControllerTest.php

+ 15 - 18
Controller/HelperController.php

@@ -146,7 +146,7 @@ class HelperController
     }
 
     /**
-     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
+     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException|\RuntimeException
      *
      * @param \Symfony\Component\HttpFoundation\Request $request
      *
@@ -173,26 +173,23 @@ class HelperController
         $object = $admin->getObject($objectId);
 
         if (!$object) {
-            return new Response();
+            throw new NotFoundHttpException();
         }
 
-        $description = 'no description available';
-        foreach (array('getAdminTitle', 'getTitle', 'getName', '__toString') as $method) {
-            if (method_exists($object, $method)) {
-                $description = call_user_func(array($object, $method));
-                break;
-            }
+        if ('json' == $request->get('_format')) {
+            return new JsonResponse(array('result' => array(
+                'id'    => $admin->id($object),
+                'label' => $admin->toString($object)
+            )));
+        } elseif ('html' == $request->get('_format')) {
+            return new Response($this->twig->render($admin->getTemplate('short_object_description'), array(
+                'admin'       => $admin,
+                'description' => $admin->toString($object),
+                'object'      => $object,
+            )));
+        } else {
+            throw new \RuntimeException('Invalid format');
         }
-
-        $htmlOutput = $this->twig->render($admin->getTemplate('short_object_description'),
-            array(
-                'admin' => $admin,
-                'description' => $description,
-                'object' => $object,
-            )
-        );
-
-        return new Response($htmlOutput);
     }
 
     /**

+ 7 - 4
Tests/Controller/HelperControllerTest.php

@@ -60,6 +60,9 @@ class HelperControllerTest extends \PHPUnit_Framework_TestCase
         $controller->getShortObjectDescriptionAction($request);
     }
 
+    /**
+     * @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException
+     */
     public function testgetShortObjectDescriptionActionObjectDoesNotExist()
     {
         $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
@@ -82,9 +85,7 @@ class HelperControllerTest extends \PHPUnit_Framework_TestCase
 
         $controller = new HelperController($twig, $pool, $helper);
 
-        $response = $controller->getShortObjectDescriptionAction($request);
-
-        $this->assertEmpty($response->getContent());
+        $controller->getShortObjectDescriptionAction($request);
     }
 
     public function testgetShortObjectDescriptionActionObject()
@@ -95,6 +96,7 @@ class HelperControllerTest extends \PHPUnit_Framework_TestCase
         $admin->expects($this->once())->method('setUniqid');
         $admin->expects($this->once())->method('getTemplate')->will($this->returnValue($mockTemplate));
         $admin->expects($this->once())->method('getObject')->will($this->returnValue(new AdminControllerHelper_Foo));
+        $admin->expects($this->once())->method('toString')->will($this->returnValue('bar'));
         $admin->expects($this->once())->method('generateObjectUrl')->will($this->returnCallback(function($type, $object, $parameters = array()) {
             if ($type != 'edit') {
                 return 'invalid name';
@@ -117,7 +119,8 @@ class HelperControllerTest extends \PHPUnit_Framework_TestCase
         $request = new Request(array(
             'code'     => 'sonata.post.admin',
             'objectId' => 42,
-            'uniqid'   => 'asdasd123'
+            'uniqid'   => 'asdasd123',
+            '_format'  => 'html'
         ));
 
         $pool = new Pool($container, 'title', 'logo');