Kaynağa Gözat

CRUDControllerTest: added tests for deleteAction

Andrej Hudec 11 yıl önce
ebeveyn
işleme
ee8ad3ee2b
1 değiştirilmiş dosya ile 167 ekleme ve 0 silme
  1. 167 0
      Tests/Controller/CRUDControllerTest.php

+ 167 - 0
Tests/Controller/CRUDControllerTest.php

@@ -286,6 +286,7 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
 
         $this->admin->expects($this->once())
             ->method('isGranted')
+            ->with($this->equalTo('LIST'))
             ->will($this->returnValue(false));
 
         $this->controller->listAction();
@@ -295,6 +296,11 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
     {
         $datagrid = $this->getMock('Sonata\AdminBundle\Datagrid\DatagridInterface');
 
+        $this->admin->expects($this->once())
+            ->method('isGranted')
+            ->with($this->equalTo('LIST'))
+            ->will($this->returnValue(true));
+
         $form = $this->getMockBuilder('Symfony\Component\Form\Form')
             ->disableOriginalConstructor()
             ->getMock();
@@ -330,6 +336,7 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
 
         $this->admin->expects($this->once())
             ->method('isGranted')
+            ->with($this->equalTo('DELETE'))
             ->will($this->returnValue(false));
 
         $this->controller->batchActionDelete($this->getMock('Sonata\AdminBundle\Datagrid\ProxyQueryInterface'));
@@ -339,6 +346,11 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
     {
         $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
 
+        $this->admin->expects($this->once())
+            ->method('isGranted')
+            ->with($this->equalTo('DELETE'))
+            ->will($this->returnValue(true));
+
         $this->admin->expects($this->once())
             ->method('getModelManager')
             ->will($this->returnValue($modelManager));
@@ -400,6 +412,7 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
 
         $this->admin->expects($this->once())
             ->method('isGranted')
+            ->with($this->equalTo('VIEW'))
             ->will($this->returnValue(false));
 
         $this->controller->showAction();
@@ -415,6 +428,7 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
 
         $this->admin->expects($this->once())
             ->method('isGranted')
+            ->with($this->equalTo('VIEW'))
             ->will($this->returnValue(true));
 
         $show = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionCollection');
@@ -476,4 +490,157 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
         $this->controller->addFlash('foo', 'bar');
         $this->assertSame(array('bar'), $this->session->getFlashBag()->get('foo'));
     }
+
+    public function testDeleteActionNotFoundException()
+    {
+        $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException');
+
+        $this->admin->expects($this->once())
+            ->method('getObject')
+            ->will($this->returnValue(false));
+
+        $this->controller->deleteAction(1);
+    }
+
+    public function testDeleteActionAccessDenied()
+    {
+        $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
+
+        $this->admin->expects($this->once())
+            ->method('getObject')
+            ->will($this->returnValue(new \stdClass()));
+
+        $this->admin->expects($this->once())
+            ->method('isGranted')
+            ->with($this->equalTo('DELETE'))
+            ->will($this->returnValue(false));
+
+        $this->controller->deleteAction(1);
+    }
+
+    public function testDeleteAction()
+    {
+        $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->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->deleteAction(1));
+
+        $this->assertEquals($this->admin, $this->parameters['admin']);
+        $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
+        $this->assertEquals($this->pool, $this->parameters['admin_pool']);
+
+        $this->assertEquals('delete', $this->parameters['action']);
+        $this->assertEquals($object, $this->parameters['object']);
+        $this->assertEquals('', $this->parameters['csrf_token']);
+    }
+
+    public function testDeleteActionAjaxSuccess()
+    {
+        $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->request->setMethod('DELETE');
+
+        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
+
+        $response = $this->controller->deleteAction(1);
+
+        $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
+        $this->assertEquals(json_encode(array('result'=>'ok')), $response->getContent());
+    }
+
+    public function testDeleteActionAjaxError()
+    {
+        $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->request->setMethod('DELETE');
+
+        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
+
+        $response = $this->controller->deleteAction(1);
+
+        $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
+        $this->assertEquals(json_encode(array('result'=>'error')), $response->getContent());
+    }
+
+    public function testDeleteActionSuccess()
+    {
+        $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->request->setMethod('DELETE');
+
+        $response = $this->controller->deleteAction(1);
+
+        $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
+        $this->assertSame(array('flash_delete_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
+        $this->assertEquals('list', $response->getTargetUrl());
+    }
+
+    public function testDeleteActionError()
+    {
+        $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->request->setMethod('DELETE');
+
+        $response = $this->controller->deleteAction(1);
+
+        $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
+        $this->assertSame(array('flash_delete_error'), $this->session->getFlashBag()->get('sonata_flash_error'));
+        $this->assertEquals('list', $response->getTargetUrl());
+    }
 }