[TASK] Provide Unit Test for Scheduler/TaskExecuter
authorAlexander Schnitzler <alex.schnitzler@typovision.de>
Sat, 19 Jan 2013 14:59:13 +0000 (15:59 +0100)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Sun, 20 Jan 2013 15:42:12 +0000 (16:42 +0100)
Releases: 6.1
Resolves: #44664
Change-Id: I9ba89e1acc98082cceb7561c394dad1488cbe888
Reviewed-on: https://review.typo3.org/17598
Reviewed-by: Wouter Wolters
Tested-by: Wouter Wolters
Reviewed-by: Anja Leichsenring
Tested-by: Anja Leichsenring
typo3/sysext/extbase/Classes/Scheduler/TaskExecutor.php
typo3/sysext/extbase/Tests/Unit/Scheduler/Fixtures/MockACommandController.php
typo3/sysext/extbase/Tests/Unit/Scheduler/TaskExecuterTest.php [new file with mode: 0644]

index 7b28bc0..20863a0 100644 (file)
@@ -36,6 +36,21 @@ namespace TYPO3\CMS\Extbase\Scheduler;
 class TaskExecutor implements \TYPO3\CMS\Core\SingletonInterface {
 
        /**
+        * @var \TYPO3\CMS\Extbase\Mvc\Cli\Request
+        */
+       protected $request;
+
+       /**
+        * @var \TYPO3\CMS\Extbase\Mvc\Cli\Response
+        */
+       protected $response;
+
+       /**
+        * @var \TYPO3\CMS\Extbase\Mvc\Dispatcher
+        */
+       protected $dispatcher;
+
+       /**
         * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
         */
        protected $objectManager;
@@ -72,6 +87,15 @@ class TaskExecutor implements \TYPO3\CMS\Core\SingletonInterface {
        }
 
        /**
+        * Initializes Request, Response and Dispatcher
+        */
+       public function initializeObject() {
+               $this->request = $this->objectManager->create('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\Request');
+               $this->response = $this->objectManager->create('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\Response');
+               $this->dispatcher = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Dispatcher');
+       }
+
+       /**
         * Initializes configuration manager, object container and reflection service
         *
         * @param array $configuration
@@ -114,15 +138,12 @@ class TaskExecutor implements \TYPO3\CMS\Core\SingletonInterface {
                list($extensionKey, $controllerName, $commandName) = explode(':', $commandIdentifier);
                $extensionName = \TYPO3\CMS\Core\Utility\GeneralUtility::underscoredToUpperCamelCase($extensionKey);
                $this->initialize(array('extensionName' => $extensionName));
-               $request = $this->objectManager->create('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\Request');
-               $dispatcher = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Dispatcher');
-               $response = $this->objectManager->create('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\Response');
                // execute command
                $command = $this->commandManager->getCommandByIdentifier($commandIdentifier);
-               $request->setControllerObjectName($command->getControllerClassName());
-               $request->setControllerCommandName($command->getControllerCommandName());
-               $request->setArguments($task->getArguments());
-               $dispatcher->dispatch($request, $response);
+               $this->request->setControllerObjectName($command->getControllerClassName());
+               $this->request->setControllerCommandName($command->getControllerCommandName());
+               $this->request->setArguments($task->getArguments());
+               $this->dispatcher->dispatch($this->request, $this->response);
                $this->shutdown();
        }
 
index b2da690..b112081 100644 (file)
@@ -36,8 +36,10 @@ class MockACommandController extends \TYPO3\CMS\Extbase\Mvc\Controller\CommandCo
         * funcACommand
         *
         * @param integer $arg A not required argument
+        * @return string
         */
        public function funcACommand($arg = 1) {
+               return 'Foo';
        }
 }
 
