forked from mozfr/transvision
/
API.php
162 lines (146 loc) · 5.61 KB
/
API.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
namespace tests\units\Transvision;
use atoum;
use Transvision\API as _API;
require_once __DIR__ . '/../bootstrap.php';
class API extends atoum\test
{
public function getParametersDP()
{
return [
[
'http://foobar.com/api/v1/tm/release/en-US/fr/Bookmark/',
["v1", "tm", "release", "en-US", "fr", "Bookmark"],
],
[
'http://foobar.com/api/v1/tm/release/en-US/fr/Vive%20le%20vent/',
["v1", "tm", "release", "en-US", "fr", "Vive le vent"],
],
[
'http://foobar.com/api/v1/tm/release/en-US/fr/%20trailing%20spaces%20',
["v1", "tm", "release", "en-US", "fr", "trailing spaces"],
],
];
}
/**
* @dataProvider getParametersDP
*/
public function testGetParameters($a, $b)
{
$url = parse_url($a);
$obj = new _API($url);
$this
->array($obj->getParameters($url['path']))
->isEqualTo($b);
}
public function getExtraParametersDP()
{
return [
[
'http://foobar.com/api/v1/tm/release/en-US/fr/Bookmark/?foo=bar',
['foo' => 'bar'],
],
[
'http://foobar.com/api/v1/tm/release/en-US/fr/Bookmark/?foo',
['foo' => ''],
],
[
'http://foobar.com/api/v1/tm/release/en-US/fr/Bookmark/?foo=',
['foo' => ''],
],
[
'http://foobar.com/api/v1/tm/release/en-US/fr/Bookmark/?foo=&bar=10',
['foo' => '', 'bar' => 10],
],
[
'http://foobar.com/api/v1/tm/release/en-US/fr/Bookmark/?foo=bar&foo2=bar2',
['foo' => 'bar', 'foo2' => 'bar2'],
],
];
}
/**
* @dataProvider getExtraParametersDP
*/
public function testGetExtraParameters($a, $b)
{
$url = parse_url($a);
$obj = new _API($url);
$this
->array($obj->getExtraParameters($url['query']))
->isEqualTo($b);
}
public function isValidRequestDP()
{
return [
// General
['http://foobar/api/v1/tm/central/en-US/fr/Bookmark/', true],
['http://foobar/api/v1/', false], // Not enough parameters
['http://foobar/api/wrong_version/tm/central/en-US/fr/Bookmark/', false],
['http://foobar/api/v1/wrong_service/central/en-US/fr/hello world', false],
// entity service
['http://foobar/api/v1/entity/central/?id=myid', true],
['http://foobar/api/v1/entity/wrong_repo/', false],
['http://foobar/api/v1/entity/central/', false], // Missing id
// locales service
['http://foobar/api/v1/locales/', false], // Not enough parameters
['http://foobar/api/v1/locales/wrong_repo/', false],
// search service
['http://foobar/api/v1/search/strings/central/en-US/fr/Add%20%20Bookmarks/', true],
['http://foobar/api/v1/search/entities/central/en-US/fr/edit-Bookmark/', true],
['http://foobar/api/v1/search/wrong_search_type/central/en-US/fr/edit-Bookmark/', false],
['http://foobar/api/v1/search/strings/central/en-US/fr/', false], // Not enough parameters
['http://foobar/api/v1/search/strings/wrong_repo/en-US/fr/', false],
['http://foobar/api/v1/search/strings/central/wrong_source/fr/', false],
['http://foobar/api/v1/search/strings/central/en-US/wrong_target/', false],
// tm service
['http://foobar/api/v1/tm/central/en-US/fr/', false], // Not enough parameters
['http://foobar/api/v1/tm/wrong_repo/en-US/fr/hello world', false],
['http://foobar/api/v1/tm/central/wrong_source/fr/hello world', false],
['http://foobar/api/v1/tm/central/en-US/wrong_target/hello world', false],
// repositories service
['http://foobar/api/v1/repositories/', true],
['http://foobar/api/v1/repositories/fr/', true],
['http://foobar/api/v1/repositories/foobar/', false],
['http://foobar/api/v1/repositories/en-US/', true],
// versions service
['http://foobar/api/versions/', true],
];
}
/**
* @dataProvider isValidRequestDP
*/
public function testIsValidRequest($a, $b)
{
$url = parse_url($a);
$obj = new _API($url);
$obj->logging = false; // logging interfers with Atoum
$this
->boolean($obj->isValidRequest())
->isEqualTo($b);
}
public function getServiceDP()
{
return [
['http://foobar/api/v1/', 'Invalid service'],
['http://foobar/api/v1/wrong_service/central/en-US/fr/hello world', 'Invalid service'],
['http://foobar/api/wrong_version/tm/central/en-US/fr/Bookmark/', true],
['http://foobar/api/v1/entity/central/?id=myid', true],
['http://foobar/api/v1/locales/', true],
['http://foobar/api/v1/search/strings/central/en-US/fr/Add%20%20Bookmarks/', true],
['http://foobar/api/v1/tm/central/en-US/fr/', true],
['http://foobar/api/versions/', true],
];
}
/**
* @dataProvider getServiceDP
*/
public function testGetService($a, $b)
{
$url = parse_url($a);
$obj = new _API($url);
$obj->logging = false; // logging interfers with Atoum
$this
->variable($obj->getService())
->isEqualTo($b);
}
}