Skip to content

Commit 23deddd

Browse files
committed
tests: Api::substituteUrlParameters() in standalone test file
1 parent 8f0dd49 commit 23deddd

2 files changed

Lines changed: 56 additions & 51 deletions

File tree

tests/Github/Api.phpt

Lines changed: 4 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ require __DIR__ . '/../bootstrap.php';
99

1010
class MockIClient implements Milo\Github\Http\IClient
1111
{
12-
/** @return Milo\Github\Http\Response */
1312
public function request(Milo\Github\Http\Request $request)
1413
{
1514
throw new \LogicException;
@@ -19,20 +18,11 @@ class MockIClient implements Milo\Github\Http\IClient
1918
public function onResponse($cb) {}
2019
}
2120

22-
class TestApi extends Milo\Github\Api
23-
{
24-
public function substituteUrlParameters(& $url, array & $parameters)
25-
{
26-
return parent::substituteUrlParameters($url, $parameters);
27-
}
28-
}
29-
30-
3121

3222
# Basics
3323
test(function() {
3424
$client = new MockIClient;
35-
$api = new TestApi($client);
25+
$api = new Milo\Github\Api($client);
3626

3727
Assert::same($client, $api->getClient());
3828
Assert::same('https://api.github.com', $api->getUrl());
@@ -48,47 +38,10 @@ test(function() {
4838
});
4939

5040

51-
# substituteUrl()
52-
test(function() {
53-
$client = new MockIClient;
54-
$api = new TestApi($client);
55-
56-
$urls = [
57-
'' => ['', ['a' => 'A', 'b' => 'B']],
58-
'/' => ['/', ['a' => 'A', 'b' => 'B']],
59-
':a' => ['A', ['b' => 'B']],
60-
'/:a' => ['/A', ['b' => 'B']],
61-
':a/' => ['A/', ['b' => 'B']],
62-
'/:a/' => ['/A/', ['b' => 'B']],
63-
'/:a/:b/c' => ['/A/B/c', []],
64-
];
65-
66-
foreach ($urls as $url => $result) {
67-
$params = ['a' => 'A', 'b' => 'B'];
68-
$api->substituteUrlParameters($url, $params);
69-
70-
Assert::same($url, $result[0]);
71-
Assert::same($params, $result[1]);
72-
}
73-
74-
Assert::exception(function() use ($api) {
75-
$url = ':a';
76-
$params = ['A' => 'a'];
77-
$api->substituteUrlParameters($url, $params);
78-
}, 'Milo\Github\MissingParameterException', "Missing parameter 'a' for URL path ':a'.");
79-
80-
Assert::exception(function() use ($api) {
81-
$url = ':a:b';
82-
$params = ['a' => 'A', 'b' => 'B'];
83-
$api->substituteUrlParameters($url, $params);
84-
}, 'Milo\Github\MissingParameterException', "Missing parameter 'a:b' for URL path ':a:b'.");
85-
});
86-
87-
8841
# createRequest()
8942
test(function() {
9043
$client = new MockIClient;
91-
$api = new TestApi($client);
44+
$api = new Milo\Github\Api($client);
9245

9346
# All slashes in URL
9447
$api->setUrl('url://test/');
@@ -117,7 +70,7 @@ test(function() {
11770
# Default parameters
11871
test(function() {
11972
$client = new MockIClient;
120-
$api = new TestApi($client);
73+
$api = new Milo\Github\Api($client);
12174
$api->setUrl('url://test');
12275

12376
Assert::same([], $api->getDefaultParameters());
@@ -140,7 +93,7 @@ test(function() {
14093
# Paginator
14194
test(function() {
14295
$client = new MockIClient;
143-
$api = new TestApi($client);
96+
$api = new Milo\Github\Api($client);
14497

14598
Assert::type('Milo\Github\Paginator', $api->paginator(''));
14699
});
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/**
4+
* @author Miloslav Hůla
5+
*/
6+
7+
require __DIR__ . '/../bootstrap.php';
8+
9+
10+
class TestApi extends Milo\Github\Api
11+
{
12+
public function substituteUrlParameters(& $url, array & $parameters)
13+
{
14+
return parent::substituteUrlParameters($url, $parameters);
15+
}
16+
}
17+
18+
19+
# substituteUrl()
20+
test(function() {
21+
$api = new TestApi;
22+
23+
$urls = [
24+
'' => ['', ['a' => 'A', 'b' => 'B']],
25+
'/' => ['/', ['a' => 'A', 'b' => 'B']],
26+
':a' => ['A', ['b' => 'B']],
27+
'/:a' => ['/A', ['b' => 'B']],
28+
':a/' => ['A/', ['b' => 'B']],
29+
'/:a/' => ['/A/', ['b' => 'B']],
30+
'/:a/:b/c' => ['/A/B/c', []],
31+
];
32+
33+
foreach ($urls as $url => $result) {
34+
$params = ['a' => 'A', 'b' => 'B'];
35+
$api->substituteUrlParameters($url, $params);
36+
37+
Assert::same($url, $result[0]);
38+
Assert::same($params, $result[1]);
39+
}
40+
41+
Assert::exception(function() use ($api) {
42+
$url = ':a';
43+
$params = ['A' => 'a'];
44+
$api->substituteUrlParameters($url, $params);
45+
}, 'Milo\Github\MissingParameterException', "Missing parameter 'a' for URL path ':a'.");
46+
47+
Assert::exception(function() use ($api) {
48+
$url = ':a:b';
49+
$params = ['a' => 'A', 'b' => 'B'];
50+
$api->substituteUrlParameters($url, $params);
51+
}, 'Milo\Github\MissingParameterException', "Missing parameter 'a:b' for URL path ':a:b'.");
52+
});

0 commit comments

Comments
 (0)