[!!!][TASK] Clean up ObjectManager injection of CommandController
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Mvc / Controller / CommandControllerTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Test case
19 */
20 class CommandControllerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
21 {
22 /**
23 * @var \TYPO3\CMS\Extbase\Mvc\Controller\CommandController|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
24 */
25 protected $commandController;
26
27 /**
28 * \Symfony\Component\Console\Output\ConsoleOutput|\PHPUnit_Framework_MockObject_MockObject
29 */
30 protected $mockConsoleOutput;
31
32 protected function setUp()
33 {
34 $this->commandController = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\CommandController::class, array('dummyCommand'));
35 $this->mockConsoleOutput = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Cli\ConsoleOutput::class)->disableOriginalConstructor()->getMock();
36 $this->commandController->_set('output', $this->mockConsoleOutput);
37 }
38
39 /**
40 * @test
41 */
42 public function outputAppendsGivenStringToTheResponseContent()
43 {
44 $this->mockConsoleOutput->expects($this->once())->method('output')->with('some text');
45 $this->commandController->_call('output', 'some text');
46 }
47
48 /**
49 * @test
50 */
51 public function outputReplacesArgumentsInGivenString()
52 {
53 $this->mockConsoleOutput->expects($this->once())->method('output')->with('%2$s %1$s', array('text', 'some'));
54 $this->commandController->_call('output', '%2$s %1$s', array('text', 'some'));
55 }
56
57 /**
58 * @test
59 * @expectedException \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
60 */
61 public function quitThrowsStopActionException()
62 {
63 $mockResponse = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Cli\Response::class);
64 $this->commandController->_set('response', $mockResponse);
65 $this->commandController->_call('quit');
66 }
67
68 /**
69 * @test
70 * @expectedException \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
71 */
72 public function quitSetsResponseExitCode()
73 {
74 $mockResponse = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Cli\Response::class);
75 $mockResponse->expects($this->once())->method('setExitCode')->with(123);
76 $this->commandController->_set('response', $mockResponse);
77 $this->commandController->_call('quit', 123);
78 }
79
80 /**
81 * @test
82 */
83 public function settingRequestAdminPropertySetsAdminRoleInUserAuthentication()
84 {
85 $mockedUserAuthentication = $this->getMock(\TYPO3\CMS\Core\Authentication\AbstractUserAuthentication::class);
86 $mockedUserAuthentication->user['admin'] = 42;
87 $this->commandController->expects($this->once())
88 ->method('dummyCommand')
89 ->will(
90 $this->returnCallback(
91 function () use ($mockedUserAuthentication) {
92 if ($mockedUserAuthentication->user['admin'] !== 1) {
93 throw new \Exception('User role is not admin');
94 }
95 }
96 ));
97 $GLOBALS['BE_USER'] = $mockedUserAuthentication;
98 $this->commandController->_set('arguments', array());
99 $this->commandController->_set('commandMethodName', 'dummyCommand');
100 $this->commandController->_set('requestAdminPermissions', true);
101 $this->commandController->_call('callCommandMethod');
102
103 $this->assertSame(42, $mockedUserAuthentication->user['admin']);
104 }
105 }