diff --git a/module/VuFind/src/VuFindTest/Container/MockContainer.php b/module/VuFind/src/VuFindTest/Container/MockContainer.php new file mode 100644 index 0000000000000000000000000000000000000000..5ac932b5a51c8f2198bccffca4299850326d0d98 --- /dev/null +++ b/module/VuFind/src/VuFindTest/Container/MockContainer.php @@ -0,0 +1,153 @@ +<?php +/** + * Container that produces mock objects. + * + * PHP version 7 + * + * Copyright (C) Villanova University 2018. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * @category VuFind + * @package Tests + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link https://vufind.org Main Page + */ +namespace VuFindTest\Container; + +use Interop\Container\ContainerInterface; +use PHPUnit\Framework\TestCase; + +/** + * Container that produces mock objects. + * + * @category VuFind + * @package Tests + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link https://vufind.org Main Page + */ +class MockContainer implements ContainerInterface +{ + /** + * Disabled services. + * + * @var string[] + */ + protected $disabled = []; + + /** + * Services + * + * @var array + */ + protected $services = []; + + /** + * Test case (for building mock objects) + * + * @var TestCase + */ + protected $test; + + /** + * Constructor + * + * @param TestCase $test Test using the container + */ + public function __construct(TestCase $test) + { + $this->test = $test; + } + + /** + * Create a mock object. + * + * @param string $id Identifier of the service to mock out. + * @param array $methods Methods to mock. + * + * @return mixed + */ + public function createMock($id, $methods = []) + { + return $this->test->getMockBuilder($id) + ->disableOriginalConstructor() + ->setMethods($methods) + ->getMock(); + } + + /** + * Disable a service + * + * @param string $id Identifier of the entry to disable. + * + * @return MockContainer + */ + public function disable($id) + { + // Don't double-disable a service: + if ($this->has($id)) { + $this->disabled[] = $id; + } + // Fluent interface: + return $this; + } + + /** + * Finds an entry of the container by its identifier and returns it. + * + * @param string $id Identifier of the entry to look for. + * + * @return mixed + */ + public function get($id) + { + if (!isset($this->services[$id])) { + $this->services[$id] = $this->createMock($id); + } + return $this->services[$id]; + } + + /** + * Returns true if the container can return an entry for the given identifier. + * Returns false otherwise. + * + * `has($id)` returning true does not mean that `get($id)` will not throw an exception. + * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. + * + * @param string $id Identifier of the entry to look for. + * + * @return bool + */ + public function has($id) + { + // Assume every service exists unless explicitly disabled + return !in_array($id, $this->disabled); + } + + /** + * Explicitly set an entry in the container. + * + * @param string $id Identifier of the entry to set. + * @param mixed $obj The service to set. + * + * @return MockContainer + */ + public function set($id, $obj) + { + $this->services[$id] = $obj; + return $this; + } +} diff --git a/module/VuFind/src/VuFindTest/Unit/AjaxHandlerTest.php b/module/VuFind/src/VuFindTest/Unit/AjaxHandlerTest.php index 997fb9fbe6e359a303dbdd631d602f902422baea..56f87a259c1993aca378846495e1b19387577881 100644 --- a/module/VuFind/src/VuFindTest/Unit/AjaxHandlerTest.php +++ b/module/VuFind/src/VuFindTest/Unit/AjaxHandlerTest.php @@ -29,7 +29,6 @@ namespace VuFindTest\Unit; use Zend\Http\Request; use Zend\Mvc\Controller\Plugin\Params; -use Zend\ServiceManager\ServiceManager; use Zend\Stdlib\Parameters; /** @@ -41,24 +40,8 @@ use Zend\Stdlib\Parameters; * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License * @link https://vufind.org Main Page */ -abstract class AjaxHandlerTest extends \PHPUnit\Framework\TestCase +abstract class AjaxHandlerTest extends MockContainerTest { - /** - * Create a mock service. - * - * @param string $name Name of class implementing service - * @param array $methods Methods to mock - * - * @return object - */ - protected function getMockService($name, $methods = []) - { - return $this->getMockBuilder($name) - ->disableOriginalConstructor() - ->setMethods($methods) - ->getMock(); - } - /** * Create mock user object. * @@ -66,21 +49,7 @@ abstract class AjaxHandlerTest extends \PHPUnit\Framework\TestCase */ protected function getMockUser() { - return $this->getMockService('VuFind\Db\Row\User'); - } - - /** - * Add a service to a container. - * - * @param ServiceManager $container Container to populate - * @param string $name Name of service to create - * @param mixed $value Value of service (or null to create mock) - * - * @return void - */ - protected function addServiceToContainer($container, $name, $value = null) - { - $container->setService($name, $value ?? $this->getMockService($name)); + return $this->container->get('VuFind\Db\Row\User'); } /** @@ -92,7 +61,9 @@ abstract class AjaxHandlerTest extends \PHPUnit\Framework\TestCase */ protected function getMockAuthManager($user) { - $authManager = $this->getMockService('VuFind\Auth\Manager', ['isLoggedIn']); + $authManager = $this->container->createMock( + 'VuFind\Auth\Manager', ['isLoggedIn'] + ); $authManager->expects($this->any())->method('isLoggedIn') ->will($this->returnValue($user)); return $authManager; @@ -112,7 +83,7 @@ abstract class AjaxHandlerTest extends \PHPUnit\Framework\TestCase $request = new Request(); $request->setQuery(new Parameters($get)); $request->setPost(new Parameters($post)); - $controller = $this->getMockService( + $controller = $this->container->createMock( 'Zend\Mvc\Controller\AbstractActionController', ['getRequest'] ); $controller->expects($this->any())->method('getRequest') diff --git a/module/VuFind/src/VuFindTest/Unit/MockContainerTest.php b/module/VuFind/src/VuFindTest/Unit/MockContainerTest.php new file mode 100644 index 0000000000000000000000000000000000000000..995f5b1073ff8d6d8c64ccffc4b1bf1dc00a081d --- /dev/null +++ b/module/VuFind/src/VuFindTest/Unit/MockContainerTest.php @@ -0,0 +1,59 @@ +<?php +/** + * Base class for tests using a MockContainer. + * + * PHP version 7 + * + * Copyright (C) Villanova University 2018. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * @category VuFind + * @package Tests + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link https://vufind.org Main Page + */ +namespace VuFindTest\Unit; + +use VuFindTest\Container\MockContainer; + +/** + * Base class for tests using a MockContainer. + * + * @category VuFind + * @package Tests + * @author Demian Katz <demian.katz@villanova.edu> + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link https://vufind.org Main Page + */ +abstract class MockContainerTest extends \PHPUnit\Framework\TestCase +{ + /** + * Mock container + * + * @var MockContainer + */ + protected $container; + + /** + * Standard setup method. + * + * @return void + */ + public function setUp() + { + $this->container = new MockContainer($this); + } +} diff --git a/module/VuFind/tests/unit-tests/src/VuFindTest/AjaxHandler/CheckRequestIsValidTest.php b/module/VuFind/tests/unit-tests/src/VuFindTest/AjaxHandler/CheckRequestIsValidTest.php index 17079e4996861705a9310830ca68779294fb03e8..4b3b2938825cce2b05a514bf3d7431c01f7278cc 100644 --- a/module/VuFind/tests/unit-tests/src/VuFindTest/AjaxHandler/CheckRequestIsValidTest.php +++ b/module/VuFind/tests/unit-tests/src/VuFindTest/AjaxHandler/CheckRequestIsValidTest.php @@ -30,9 +30,8 @@ namespace VuFindTest\AjaxHandler; use VuFind\AjaxHandler\AbstractIlsAndUserActionFactory; use VuFind\AjaxHandler\CheckRequestIsValid; use VuFind\Auth\ILSAuthenticator; +use VuFind\Auth\Manager; use VuFind\ILS\Connection; -use VuFind\Session\Settings; -use Zend\ServiceManager\ServiceManager; /** * CheckRequestIsValid test class. @@ -48,33 +47,18 @@ class CheckRequestIsValidTest extends \VuFindTest\Unit\AjaxHandlerTest /** * Set up a CheckRequestIsValid handler for testing. * - * @param Settings $ss Session settings (or null for default) - * @param Connection $ils ILS connection (or null for default) - * @param ILSAuthenticator $ilsAuth ILS authenticator (or null for default) - * @param User|bool $user Return value for isLoggedIn() in auth manager + * @param User|bool $user Return value for isLoggedIn() in auth manager * * @return CheckRequestIsValid */ - protected function getHandler($ss = null, $ils = null, $ilsAuth = null, - $user = false - ) { - // Create container - $container = new ServiceManager(); - - // Install or mock up services: - $this->addServiceToContainer($container, 'VuFind\Session\Settings', $ss); - $this->addServiceToContainer($container, 'VuFind\ILS\Connection', $ils); - $this->addServiceToContainer( - $container, 'VuFind\Auth\ILSAuthenticator', $ilsAuth - ); - + protected function getHandler($user = false) + { // Set up auth manager with user: - $authManager = $this->getMockAuthManager($user); - $container->setService('VuFind\Auth\Manager', $authManager); + $this->container->set(Manager::class, $this->getMockAuthManager($user)); // Build the handler: $factory = new AbstractIlsAndUserActionFactory(); - return $factory($container, CheckRequestIsValid::class); + return $factory($this->container, CheckRequestIsValid::class); } /** @@ -98,7 +82,7 @@ class CheckRequestIsValidTest extends \VuFindTest\Unit\AjaxHandlerTest */ public function testEmptyQuery() { - $handler = $this->getHandler(null, null, null, $this->getMockUser()); + $handler = $this->getHandler($this->getMockUser()); $this->assertEquals( ['bulk_error_missing', 400], $handler->handleRequest($this->getParamsHelper()) @@ -112,17 +96,17 @@ class CheckRequestIsValidTest extends \VuFindTest\Unit\AjaxHandlerTest */ protected function runSuccessfulTest($ilsMethod, $requestType = null) { - $ilsAuth = $this->getMockService( - 'VuFind\Auth\ILSAuthenticator', ['storedCatalogLogin'] - ); + $ilsAuth = $this->container + ->createMock(ILSAuthenticator::class, ['storedCatalogLogin']); $ilsAuth->expects($this->once())->method('storedCatalogLogin') ->will($this->returnValue([3])); - $ils = $this - ->getMockService('VuFind\ILS\Connection', [$ilsMethod]); + $ils = $this->container->createMock(Connection::class, [$ilsMethod]); $ils->expects($this->once())->method($ilsMethod) ->with($this->equalTo(1), $this->equalTo(2), $this->equalTo([3])) ->will($this->returnValue(true)); - $handler = $this->getHandler(null, $ils, $ilsAuth, $this->getMockUser()); + $this->container->set(Connection::class, $ils); + $this->container->set(ILSAuthenticator::class, $ilsAuth); + $handler = $this->getHandler($this->getMockUser()); $params = ['id' => 1, 'data' => 2, 'requestType' => $requestType]; return $handler->handleRequest($this->getParamsHelper($params)); } diff --git a/module/VuFind/tests/unit-tests/src/VuFindTest/AjaxHandler/CommentRecordTest.php b/module/VuFind/tests/unit-tests/src/VuFindTest/AjaxHandler/CommentRecordTest.php index b2676de64988bab07e3cc74da0fe1c63735a8a66..e30aaf0a0887289ef524b4cf84a4e37cb19c0735 100644 --- a/module/VuFind/tests/unit-tests/src/VuFindTest/AjaxHandler/CommentRecordTest.php +++ b/module/VuFind/tests/unit-tests/src/VuFindTest/AjaxHandler/CommentRecordTest.php @@ -29,11 +29,10 @@ namespace VuFindTest\AjaxHandler; use VuFind\AjaxHandler\CommentRecord; use VuFind\AjaxHandler\CommentRecordFactory; -use VuFind\Controller\Plugin\Recaptcha; +use VuFind\Config\AccountCapabilities; use VuFind\Db\Row\Resource; use VuFind\Db\Row\User; use VuFind\Db\Table\Resource as ResourceTable; -use Zend\ServiceManager\ServiceManager; /** * CommentRecord test class. @@ -49,46 +48,32 @@ class CommentRecordTest extends \VuFindTest\Unit\AjaxHandlerTest /** * Set up a CommentRecord handler for testing. * - * @param ResourceTable $table Resource table mock (or null for default) - * @param Recaptcha $recaptcha Recaptcha plugin mock (or null for default) - * @param bool $enabled Are comments enabled? - * @param User|bool $user Return value for isLoggedIn() in auth manager + * @param bool $enabled Are comments enabled? + * @param User|bool $user Return value for isLoggedIn() in auth manager * * @return CommentRecord */ - protected function getHandler($table = null, $recaptcha = null, $enabled = true, - $user = false - ) { - // Create container - $container = new ServiceManager(); - + protected function getHandler($enabled = true, $user = false) + { // For simplicity, let the top-level container stand in for the plugin // managers: - $container->setService('VuFind\Db\Table\PluginManager', $container); - $container->setService('ControllerPluginManager', $container); - - // Install or mock up remaining services: - $this->addServiceToContainer( - $container, 'VuFind\Db\Table\Resource', $table - ); - $this->addServiceToContainer( - $container, 'VuFind\Controller\Plugin\Recaptcha', $recaptcha - ); + $this->container->set('VuFind\Db\Table\PluginManager', $this->container); + $this->container->set('ControllerPluginManager', $this->container); // Set up auth manager with user: $authManager = $this->getMockAuthManager($user); - $container->setService('VuFind\Auth\Manager', $authManager); + $this->container->set('VuFind\Auth\Manager', $authManager); // Set up capability configuration: $cfg = new \Zend\Config\Config( ['Social' => ['comments' => $enabled ? 'enabled' : 'disabled']] ); - $capabilities = new \VuFind\Config\AccountCapabilities($cfg, $authManager); - $container->setService('VuFind\Config\AccountCapabilities', $capabilities); + $capabilities = new AccountCapabilities($cfg, $authManager); + $this->container->set(AccountCapabilities::class, $capabilities); // Build the handler: $factory = new CommentRecordFactory(); - return $factory($container, CommentRecord::class); + return $factory($this->container, CommentRecord::class); } /** @@ -101,7 +86,7 @@ class CommentRecordTest extends \VuFindTest\Unit\AjaxHandlerTest */ protected function getMockResource($comment, $user) { - $row = $this->getMockService('VuFind\Db\Row\Resource', ['addComment']); + $row = $this->container->createMock(Resource::class, ['addComment']); $row->expects($this->once())->method('addComment') ->with($this->equalTo($comment), $this->equalTo($user)) ->will($this->returnValue(true)); @@ -115,7 +100,7 @@ class CommentRecordTest extends \VuFindTest\Unit\AjaxHandlerTest */ public function testDisabledResponse() { - $handler = $this->getHandler(null, null, false); + $handler = $this->getHandler(false); $this->assertEquals( ['Comments disabled', 400], $handler->handleRequest($this->getParamsHelper()) @@ -129,7 +114,7 @@ class CommentRecordTest extends \VuFindTest\Unit\AjaxHandlerTest */ public function testLoggedOutUser() { - $handler = $this->getHandler(null, null, true); + $handler = $this->getHandler(true); $this->assertEquals( ['You must be logged in first', 401], $handler->handleRequest($this->getParamsHelper()) @@ -143,7 +128,7 @@ class CommentRecordTest extends \VuFindTest\Unit\AjaxHandlerTest */ public function testEmptyQuery() { - $handler = $this->getHandler(null, null, true, $this->getMockUser()); + $handler = $this->getHandler(true, $this->getMockUser()); $this->assertEquals( ['bulk_error_missing', 400], $handler->handleRequest($this->getParamsHelper()) @@ -158,11 +143,13 @@ class CommentRecordTest extends \VuFindTest\Unit\AjaxHandlerTest public function testSuccessfulTransaction() { $user = $this->getMockUser(); - $table = $this->getMockService('VuFind\Db\Table\Resource', ['findResource']); + $table = $this->container + ->createMock(ResourceTable::class, ['findResource']); $table->expects($this->once())->method('findResource') ->with($this->equalTo('foo'), $this->equalTo('Solr')) ->will($this->returnValue($this->getMockResource('bar', $user))); - $handler = $this->getHandler($table, null, true, $user); + $this->container->set(ResourceTable::class, $table); + $handler = $this->getHandler(true, $user); $post = [ 'id' => 'foo', 'comment' => 'bar', diff --git a/module/VuFind/tests/unit-tests/src/VuFindTest/AjaxHandler/KeepAliveTest.php b/module/VuFind/tests/unit-tests/src/VuFindTest/AjaxHandler/KeepAliveTest.php index 0fa748c52cfd2fb95060314c4a775af9ebe1d2aa..7334e6f0cc36e046af50b7572fdebbbc394a822a 100644 --- a/module/VuFind/tests/unit-tests/src/VuFindTest/AjaxHandler/KeepAliveTest.php +++ b/module/VuFind/tests/unit-tests/src/VuFindTest/AjaxHandler/KeepAliveTest.php @@ -29,6 +29,7 @@ namespace VuFindTest\AjaxHandler; use VuFind\AjaxHandler\KeepAlive; use VuFind\AjaxHandler\KeepAliveFactory; +use Zend\Session\SessionManager; /** * KeepAlive test class. @@ -48,12 +49,11 @@ class KeepAliveTest extends \VuFindTest\Unit\AjaxHandlerTest */ public function testResponse() { - $sm = $this->getMockService('Zend\Session\SessionManager', ['getId']); + $sm = $this->container->createMock(SessionManager::class, ['getId']); $sm->expects($this->once())->method('getId'); - $container = new \Zend\ServiceManager\ServiceManager(); - $container->setService('Zend\Session\SessionManager', $sm); + $this->container->set(SessionManager::class, $sm); $factory = new KeepAliveFactory(); - $handler = $factory($container, KeepAlive::class); + $handler = $factory($this->container, KeepAlive::class); $params = new \Zend\Mvc\Controller\Plugin\Params(); $this->assertEquals([true], $handler->handleRequest($params)); } diff --git a/module/VuFind/tests/unit-tests/src/VuFindTest/AjaxHandler/RecommendTest.php b/module/VuFind/tests/unit-tests/src/VuFindTest/AjaxHandler/RecommendTest.php index c6330d1f44af7f0928581896305d4153e16597a4..d07c83dc503315c040895098e6119623d20f2cbf 100644 --- a/module/VuFind/tests/unit-tests/src/VuFindTest/AjaxHandler/RecommendTest.php +++ b/module/VuFind/tests/unit-tests/src/VuFindTest/AjaxHandler/RecommendTest.php @@ -28,6 +28,14 @@ namespace VuFindTest\AjaxHandler; use VuFind\AjaxHandler\Recommend; +use VuFind\AjaxHandler\RecommendFactory; +use VuFind\Recommend\PluginManager; +use VuFind\Recommend\RecommendInterface; +use VuFind\Search\Results\PluginManager as ResultsManager; +use VuFind\Search\Solr\Results; +use VuFind\Session\Settings; +use VuFind\View\Helper\Root\Recommend as RecommendHelper; +use Zend\View\Renderer\PhpRenderer; /** * Recommend test class. @@ -47,20 +55,37 @@ class RecommendTest extends \VuFindTest\Unit\AjaxHandlerTest */ public function testResponse() { - $ss = $this->getMockService('VuFind\Session\Settings', ['disableWrite']); + // Set up session settings: + $ss = $this->container->createMock(Settings::class, ['disableWrite']); $ss->expects($this->once())->method('disableWrite'); - $mockPlugin = $this->getMockService('VuFind\Recommend\RecommendInterface'); - $rm = $this->getMockService('VuFind\Recommend\PluginManager', ['get']); + $this->container->set(Settings::class, $ss); + + // Set up recommend plugin manager: + $mockPlugin = $this->container->createMock(RecommendInterface::class); + $rm = $this->container->createMock(PluginManager::class, ['get']); $rm->expects($this->once())->method('get')->with($this->equalTo('foo')) ->will($this->returnValue($mockPlugin)); - $r = $this->getMockService('VuFind\Search\Solr\Results'); - $viewHelper = $this->getMockService('VuFind\View\Helper\Root\Recommend'); - $view = $this - ->getMockService('Zend\View\Renderer\PhpRenderer', ['plugin']); + $this->container->set(PluginManager::class, $rm); + + // Set up results plugin manager: + $resultsManager = $this->container + ->createMock(ResultsManager::class, ['get']); + $resultsManager->expects($this->once())->method('get') + ->with($this->equalTo('Solr')) + ->will($this->returnValue($this->container->createMock(Results::class))); + $this->container->set(ResultsManager::class, $resultsManager); + + // Set up view helper and renderer: + $viewHelper = $this->container->createMock(RecommendHelper::class); + $view = $this->container->createMock(PhpRenderer::class, ['plugin']); $view->expects($this->once())->method('plugin') ->with($this->equalTo('recommend')) ->will($this->returnValue($viewHelper)); - $handler = new Recommend($ss, $rm, $r, $view); + $this->container->set('ViewRenderer', $view); + + // Build and test the ajax handler: + $factory = new RecommendFactory(); + $handler = $factory($this->container, Recommend::class); $params = $this->getParamsHelper(['mod' => 'foo']); $this->assertEquals([null], $handler->handleRequest($params)); }