[TASK] Provide Unit Test for Scheduler/Task
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Scheduler / TaskTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Scheduler;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2012 Extbase Team (http://forge.typo3.org/projects/typo3v4-mvc)
8 * Extbase is a backport of TYPO3 Flow. All credits go to the TYPO3 Flow team.
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 * A copy is found in the textfile GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30 /**
31 * Testcase for the Task object
32 *
33 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
34 */
35 class TaskTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
36
37 /**
38 * @var \TYPO3\CMS\Extbase\Scheduler\Task|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
39 */
40 protected $task;
41
42 /**
43 * @var \TYPO3\CMS\Extbase\Scheduler\TaskExecutor|\PHPUnit_Framework_MockObject_MockObject
44 */
45 protected $taskExecutor;
46
47 public function setUp() {
48 $this->taskExecutor = $this->getMock('TYPO3\\CMS\\Extbase\\Scheduler\\TaskExecutor', array('execute'));
49 $this->task = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Scheduler\\Task', array('logException'), array(), '', FALSE);
50 $this->task->_set('objectManager', \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager'));
51 $this->task->_set('commandManager', \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\CommandManager'));
52 }
53
54 /**
55 * @test
56 * @author Alexander Schnitzler <alex.schnitzler@typovision.de>
57 */
58 public function executeCallsLogExceptionOnCaughtException() {
59 $this->taskExecutor->expects($this->once())->method('execute')->will($this->throwException(new \Exception()));
60 $this->task->_set('taskExecutor', $this->taskExecutor);
61 $this->task->expects($this->once())->method('logException');
62 $this->task->execute();
63 }
64
65 /**
66 * @test
67 * @author Alexander Schnitzler <alex.schnitzler@typovision.de>
68 */
69 public function executeReturnsTrueIfNoExceptionIsCaught() {
70 $this->task->_set('taskExecutor', $this->taskExecutor);
71 $this->assertTrue($this->task->execute());
72 }
73
74 /**
75 * @test
76 * @author Alexander Schnitzler <alex.schnitzler@typovision.de>
77 */
78 public function setCommandIdentifierSetsCommandIdentifierCorrectly() {
79 $this->task->setCommandIdentifier('Foo');
80 $this->assertSame('Foo', $this->task->_get('commandIdentifier'));
81 }
82
83 /**
84 * @test
85 * @author Alexander Schnitzler <alex.schnitzler@typovision.de>
86 */
87 public function getCommandIdentifierReturnsCorrectCommandIdentifier() {
88 $this->task->_set('commandIdentifier', 'Foo');
89 $this->assertSame('Foo', $this->task->getCommandIdentifier());
90 }
91
92 /**
93 * @test
94 * @author Alexander Schnitzler <alex.schnitzler@typovision.de>
95 */
96 public function setArgumentsSetsArgumentsCorrectly() {
97 $this->task->setArguments(array('Foo'));
98 $this->assertSame(array('Foo'), $this->task->_get('arguments'));
99 }
100
101 /**
102 * @test
103 * @author Alexander Schnitzler <alex.schnitzler@typovision.de>
104 */
105 public function getArgumentsReturnsCorrectArguments() {
106 $this->task->_set('arguments', array('Foo'));
107 $this->assertSame(array('Foo'), $this->task->getArguments());
108 }
109
110 /**
111 * @test
112 * @author Alexander Schnitzler <alex.schnitzler@typovision.de>
113 */
114 public function setDefaultsSetsDefaultsCorrectly() {
115 $this->task->setDefaults(array('Foo'));
116 $this->assertSame(array('Foo'), $this->task->_get('defaults'));
117 }
118
119 /**
120 * @test
121 * @author Alexander Schnitzler <alex.schnitzler@typovision.de>
122 */
123 public function getDefaultsReturnsCorrectDefaults() {
124 $this->task->_set('defaults', array('Foo'));
125 $this->assertSame(array('Foo'), $this->task->getDefaults());
126 }
127
128 /**
129 * @test
130 * @author Alexander Schnitzler <alex.schnitzler@typovision.de>
131 */
132 public function addDefaultValueAddsDefaultToDefaults() {
133 $defaults = array('foo' => 'bar');
134 $this->task->_set('defaults', $defaults);
135
136 $defaults['baz'] = 'qux';
137 $this->task->addDefaultValue('baz', 'qux');
138
139 $this->assertSame($defaults, $this->task->getDefaults());
140 }
141
142 /**
143 * @test
144 * @author Alexander Schnitzler <alex.schnitzler@typovision.de>
145 */
146 public function addDefaultValueConvertsBooleanValuesToInteger() {
147 $defaults = array('foo' => 'bar');
148 $this->task->_set('defaults', $defaults);
149
150 $defaults['baz'] = 1;
151 $this->task->addDefaultValue('baz', TRUE);
152
153 $this->assertSame($defaults, $this->task->getDefaults());
154 }
155
156 /**
157 * @test
158 * @author Alexander Schnitzler <alex.schnitzler@typovision.de>
159 */
160 public function getAdditionalInformationRespectsArguments() {
161 $this->task->_set('commandIdentifier', 'foo');
162 $this->task->_set('defaults', array('bar' => 'baz'));
163 $this->task->_set('arguments', array('qux' => 'quux'));
164
165 $this->assertSame('foo qux=quux', $this->task->getAdditionalInformation());
166 }
167 }
168
169 ?>