From d62a4eb4e7c4787c1acf4c57c3ee7303a5878c1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Burnichon?= Date: Wed, 1 Jul 2015 15:01:34 +0200 Subject: [PATCH] Refactor `setAdmin` method to properly handle special cases. --- .../Phrasea/Controller/Admin/DataboxTest.php | 39 ++++--- .../PhraseanetAuthenticatedWebTestCase.php | 105 ++++++++++-------- 2 files changed, 80 insertions(+), 64 deletions(-) diff --git a/tests/Alchemy/Tests/Phrasea/Controller/Admin/DataboxTest.php b/tests/Alchemy/Tests/Phrasea/Controller/Admin/DataboxTest.php index 3f82e8da18..24febf3050 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/Admin/DataboxTest.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/Admin/DataboxTest.php @@ -135,11 +135,14 @@ class DataboxTest extends \PhraseanetAuthenticatedWebTestCase */ public function testGetCGUHasNoRights() { - $this->setAdmin(true) - ->expects($this->once()) - ->method('has_right_on_sbas') - ->with($this->equalTo(self::$DI['collection']->get_sbas_id()), 'bas_modify_struct') - ->will($this->returnValue(false)); + $this->setAdmin(true, [ + 'has_right_on_sbas'=> function (\PHPUnit_Framework_MockObject_MockObject $acl) { + $acl->expects($this->once()) + ->method('has_right_on_sbas') + ->with($this->equalTo(self::$DI['collection']->get_sbas_id()), 'bas_modify_struct') + ->will($this->returnValue(false)); + } + ]); self::$DI['client']->request('GET', '/admin/databox/' . self::$DI['collection']->get_sbas_id() . '/cgus/'); @@ -151,11 +154,14 @@ class DataboxTest extends \PhraseanetAuthenticatedWebTestCase */ public function testGetCGU() { - $this->setAdmin(true) - ->expects($this->once()) - ->method('has_right_on_sbas') - ->with($this->equalTo(self::$DI['collection']->get_sbas_id()), 'bas_modify_struct') - ->will($this->returnValue(true)); + $this->setAdmin(true, [ + 'has_right_on_sbas'=> function (\PHPUnit_Framework_MockObject_MockObject $acl) { + $acl->expects($this->once()) + ->method('has_right_on_sbas') + ->with($this->equalTo(self::$DI['collection']->get_sbas_id()), 'bas_modify_struct') + ->will($this->returnValue(true)); + } + ]); self::$DI['client']->request('GET', '/admin/databox/' . self::$DI['collection']->get_sbas_id() . '/cgus/'); $this->assertTrue(self::$DI['client']->getResponse()->isOk()); @@ -182,11 +188,14 @@ class DataboxTest extends \PhraseanetAuthenticatedWebTestCase */ public function testUpdateDatabaseCGU() { - $this->setAdmin(true) - ->expects($this->once()) - ->method('has_right_on_sbas') - ->with($this->equalTo(self::$DI['collection']->get_sbas_id()), 'bas_modify_struct') - ->will($this->returnValue(true)); + $this->setAdmin(true, [ + 'has_right_on_sbas'=> function (\PHPUnit_Framework_MockObject_MockObject $acl) { + $acl->expects($this->once()) + ->method('has_right_on_sbas') + ->with($this->equalTo(self::$DI['collection']->get_sbas_id()), 'bas_modify_struct') + ->will($this->returnValue(true)); + } + ]); $cgusUpdate = 'Test update CGUS'; diff --git a/tests/classes/PhraseanetAuthenticatedWebTestCase.php b/tests/classes/PhraseanetAuthenticatedWebTestCase.php index 5288eceae4..b03d3f4fa9 100644 --- a/tests/classes/PhraseanetAuthenticatedWebTestCase.php +++ b/tests/classes/PhraseanetAuthenticatedWebTestCase.php @@ -9,64 +9,71 @@ abstract class PhraseanetAuthenticatedWebTestCase extends \PhraseanetAuthenticat private static $createdDataboxes = []; /** - * @param bool $bool + * @param bool $bool + * @param array $stubs List of Closure to call indexed by method names, null to avoid calls * @return PHPUnit_Framework_MockObject_MockObject The stubbedACL */ - public function setAdmin($bool) + public function setAdmin($bool, array $stubs = []) { $stubbedACL = $this->stubACL(); + $stubs = array_filter(array_replace($this->getDefaultStubs(), $stubs)); - $stubbedACL->expects($this->any()) - ->method('is_admin') - ->will($this->returnValue($bool)); - - $stubbedACL->expects($this->any()) - ->method('give_access_to_sbas') - ->will($this->returnSelf()); - - $stubbedACL->expects($this->any()) - ->method('update_rights_to_sbas') - ->will($this->returnSelf()); - - $stubbedACL->expects($this->any()) - ->method('update_rights_to_bas') - ->will($this->returnSelf()); - - $stubbedACL->expects($this->any()) - ->method('has_right_on_base') - ->will($this->returnValue($bool)); - - $stubbedACL->expects($this->any()) - ->method('has_right_on_sbas') - ->will($this->returnValue($bool)); - - $stubbedACL->expects($this->any()) - ->method('has_access_to_sbas') - ->will($this->returnValue($bool)); - - $stubbedACL->expects($this->any()) - ->method('has_access_to_base') - ->will($this->returnValue($bool)); - - $stubbedACL->expects($this->any()) - ->method('has_right') - ->will($this->returnValue($bool)); - - $stubbedACL->expects($this->any()) - ->method('has_access_to_module') - ->will($this->returnValue($bool)); - - $stubbedACL->expects($this->any()) - ->method('get_granted_base') - ->will($this->returnValue([self::$DI['collection']])); - - $stubbedACL->expects($this->any()) - ->method('get_granted_sbas') - ->will($this->returnValue([self::$DI['collection']->get_databox()])); + foreach ($stubs as $method => $stub) { + call_user_func($stub, $stubbedACL, $method, $bool); + } return $stubbedACL; } + protected function getDefaultStubs() + { + static $stubs; + + if (is_array($stubs)) { + return $stubs; + } + + $returnBool = function (PHPUnit_Framework_MockObject_MockObject $acl, $method, $is_admin) { + $acl->expects($this->any()) + ->method($method) + ->will($this->returnValue($is_admin)); + }; + + $returnSelf = function (PHPUnit_Framework_MockObject_MockObject $acl, $method) { + $acl->expects($this->any()) + ->method($method) + ->will($this->returnSelf()); + }; + + $stubGrantedBase = function (PHPUnit_Framework_MockObject_MockObject $acl, $method) { + $acl->expects($this->any()) + ->method($method) + ->will($this->returnValue([self::$DI['collection']])); + }; + $stubGrantedSBase = function (PHPUnit_Framework_MockObject_MockObject $acl, $method) { + $acl->expects($this->any()) + ->method($method) + ->will($this->returnValue([self::$DI['collection']->get_databox()])); + }; + + $stubs = [ + 'is_admin' => $returnBool, + 'give_access_to_sbas' => $returnSelf, + 'update_rights_to_sbas' => $returnSelf, + 'update_rights_to_bas' => $returnSelf, + 'has_right_on_base' => $returnBool, + 'has_right_on_sbas' => $returnBool, + 'has_access_to_sbas' => $returnBool, + 'has_access_to_base' => $returnBool, + 'has_right' => $returnBool, + 'has_access_to_module' => $returnBool, + 'get_granted_base' => $stubGrantedBase, + 'get_granted_sbas' => $stubGrantedSBase, + ]; + + return $stubs; + } + public function createDatabox() { $this->createDatabase();