[!!!][TASK] Extract testing framework for TYPO3
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / Core / Widget / WidgetRequestBuilderTest.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Tests\Unit\Core\Widget;
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 WidgetRequestBuilderTest extends \TYPO3\CMS\Components\TestingFramework\Core\UnitTestCase
21 {
22 /**
23 * @var \TYPO3\CMS\Fluid\Core\Widget\WidgetRequestBuilder
24 */
25 protected $widgetRequestBuilder;
26
27 /**
28 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
29 */
30 protected $mockObjectManager;
31
32 /**
33 * @var \TYPO3\CMS\Fluid\Core\Widget\WidgetRequest
34 */
35 protected $mockWidgetRequest;
36
37 /**
38 * @var \TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder
39 */
40 protected $mockAjaxWidgetContextHolder;
41
42 /**
43 * @var \TYPO3\CMS\Fluid\Core\Widget\WidgetContext
44 */
45 protected $mockWidgetContext;
46
47 protected function setUp()
48 {
49 $this->widgetRequestBuilder = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\Widget\WidgetRequestBuilder::class, ['setArgumentsFromRawRequestData']);
50 $this->mockWidgetRequest = $this->createMock(\TYPO3\CMS\Fluid\Core\Widget\WidgetRequest::class);
51 $this->mockObjectManager = $this->createMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
52 $this->mockObjectManager->expects($this->once())->method('get')->with(\TYPO3\CMS\Fluid\Core\Widget\WidgetRequest::class)->will($this->returnValue($this->mockWidgetRequest));
53 $this->widgetRequestBuilder->_set('objectManager', $this->mockObjectManager);
54 $this->mockWidgetContext = $this->createMock(\TYPO3\CMS\Fluid\Core\Widget\WidgetContext::class);
55 $this->mockAjaxWidgetContextHolder = $this->createMock(\TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder::class);
56 $this->widgetRequestBuilder->injectAjaxWidgetContextHolder($this->mockAjaxWidgetContextHolder);
57 $this->mockAjaxWidgetContextHolder->expects($this->once())->method('get')->will($this->returnValue($this->mockWidgetContext));
58 }
59
60 /**
61 * @test
62 */
63 public function buildSetsRequestUri()
64 {
65 $requestUri = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL');
66 $this->mockWidgetRequest->expects($this->once())->method('setRequestURI')->with($requestUri);
67 $this->widgetRequestBuilder->build();
68 }
69
70 /**
71 * @test
72 */
73 public function buildSetsBaseUri()
74 {
75 $baseUri = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
76 $this->mockWidgetRequest->expects($this->once())->method('setBaseURI')->with($baseUri);
77 $this->widgetRequestBuilder->build();
78 }
79
80 /**
81 * @test
82 */
83 public function buildSetsRequestMethod()
84 {
85 $_SERVER['REQUEST_METHOD'] = 'POST';
86 $this->mockWidgetRequest->expects($this->once())->method('setMethod')->with('POST');
87 $this->widgetRequestBuilder->build();
88 }
89
90 /**
91 * @test
92 */
93 public function buildSetsPostArgumentsFromRequest()
94 {
95 $_SERVER['REQUEST_METHOD'] = 'POST';
96 $_GET = ['get' => 'foo'];
97 $_POST = ['post' => 'bar'];
98 $this->mockWidgetRequest->expects($this->once())->method('setArguments')->with($_POST);
99 $this->widgetRequestBuilder->build();
100 }
101
102 /**
103 * @test
104 */
105 public function buildSetsGetArgumentsFromRequest()
106 {
107 $_SERVER['REQUEST_METHOD'] = 'GET';
108 $_GET = ['get' => 'foo'];
109 $_POST = ['post' => 'bar'];
110 $this->mockWidgetRequest->expects($this->once())->method('setArguments')->with($_GET);
111 $this->widgetRequestBuilder->build();
112 }
113
114 /**
115 * @test
116 */
117 public function buildSetsControllerActionNameFromGetArguments()
118 {
119 $_GET = ['action' => 'myAction'];
120 $this->mockWidgetRequest->expects($this->once())->method('setControllerActionName')->with('myAction');
121 $this->widgetRequestBuilder->build();
122 }
123
124 /**
125 * @test
126 */
127 public function buildSetsWidgetContext()
128 {
129 $_GET = ['fluid-widget-id' => '123'];
130 $this->mockAjaxWidgetContextHolder->expects($this->once())->method('get')->with('123')->will($this->returnValue($this->mockWidgetContext));
131 $this->mockWidgetRequest->expects($this->once())->method('setWidgetContext')->with($this->mockWidgetContext);
132 $this->widgetRequestBuilder->build();
133 }
134
135 /**
136 * @test
137 */
138 public function buildReturnsRequest()
139 {
140 $expected = $this->mockWidgetRequest;
141 $actual = $this->widgetRequestBuilder->build();
142 $this->assertSame($expected, $actual);
143 }
144 }