Browse Source

Merge pull request #1982 from aitboudad/patch-2

[CRUDController]Allowed throw exception on debug mode
Andrej Hudec 10 năm trước cách đây
mục cha
commit
5f15add73c
2 tập tin đã thay đổi với 76 bổ sung7 xóa
  1. 9 5
      Controller/CRUDController.php
  2. 67 2
      Tests/Controller/CRUDControllerTest.php

+ 9 - 5
Controller/CRUDController.php

@@ -186,8 +186,12 @@ class CRUDController extends Controller
         return parent::render($view, $parameters, $response);
     }
 
-    private function logModelManagerException($e)
+    private function handleModelManagerException($e)
     {
+        if ($this->get('kernel')->isDebug()) {
+            throw $e;
+        }
+
         $context = array('exception' => $e);
         if ($e->getPrevious()) {
             $context['previous_exception_message'] = $e->getPrevious()->getMessage();
@@ -247,7 +251,7 @@ class CRUDController extends Controller
             $this->addFlash('sonata_flash_success', 'flash_batch_delete_success');
         } catch (ModelManagerException $e) {
 
-            $this->logModelManagerException($e);
+            $this->handleModelManagerException($e);
             $this->addFlash('sonata_flash_error', 'flash_batch_delete_error');
         }
 
@@ -301,7 +305,7 @@ class CRUDController extends Controller
                 );
 
             } catch (ModelManagerException $e) {
-                $this->logModelManagerException($e);
+                $this->handleModelManagerException($e);
 
                 if ($this->isXmlHttpRequest()) {
                     return $this->renderJson(array('result' => 'error'));
@@ -390,7 +394,7 @@ class CRUDController extends Controller
                     return $this->redirectTo($object);
 
                 } catch (ModelManagerException $e) {
-                    $this->logModelManagerException($e);
+                    $this->handleModelManagerException($e);
 
                     $isFormValid = false;
                 }
@@ -634,7 +638,7 @@ class CRUDController extends Controller
                     return $this->redirectTo($object);
 
                 } catch (ModelManagerException $e) {
-                    $this->logModelManagerException($e);
+                    $this->handleModelManagerException($e);
 
                     $isFormValid = false;
                 }

+ 67 - 2
Tests/Controller/CRUDControllerTest.php

@@ -26,6 +26,7 @@ use Sonata\AdminBundle\Util\AdminObjectAclManipulator;
 use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
 use Sonata\AdminBundle\Tests\Fixtures\Controller\BatchAdminController;
 use Symfony\Component\HttpKernel\Kernel;
+use Symfony\Component\HttpKernel\KernelInterface;
 use Symfony\Component\HttpKernel\Exception\HttpException;
 use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface;
 use Sonata\AdminBundle\Admin\AdminInterface;
@@ -97,6 +98,11 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
      */
     private $csrfProvider;
 
+    /**
+     * @var KernelInterface
+     */
+    private $kernel;
+
     /**
      * {@inheritDoc}
      */
@@ -215,13 +221,15 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
         $this->logger = $this->getMock('Psr\Log\LoggerInterface');
         $logger       = $this->logger; // php 5.3 BC
 
-
         $requestStack = null;
         if (Kernel::MINOR_VERSION > 3) {
             $requestStack = new \Symfony\Component\HttpFoundation\RequestStack();
             $requestStack->push($request);
         }
 
+        $this->kernel = $this->getMock('Symfony\Component\HttpKernel\KernelInterface');
+        $kernel = $this->kernel; // php 5.3 BC
+
         $this->container->expects($this->any())
             ->method('get')
             ->will($this->returnCallback(function ($id) use (
@@ -236,7 +244,8 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
                 $adminObjectAclManipulator,
                 $requestStack,
                 $csrfProvider,
-                $logger
+                $logger,
+                $kernel
             ) {
                 switch ($id) {
                     case 'sonata.admin.pool':
@@ -263,6 +272,8 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
                         return $csrfProvider;
                     case 'logger':
                         return $logger;
+                    case 'kernel':
+                        return $kernel;
                 }
 
                 return null;
@@ -702,6 +713,28 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals('list?filter%5Bfoo%5D=bar', $result->getTargetUrl());
     }
 
+    public function testBatchActionDeleteWithModelManagerExceptionInDebugMode()
+    {
+        $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
+        $this->setExpectedException('Sonata\AdminBundle\Exception\ModelManagerException');
+
+        $modelManager->expects($this->once())
+            ->method('batchDelete')
+            ->will($this->returnCallback(function () {
+                    throw new ModelManagerException();
+                }));
+
+        $this->admin->expects($this->once())
+            ->method('getModelManager')
+            ->will($this->returnValue($modelManager));
+
+        $this->kernel->expects($this->once())
+            ->method('isDebug')
+            ->will($this->returnValue(true));
+
+        $this->controller->batchActionDelete($this->getMock('Sonata\AdminBundle\Datagrid\ProxyQueryInterface'));
+    }
+
     public function testShowActionNotFoundException()
     {
         $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException');
@@ -960,6 +993,38 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals(array(), $this->session->getFlashBag()->all());
     }
 
+    public function testDeleteActionWithModelManagerExceptionInDebugMode()
+    {
+        $this->setExpectedException('Sonata\AdminBundle\Exception\ModelManagerException');
+
+        $object = new \stdClass();
+
+        $this->admin->expects($this->once())
+            ->method('getObject')
+            ->will($this->returnValue($object));
+
+        $this->admin->expects($this->once())
+            ->method('isGranted')
+            ->with($this->equalTo('DELETE'))
+            ->will($this->returnValue(true));
+
+        $this->admin->expects($this->once())
+            ->method('delete')
+            ->will($this->returnCallback(function () {
+                    throw new ModelManagerException();
+                }));
+
+        $this->kernel->expects($this->once())
+            ->method('isDebug')
+            ->will($this->returnValue(true));
+
+        $this->request->setMethod('DELETE');
+        $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');
+
+        $this->controller->deleteAction(1);
+
+    }
+
     public function testDeleteActionSuccess1()
     {
         $object = new \stdClass();