Skip to content

Commit 16e2920

Browse files
authored
Merge pull request #6619 from kenjis/refactor-remove-request-uri
test: remove $request->uri
2 parents a7fb29f + 1dad2a9 commit 16e2920

5 files changed

Lines changed: 98 additions & 136 deletions

File tree

tests/system/Helpers/ArrayHelperTest.php

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ public function testArrayDotIgnoresLastWildcard()
209209
/**
210210
* @dataProvider deepSearchProvider
211211
*
212-
* @param mixed $key
213-
* @param mixed $expected
212+
* @param int|string $key
213+
* @param array|string|null $expected
214214
*/
215215
public function testArrayDeepSearch($key, $expected)
216216
{
@@ -247,12 +247,8 @@ public function testArrayDeepSearchReturnNullEmptyArray()
247247

248248
/**
249249
* @dataProvider sortByMultipleKeysProvider
250-
*
251-
* @param mixed $data
252-
* @param mixed $sortColumns
253-
* @param mixed $expected
254250
*/
255-
public function testArraySortByMultipleKeysWithArray($data, $sortColumns, $expected)
251+
public function testArraySortByMultipleKeysWithArray(array $data, array $sortColumns, array $expected)
256252
{
257253
$success = array_sort_by_multiple_keys($data, $sortColumns);
258254

@@ -262,12 +258,8 @@ public function testArraySortByMultipleKeysWithArray($data, $sortColumns, $expec
262258

263259
/**
264260
* @dataProvider sortByMultipleKeysProvider
265-
*
266-
* @param mixed $data
267-
* @param mixed $sortColumns
268-
* @param mixed $expected
269261
*/
270-
public function testArraySortByMultipleKeysWithObjects($data, $sortColumns, $expected)
262+
public function testArraySortByMultipleKeysWithObjects(array $data, array $sortColumns, array $expected)
271263
{
272264
// Morph to objects
273265
foreach ($data as $index => $dataSet) {
@@ -282,12 +274,8 @@ public function testArraySortByMultipleKeysWithObjects($data, $sortColumns, $exp
282274

283275
/**
284276
* @dataProvider sortByMultipleKeysProvider
285-
*
286-
* @param mixed $data
287-
* @param mixed $sortColumns
288-
* @param mixed $expected
289277
*/
290-
public function testArraySortByMultipleKeysFailsEmptyParameter($data, $sortColumns, $expected)
278+
public function testArraySortByMultipleKeysFailsEmptyParameter(array $data, array $sortColumns, array $expected)
291279
{
292280
// Both filled
293281
$success = array_sort_by_multiple_keys($data, $sortColumns);

tests/system/Helpers/FormHelperTest.php

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,26 @@ protected function setUp(): void
2929
parent::setUp();
3030

3131
helper('form');
32+
33+
$this->resetServices();
3234
}
3335

34-
public function testFormOpenBasic()
36+
private function setRequest(): void
3537
{
38+
$uri = new URI('http://example.com/');
39+
Services::injectMock('uri', $uri);
40+
3641
$config = new App();
3742
$config->baseURL = '';
3843
$config->indexPage = 'index.php';
39-
$request = Services::request($config);
40-
$request->uri = new URI('http://example.com/');
4144

45+
$request = Services::request($config);
46+
Services::injectMock('request', $request);
47+
}
48+
49+
public function testFormOpenBasic()
50+
{
51+
$request = $this->setRequest();
4252
Services::injectMock('request', $request);
4353

4454
$before = (new Filters())->globals['before'];
@@ -67,13 +77,8 @@ public function testFormOpenBasic()
6777

6878
public function testFormOpenHasLocale()
6979
{
70-
$config = new App();
71-
$config->baseURL = '';
72-
$config->indexPage = 'index.php';
73-
$request = Services::request($config);
74-
$request->uri = new URI('http://example.com/');
80+
$this->setRequest();
7581

76-
Services::injectMock('request', $request);
7782
$expected = <<<'EOH'
7883
<form action="http://example.com/index.php/en/foo/bar" name="form" id="form" method="POST" accept-charset="utf-8">
7984

@@ -89,13 +94,7 @@ public function testFormOpenHasLocale()
8994

9095
public function testFormOpenWithoutAction()
9196
{
92-
$config = new App();
93-
$config->baseURL = '';
94-
$config->indexPage = 'index.php';
95-
$request = Services::request($config);
96-
$request->uri = new URI('http://example.com/');
97-
98-
Services::injectMock('request', $request);
97+
$this->setRequest();
9998

10099
$before = (new Filters())->globals['before'];
101100
if (in_array('csrf', $before, true) || array_key_exists('csrf', $before)) {
@@ -122,13 +121,7 @@ public function testFormOpenWithoutAction()
122121

123122
public function testFormOpenWithoutMethod()
124123
{
125-
$config = new App();
126-
$config->baseURL = '';
127-
$config->indexPage = 'index.php';
128-
$request = Services::request($config);
129-
$request->uri = new URI('http://example.com/');
130-
131-
Services::injectMock('request', $request);
124+
$this->setRequest();
132125

133126
$before = (new Filters())->globals['before'];
134127
if (in_array('csrf', $before, true) || array_key_exists('csrf', $before)) {
@@ -155,13 +148,7 @@ public function testFormOpenWithoutMethod()
155148

156149
public function testFormOpenWithHidden()
157150
{
158-
$config = new App();
159-
$config->baseURL = '';
160-
$config->indexPage = 'index.php';
161-
$request = Services::request($config);
162-
$request->uri = new URI('http://example.com/');
163-
164-
Services::injectMock('request', $request);
151+
$this->setRequest();
165152

166153
$before = (new Filters())->globals['before'];
167154
if (in_array('csrf', $before, true) || array_key_exists('csrf', $before)) {
@@ -195,13 +182,7 @@ public function testFormOpenWithHidden()
195182

196183
public function testFormOpenMultipart()
197184
{
198-
$config = new App();
199-
$config->baseURL = '';
200-
$config->indexPage = 'index.php';
201-
$request = Services::request($config);
202-
$request->uri = new URI('http://example.com/');
203-
204-
Services::injectMock('request', $request);
185+
$this->setRequest();
205186

206187
$before = (new Filters())->globals['before'];
207188
if (in_array('csrf', $before, true) || array_key_exists('csrf', $before)) {

tests/system/Helpers/URLHelper/CurrentUrlTest.php

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -140,23 +140,28 @@ public function testUriStringAbsolute()
140140
$_SERVER['HTTP_HOST'] = 'example.com';
141141
$_SERVER['REQUEST_URI'] = '/assets/image.jpg';
142142

143-
$request = Services::request($this->config);
144-
$request->uri = new URI('http://example.com/assets/image.jpg');
143+
$uri = 'http://example.com/assets/image.jpg';
144+
$this->setService($uri);
145145

146-
Services::injectMock('request', $request);
146+
$this->assertSame('assets/image.jpg', uri_string());
147+
}
148+
149+
private function setService(string $uri): void
150+
{
151+
$uri = new URI($uri);
152+
Services::injectMock('uri', $uri);
147153

148-
$this->assertSame('/assets/image.jpg', uri_string());
154+
$request = Services::request($this->config);
155+
Services::injectMock('request', $request);
149156
}
150157

151158
public function testUriStringRelative()
152159
{
153160
$_SERVER['HTTP_HOST'] = 'example.com';
154161
$_SERVER['REQUEST_URI'] = '/assets/image.jpg';
155162

156-
$request = Services::request($this->config);
157-
$request->uri = new URI('http://example.com/assets/image.jpg');
158-
159-
Services::injectMock('request', $request);
163+
$uri = 'http://example.com/assets/image.jpg';
164+
$this->setService($uri);
160165

161166
$this->assertSame('assets/image.jpg', uri_string(true));
162167
}
@@ -167,12 +172,11 @@ public function testUriStringNoTrailingSlashAbsolute()
167172
$_SERVER['REQUEST_URI'] = '/assets/image.jpg';
168173

169174
$this->config->baseURL = 'http://example.com';
170-
$request = Services::request($this->config);
171-
$request->uri = new URI('http://example.com/assets/image.jpg');
172175

173-
Services::injectMock('request', $request);
176+
$uri = 'http://example.com/assets/image.jpg';
177+
$this->setService($uri);
174178

175-
$this->assertSame('/assets/image.jpg', uri_string());
179+
$this->assertSame('assets/image.jpg', uri_string());
176180
}
177181

178182
public function testUriStringNoTrailingSlashRelative()
@@ -181,30 +185,25 @@ public function testUriStringNoTrailingSlashRelative()
181185
$_SERVER['REQUEST_URI'] = '/assets/image.jpg';
182186

183187
$this->config->baseURL = 'http://example.com';
184-
$request = Services::request($this->config);
185-
$request->uri = new URI('http://example.com/assets/image.jpg');
186188

187-
Services::injectMock('request', $request);
189+
$uri = 'http://example.com/assets/image.jpg';
190+
$this->setService($uri);
188191

189192
$this->assertSame('assets/image.jpg', uri_string(true));
190193
}
191194

192195
public function testUriStringEmptyAbsolute()
193196
{
194-
$request = Services::request($this->config);
195-
$request->uri = new URI('http://example.com/');
196-
197-
Services::injectMock('request', $request);
197+
$uri = 'http://example.com/';
198+
$this->setService($uri);
198199

199200
$this->assertSame('/', uri_string());
200201
}
201202

202203
public function testUriStringEmptyRelative()
203204
{
204-
$request = Services::request($this->config);
205-
$request->uri = new URI('http://example.com/');
206-
207-
Services::injectMock('request', $request);
205+
$uri = 'http://example.com/';
206+
$this->setService($uri);
208207

209208
$this->assertSame('', uri_string(true));
210209
}
@@ -215,12 +214,11 @@ public function testUriStringSubfolderAbsolute()
215214
$_SERVER['REQUEST_URI'] = '/subfolder/assets/image.jpg';
216215

217216
$this->config->baseURL = 'http://example.com/subfolder/';
218-
$request = Services::request($this->config);
219-
$request->uri = new URI('http://example.com/subfolder/assets/image.jpg');
220217

221-
Services::injectMock('request', $request);
218+
$uri = 'http://example.com/subfolder/assets/image.jpg';
219+
$this->setService($uri);
222220

223-
$this->assertSame('/subfolder/assets/image.jpg', uri_string());
221+
$this->assertSame('subfolder/assets/image.jpg', uri_string());
224222
}
225223

226224
public function testUriStringSubfolderRelative()
@@ -230,10 +228,9 @@ public function testUriStringSubfolderRelative()
230228
$_SERVER['SCRIPT_NAME'] = '/subfolder/index.php';
231229

232230
$this->config->baseURL = 'http://example.com/subfolder/';
233-
$request = Services::request($this->config);
234-
$request->uri = new URI('http://example.com/subfolder/assets/image.jpg');
235231

236-
Services::injectMock('request', $request);
232+
$uri = 'http://example.com/subfolder/assets/image.jpg';
233+
$this->setService($uri);
237234

238235
$this->assertSame('assets/image.jpg', uri_string(true));
239236
}
@@ -287,9 +284,8 @@ public function testUrlIs(string $currentPath, string $testPath, bool $expected)
287284
$_SERVER['HTTP_HOST'] = 'example.com';
288285
$_SERVER['REQUEST_URI'] = '/' . $currentPath;
289286

290-
$request = Services::request();
291-
$request->uri = new URI('http://example.com/' . $currentPath);
292-
Services::injectMock('request', $request);
287+
$uri = 'http://example.com/' . $currentPath;
288+
$this->setService($uri);
293289

294290
$this->assertSame($expected, url_is($testPath));
295291
}
@@ -299,13 +295,13 @@ public function testUrlIs(string $currentPath, string $testPath, bool $expected)
299295
*/
300296
public function testUrlIsNoIndex(string $currentPath, string $testPath, bool $expected)
301297
{
302-
$_SERVER['HTTP_HOST'] = 'example.com';
303-
$_SERVER['REQUEST_URI'] = '/' . $currentPath;
298+
$_SERVER['HTTP_HOST'] = 'example.com';
299+
$_SERVER['REQUEST_URI'] = '/' . $currentPath;
300+
304301
$this->config->indexPage = '';
305302

306-
$request = Services::request($this->config);
307-
$request->uri = new URI('http://example.com/' . $currentPath);
308-
Services::injectMock('request', $request);
303+
$uri = 'http://example.com/' . $currentPath;
304+
$this->setService($uri);
309305

310306
$this->assertSame($expected, url_is($testPath));
311307
}
@@ -318,11 +314,11 @@ public function testUrlIsWithSubfolder(string $currentPath, string $testPath, bo
318314
$_SERVER['HTTP_HOST'] = 'example.com';
319315
$_SERVER['REQUEST_URI'] = '/' . $currentPath;
320316
$_SERVER['SCRIPT_NAME'] = '/subfolder/index.php';
321-
$this->config->baseURL = 'http://example.com/subfolder/';
322317

323-
$request = Services::request($this->config);
324-
$request->uri = new URI('http://example.com/subfolder/' . $currentPath);
325-
Services::injectMock('request', $request);
318+
$this->config->baseURL = 'http://example.com/subfolder/';
319+
320+
$uri = 'http://example.com/subfolder/' . $currentPath;
321+
$this->setService($uri);
326322

327323
$this->assertSame($expected, url_is($testPath));
328324
}

0 commit comments

Comments
 (0)