Skip to content

Commit 200cd8d

Browse files
committed
test: extract method
1 parent 8567b70 commit 200cd8d

1 file changed

Lines changed: 36 additions & 56 deletions

File tree

tests/system/Helpers/URLHelper/MiscUrlTest.php

Lines changed: 36 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,22 @@ public function testPreviousURLUsesSessionFirst()
5555
$_SERVER['HTTP_REFERER'] = $uri1;
5656
$_SESSION['_ci_previous_url'] = $uri2;
5757

58-
$uri = new URI('http://example.com/public');
58+
$this->config->baseURL = 'http://example.com/public';
59+
60+
$uri = 'http://example.com/public';
61+
$this->setRequest($uri);
62+
63+
$this->assertSame($uri2, previous_url());
64+
}
65+
66+
private function setRequest(string $uri): void
67+
{
68+
$uri = new URI($uri);
5969
Services::injectMock('uri', $uri);
6070

6171
// Since we're on a CLI, we must provide our own URI
62-
$this->config->baseURL = 'http://example.com/public';
63-
$request = Services::request($this->config);
72+
$request = Services::request($this->config);
6473
Services::injectMock('request', $request);
65-
66-
$this->assertSame($uri2, previous_url());
6774
}
6875

6976
public function testPreviousURLUsesRefererIfNeeded()
@@ -72,13 +79,10 @@ public function testPreviousURLUsesRefererIfNeeded()
7279

7380
$_SERVER['HTTP_REFERER'] = $uri1;
7481

75-
$uri = new URI('http://example.com/public');
76-
Services::injectMock('uri', $uri);
77-
78-
// Since we're on a CLI, we must provide our own URI
7982
$this->config->baseURL = 'http://example.com/public';
80-
$request = Services::request($this->config);
81-
Services::injectMock('request', $request);
83+
84+
$uri = 'http://example.com/public';
85+
$this->setRequest($uri);
8286

8387
$this->assertSame($uri1, previous_url());
8488
}
@@ -87,23 +91,18 @@ public function testPreviousURLUsesRefererIfNeeded()
8791

8892
public function testIndexPage()
8993
{
90-
$uri = new URI('http://example.com/');
91-
Services::injectMock('uri', $uri);
92-
93-
$request = Services::request($this->config);
94-
Services::injectMock('request', $request);
94+
$uri = 'http://example.com/';
95+
$this->setRequest($uri);
9596

9697
$this->assertSame('index.php', index_page());
9798
}
9899

99100
public function testIndexPageAlt()
100101
{
101-
$uri = new URI('http://example.com/');
102-
Services::injectMock('uri', $uri);
103-
104102
$this->config->indexPage = 'banana.php';
105-
$request = Services::request($this->config);
106-
Services::injectMock('request', $request);
103+
104+
$uri = 'http://example.com/';
105+
$this->setRequest($uri);
107106

108107
$this->assertSame('banana.php', index_page($this->config));
109108
}
@@ -163,11 +162,8 @@ public function anchorNormalPatterns()
163162
*/
164163
public function testAnchor($expected = '', $uri = '', $title = '', $attributes = '')
165164
{
166-
$uriObj = new URI('http://example.com/');
167-
Services::injectMock('uri', $uriObj);
168-
169-
$request = Services::request($this->config);
170-
Services::injectMock('request', $request);
165+
$uriString = 'http://example.com/';
166+
$this->setRequest($uriString);
171167

172168
$this->assertSame($expected, anchor($uri, $title, $attributes, $this->config));
173169
}
@@ -231,12 +227,10 @@ public function anchorNoindexPatterns()
231227
*/
232228
public function testAnchorNoindex($expected = '', $uri = '', $title = '', $attributes = '')
233229
{
234-
$uriObj = new URI('http://example.com/');
235-
Services::injectMock('uri', $uriObj);
236-
237230
$this->config->indexPage = '';
238-
$request = Services::request($this->config);
239-
Services::injectMock('request', $request);
231+
232+
$uriString = 'http://example.com/';
233+
$this->setRequest($uriString);
240234

241235
$this->assertSame($expected, anchor($uri, $title, $attributes, $this->config));
242236
}
@@ -290,12 +284,10 @@ public function anchorSubpagePatterns()
290284
*/
291285
public function testAnchorTargetted($expected = '', $uri = '', $title = '', $attributes = '')
292286
{
293-
$uriObj = new URI('http://example.com/');
294-
Services::injectMock('uri', $uriObj);
295-
296287
$this->config->indexPage = '';
297-
$request = Services::request($this->config);
298-
Services::injectMock('request', $request);
288+
289+
$uriString = 'http://example.com/';
290+
$this->setRequest($uriString);
299291

300292
$this->assertSame($expected, anchor($uri, $title, $attributes, $this->config));
301293
}
@@ -338,11 +330,8 @@ public function anchorExamplePatterns()
338330
*/
339331
public function testAnchorExamples($expected = '', $uri = '', $title = '', $attributes = '')
340332
{
341-
$uriObj = new URI('http://example.com/');
342-
Services::injectMock('uri', $uriObj);
343-
344-
$request = Services::request($this->config);
345-
Services::injectMock('request', $request);
333+
$uriString = 'http://example.com/';
334+
$this->setRequest($uriString);
346335

347336
$this->assertSame($expected, anchor($uri, $title, $attributes, $this->config));
348337
}
@@ -399,11 +388,8 @@ public function anchorPopupPatterns()
399388
*/
400389
public function testAnchorPopup($expected = '', $uri = '', $title = '', $attributes = false)
401390
{
402-
$uriObj = new URI('http://example.com/');
403-
Services::injectMock('uri', $uriObj);
404-
405-
$request = Services::request($this->config);
406-
Services::injectMock('request', $request);
391+
$uriString = 'http://example.com/';
392+
$this->setRequest($uriString);
407393

408394
$this->assertSame($expected, anchor_popup($uri, $title, $attributes, $this->config));
409395
}
@@ -441,11 +427,8 @@ public function mailtoPatterns()
441427
*/
442428
public function testMailto($expected = '', $email = '', $title = '', $attributes = '')
443429
{
444-
$uri = new URI('http://example.com/');
445-
Services::injectMock('uri', $uri);
446-
447-
$request = Services::request($this->config);
448-
Services::injectMock('request', $request);
430+
$uriString = 'http://example.com/';
431+
$this->setRequest($uriString);
449432

450433
$this->assertSame($expected, mailto($email, $title, $attributes));
451434
}
@@ -483,11 +466,8 @@ public function safeMailtoPatterns()
483466
*/
484467
public function testSafeMailto($expected = '', $email = '', $title = '', $attributes = '')
485468
{
486-
$uri = new URI('http://example.com/');
487-
Services::injectMock('uri', $uri);
488-
489-
$request = Services::incomingrequest($this->config);
490-
Services::injectMock('request', $request);
469+
$uriString = 'http://example.com/';
470+
$this->setRequest($uriString);
491471

492472
$this->assertSame($expected, safe_mailto($email, $title, $attributes));
493473
}

0 commit comments

Comments
 (0)