diff --git a/typo3/sysext/extbase/Tests/Unit/Scheduler/TaskExecuterTest.php b/typo3/sysext/extbase/Tests/Unit/Scheduler/TaskExecuterTest.php
new file mode 100644 (file)
index 0000000..9c8aa42
--- /dev/null
@@ -0,0 +1,121 @@
+<?php
+namespace TYPO3\CMS\Extbase\Tests\Unit\Scheduler;
+
+require_once (__DIR__ . '/Fixtures/MockACommandController.php');
+
+/***************************************************************
+ *  Copyright notice
+ *
+ *  (c) 2010-2012 Extbase Team (http://forge.typo3.org/projects/typo3v4-mvc)
+ *  Extbase is a backport of TYPO3 Flow. All credits go to the TYPO3 Flow team.
+ *  All rights reserved
+ *
+ *  This script is part of the TYPO3 project. The TYPO3 project is
+ *  free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  The GNU General Public License can be found at
+ *  http://www.gnu.org/copyleft/gpl.html.
+ *  A copy is found in the textfile GPL.txt and important notices to the license
+ *  from the author is found in LICENSE.txt distributed with these scripts.
+ *
+ *
+ *  This script 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.
+ *
+ *  This copyright notice MUST APPEAR in all copies of the script!
+ ***************************************************************/
+/**
+ * TaskExecutor Test Class
+ */
+class TaskExecutorTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
+
+       /**
+        * @var \TYPO3\CMS\Extbase\Mvc\Controller\CommandController|\PHPUnit_Framework_MockObject_MockObject|\Tx_Phpunit_Interface_AccessibleObject
+        */
+       protected $controller;
+
+       /**
+        * @var \TYPO3\CMS\Core\Cache\CacheManager|\PHPUnit_Framework_MockObject_MockObject
+        */
+       protected $cacheManager;
+
+       /**
+        * @var \TYPO3\CMS\Extbase\Object\ObjectManager|\PHPUnit_Framework_MockObject_MockObject
+        */
+       protected $objectManager;
+
+       /**
+        * @var \TYPO3\CMS\Extbase\Mvc\Cli\CommandManager|\PHPUnit_Framework_MockObject_MockObject
+        */
+       protected $commandManager;
+
+       /**
+        * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManager|\PHPUnit_Framework_MockObject_MockObject|\Tx_Phpunit_Interface_AccessibleObject
+        */
+       protected $configurationManager;
+
+       /**
+        * @var \TYPO3\CMS\Extbase\Scheduler\TaskExecutor|\PHPUnit_Framework_MockObject_MockObject|\Tx_Phpunit_Interface_AccessibleObject
+        */
+       protected $taskExecuter;
+
+       public function setUp() {
+               $this->controller = $this->getAccessibleMock('TYPO3\CMS\Extbase\Tests\MockACommandController', array('dummy'));
+               $this->controller->injectReflectionService($this->objectManager->get('TYPO3\CMS\Extbase\Reflection\ReflectionService'));
+               $this->controller->injectObjectManager($this->objectManager);
+
+               $command = new \TYPO3\CMS\Extbase\Mvc\Cli\Command('TYPO3\CMS\Extbase\Tests\MockACommandController', 'funcA');
+               $nullBackend = new \TYPO3\CMS\Core\Cache\Backend\NullBackend('production');
+               $variableFrontend = new \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('foo', $nullBackend);
+
+               $this->cacheManager = $this->getMock('TYPO3\CMS\Core\Cache\CacheManager', array('dummy', 'getCache'));
+               $this->cacheManager->expects($this->any())->method('getCache')->will($this->returnValue($variableFrontend));
+               $GLOBALS['typo3CacheManager'] = $this->cacheManager;
+
+               $this->objectManager = $this->getMock('TYPO3\CMS\Extbase\Object\ObjectManager', array('dummy'));
+               $this->commandManager = $this->getMock('TYPO3\CMS\Extbase\Mvc\Cli\CommandManager', array('dummy', 'getCommandByIdentifier'));
+               $this->configurationManager = $this->getAccessibleMock('TYPO3\CMS\Extbase\Configuration\ConfigurationManager', array('dummy', 'getConfiguration', 'setContentObject', 'setConfiguration'));
+
+               $this->configurationManager
+                       ->expects($this->once())
+                       ->method('getConfiguration')
+                       ->with(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT)
+                       ->will($this->returnValue(array()));
+
+               $this->commandManager
+                       ->expects($this->any())
+                       ->method('getCommandByIdentifier')
+                       ->will($this->returnValue($command));
+       }
+
+       /**
+        * @test
+        * @author Alexander Schnitzler <alex.schnitzler@typovision.de>
+        */
+       public function executeDispatchesTheRightCommandControllerAndCommandAction() {
+               $dispatcher = $this->getMock('TYPO3\CMS\Extbase\Mvc\Dispatcher', array('resolveController'), array($this->objectManager));
+               $dispatcher->expects($this->any())->method('resolveController')->will($this->returnValue($this->controller));
+               $dispatcher->injectSignalSlotDispatcher($this->objectManager->get('TYPO3\CMS\Extbase\SignalSlot\Dispatcher'));
+
+               $this->taskExecuter = $this->getAccessibleMock('TYPO3\CMS\Extbase\Scheduler\TaskExecutor', array('dummy', 'shutdown', 'getDispatcher'));
+               $this->taskExecuter->expects($this->any())->method('getDispatcher')->will($this->returnValue($dispatcher));
+               $this->taskExecuter->injectObjectManager($this->objectManager);
+               $this->taskExecuter->injectCommandManager($this->commandManager);
+               $this->taskExecuter->injectConfigurationManager($this->configurationManager);
+               $this->taskExecuter->initializeObject();
+
+               /** @var $task \TYPO3\CMS\Extbase\Scheduler\Task|\PHPUnit_Framework_MockObject_MockObject */
+               $task = $this->getMock('TYPO3\CMS\Extbase\Scheduler\Task', array('dummy', 'getCommandIdentifier'));
+               $task->expects($this->any())->method('getCommandIdentifier')->will($this->returnValue('extbase:controller:command'));
+               $this->taskExecuter->execute($task);
+
+               $this->assertSame('Foo', $this->taskExecuter->_get('response')->getContent());
+       }
+}
+
+?>
\ No newline at end of file