TYPO3 CMS  TYPO3_7-6
CommandControllerTest.php
Go to the documentation of this file.
1 <?php
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 
21 {
25  protected $commandController;
26 
30  protected $mockConsoleOutput;
31 
32  protected function setUp()
33  {
34  $this->commandController = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\CommandController::class, ['dummyCommand']);
35  $this->mockConsoleOutput = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Cli\ConsoleOutput::class)->disableOriginalConstructor()->getMock();
36  $this->commandController->_set('output', $this->mockConsoleOutput);
37  }
38 
43  {
44  $this->mockConsoleOutput->expects($this->once())->method('output')->with('some text');
45  $this->commandController->_call('output', 'some text');
46  }
47 
52  {
53  $this->mockConsoleOutput->expects($this->once())->method('output')->with('%2$s %1$s', ['text', 'some']);
54  $this->commandController->_call('output', '%2$s %1$s', ['text', 'some']);
55  }
56 
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 
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 
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  $this->commandController->_set('userAuthentication', $mockedUserAuthentication);
98  $this->commandController->_set('arguments', []);
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 }
getAccessibleMock( $originalClassName, $methods=[], array $arguments=[], $mockClassName='', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)