[TASK] Add FlashMessage API to Extbase AbstractController
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Mvc / Controller / AbstractControllerTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012 Stefan Neufeind <info@speedpartner.de>
8 * (c) 2013 Steffen Müller <typo3@t3node.com>
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 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Abstract Controller Test Class
29 */
30 class AbstractControllerTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
31
32 /**
33 * @test
34 */
35 public function constructResolvesExtensionnameFromOldStyle() {
36 $className = uniqid('Tx_Extbase_Tests_Fixtures_Controller');
37 eval('class ' . $className . ' extends \\TYPO3\\CMS\\Extbase\\Mvc\\Controller\\AbstractController { function getExtensionName() { return $this->extensionName; } }');
38 $mockController = new $className();
39 $expectedResult = 'Extbase';
40 $actualResult = $mockController->getExtensionName();
41 $this->assertEquals($expectedResult, $actualResult);
42 }
43
44 /**
45 * @test
46 */
47 public function constructResolvesExtensionnameFromNamespaced() {
48 $className = uniqid('DummyController');
49 eval('namespace ' . __NAMESPACE__ . '; class ' . $className . ' extends \\TYPO3\\CMS\\Extbase\\Mvc\\Controller\\AbstractController { function getExtensionName() { return $this->extensionName; } }');
50 $classNameNamespaced = __NAMESPACE__ . '\\' . $className;
51 $mockController = new $classNameNamespaced();
52 $expectedResult = 'Extbase';
53 $actualResult = $mockController->getExtensionName();
54 $this->assertEquals($expectedResult, $actualResult);
55 }
56
57 /**
58 * @return array
59 */
60 public function addFlashMessageDataProvider() {
61 return array(
62 array(
63 new \TYPO3\CMS\Core\Messaging\FlashMessage('Simple Message'),
64 'Simple Message',
65 '',
66 \TYPO3\CMS\Core\Messaging\FlashMessage::OK,
67 FALSE
68 ),
69 array(
70 new \TYPO3\CMS\Core\Messaging\FlashMessage('Some OK', 'Message Title', \TYPO3\CMS\Core\Messaging\FlashMessage::OK, TRUE),
71 'Some OK',
72 'Message Title',
73 \TYPO3\CMS\Core\Messaging\FlashMessage::OK,
74 TRUE
75 ),
76 array(
77 new \TYPO3\CMS\Core\Messaging\FlashMessage('Some Info', 'Message Title', \TYPO3\CMS\Core\Messaging\FlashMessage::INFO, TRUE),
78 'Some Info',
79 'Message Title',
80 \TYPO3\CMS\Core\Messaging\FlashMessage::INFO,
81 TRUE
82 ),
83 array(
84 new \TYPO3\CMS\Core\Messaging\FlashMessage('Some Notice', 'Message Title', \TYPO3\CMS\Core\Messaging\FlashMessage::NOTICE, TRUE),
85 'Some Notice',
86 'Message Title',
87 \TYPO3\CMS\Core\Messaging\FlashMessage::NOTICE,
88 TRUE
89 ),
90
91 array(
92 new \TYPO3\CMS\Core\Messaging\FlashMessage('Some Warning', 'Message Title', \TYPO3\CMS\Core\Messaging\FlashMessage::WARNING, TRUE),
93 'Some Warning',
94 'Message Title',
95 \TYPO3\CMS\Core\Messaging\FlashMessage::WARNING,
96 TRUE
97 ),
98 array(
99 new \TYPO3\CMS\Core\Messaging\FlashMessage('Some Error', 'Message Title', \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR, TRUE),
100 'Some Error',
101 'Message Title',
102 \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR,
103 TRUE
104 )
105 );
106 }
107
108 /**
109 * @test
110 * @dataProvider addFlashMessageDataProvider
111 */
112 public function addFlashMessageAddsFlashMessageObjectToFlashMessageQueue($expectedMessage, $messageBody, $messageTitle = '', $severity = \TYPO3\CMS\Core\Messaging\FlashMessage::OK, $storeInSession = TRUE) {
113 $flashMessageQueue = $this->getMock(
114 'TYPO3\\CMS\\Core\\Messaging\\FlashMessageQueue',
115 array('enqueue'),
116 array(),
117 '',
118 FALSE
119 );
120 $flashMessageQueue->expects($this->once())->method('enqueue')->with($this->equalTo($expectedMessage));
121
122 $controllerContext = $this->getMock(
123 '\\TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ControllerContext',
124 array('getFlashMessageQueue')
125 );
126 $controllerContext->expects($this->once())->method('getFlashMessageQueue')->will($this->returnValue($flashMessageQueue));
127
128 $controller = $this->getMockForAbstractClass('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\AbstractController',
129 array(),
130 '',
131 FALSE,
132 TRUE,
133 TRUE,
134 array('dummy')
135 );
136 $this->inject($controller, 'controllerContext', $controllerContext);
137
138 $controller->addFlashMessage($messageBody, $messageTitle, $severity, $storeInSession);
139 }
140
141 /**
142 * @test
143 * @expectedException \InvalidArgumentException
144 */
145 public function addFlashMessageThrowsExceptionOnInvalidMessageBody() {
146 $controller = $this->getMockForAbstractClass('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\AbstractController',
147 array(),
148 '',
149 FALSE,
150 TRUE,
151 TRUE,
152 array('dummy')
153 );
154
155 $controller->addFlashMessage(new \stdClass());
156 }
157 }