|
1
|
|
|
<?php |
|
2
|
|
|
namespace Fam\Util; |
|
3
|
|
|
|
|
4
|
|
|
use PHPUnit\Framework\TestCase; |
|
5
|
|
|
|
|
6
|
|
|
class UserAgentParserTest extends TestCase |
|
7
|
|
|
{ |
|
8
|
|
|
/** |
|
9
|
|
|
* @var UserAgentParser |
|
10
|
|
|
*/ |
|
11
|
|
|
private $subject; |
|
12
|
|
|
|
|
13
|
|
|
protected function setUp() |
|
14
|
|
|
{ |
|
15
|
|
|
parent::setUp(); |
|
16
|
|
|
$this->subject = new UserAgentParser(); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @test |
|
21
|
|
|
*/ |
|
22
|
|
|
public function getOperatingSystems() |
|
23
|
|
|
{ |
|
24
|
|
|
$subject = UserAgentParser::createInstance(); |
|
25
|
|
|
$this->assertCount(3, $subject->getOperatingSystems()); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @test |
|
30
|
|
|
* @depends getOperatingSystems |
|
31
|
|
|
*/ |
|
32
|
|
|
public function addOperatingSystem() |
|
33
|
|
|
{ |
|
34
|
|
|
$this->assertCount(0, $this->subject->getOperatingSystems()); |
|
35
|
|
|
|
|
36
|
|
|
$op = $this->getMockBuilder(UserAgentParser\OperatingSystem::class)->getMock(); |
|
37
|
|
|
$this->subject->addOperatingSystem($op); |
|
38
|
|
|
|
|
39
|
|
|
$this->assertCount(1, $this->subject->getOperatingSystems()); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @test |
|
44
|
|
|
* @depends addOperatingSystem |
|
45
|
|
|
*/ |
|
46
|
|
|
public function removeOperatingSystem() |
|
47
|
|
|
{ |
|
48
|
|
|
$op = $this->getMockBuilder(UserAgentParser\OperatingSystem::class)->getMock(); |
|
49
|
|
|
|
|
50
|
|
|
$this->subject->addOperatingSystem($op); |
|
51
|
|
|
$this->assertCount(1, $this->subject->getOperatingSystems()); |
|
52
|
|
|
|
|
53
|
|
|
$this->subject->removeOperatingSystem($op); |
|
54
|
|
|
$this->assertCount(0, $this->subject->getOperatingSystems()); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @test |
|
59
|
|
|
* @depends addOperatingSystem |
|
60
|
|
|
*/ |
|
61
|
|
|
public function removeOperatingSystemByClassName() |
|
62
|
|
|
{ |
|
63
|
|
|
$op = $this->getMockBuilder(UserAgentParser\OperatingSystem::class)->getMock(); |
|
64
|
|
|
|
|
65
|
|
|
$this->subject->addOperatingSystem($op); |
|
66
|
|
|
$this->assertCount(1, $this->subject->getOperatingSystems()); |
|
67
|
|
|
|
|
68
|
|
|
$this->subject->removeOperatingSystemByClassName(get_class($op)); |
|
69
|
|
|
$this->assertCount(0, $this->subject->getOperatingSystems()); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @test |
|
74
|
|
|
*/ |
|
75
|
|
|
public function setGetUndefinedOperatingSystem() |
|
76
|
|
|
{ |
|
77
|
|
|
$op = $this->getMockBuilder(UserAgentParser\OperatingSystem::class)->getMock(); |
|
78
|
|
|
$this->subject->setUndefinedOperatingSystem($op); |
|
79
|
|
|
$this->assertSame($op, $this->subject->getUndefinedOperatingSystem()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @test |
|
84
|
|
|
* @depends removeOperatingSystem |
|
85
|
|
|
* @depends addOperatingSystem |
|
86
|
|
|
*/ |
|
87
|
|
|
public function detectOs_WithValidOperatingSystem() |
|
88
|
|
|
{ |
|
89
|
|
|
$op = $this->getMockBuilder(UserAgentParser\OperatingSystem::class)->getMock(); |
|
90
|
|
|
$op->expects($this->once()) |
|
91
|
|
|
->method('match') |
|
92
|
|
|
->will($this->returnValue(true)); |
|
93
|
|
|
$op->expects($this->once()) |
|
94
|
|
|
->method('getName') |
|
95
|
|
|
->will($this->returnValue(__CLASS__)); |
|
96
|
|
|
$this->subject->addOperatingSystem($op); |
|
97
|
|
|
|
|
98
|
|
|
$userAgent = $this->subject->parseUserAgent("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0"); |
|
99
|
|
|
|
|
100
|
|
|
$this->assertEquals(__CLASS__, $userAgent->os()); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @test |
|
105
|
|
|
* @depends removeOperatingSystem |
|
106
|
|
|
* @depends addOperatingSystem |
|
107
|
|
|
* @depends setGetUndefinedOperatingSystem |
|
108
|
|
|
*/ |
|
109
|
|
|
public function detectOs_WithUndefinedOperatingSystem() |
|
110
|
|
|
{ |
|
111
|
|
|
$op = $this->getMockBuilder(UserAgentParser\OperatingSystem::class)->getMock(); |
|
112
|
|
|
$op->expects($this->once()) |
|
113
|
|
|
->method('match') |
|
114
|
|
|
->will($this->returnValue(false)); |
|
115
|
|
|
$this->subject->addOperatingSystem($op); |
|
116
|
|
|
|
|
117
|
|
|
$undefinedOp = $this->getMockBuilder(UserAgentParser\OperatingSystem::class)->getMock(); |
|
118
|
|
|
$undefinedOp->expects($this->once()) |
|
119
|
|
|
->method('getName') |
|
120
|
|
|
->will($this->returnValue(__CLASS__)); |
|
121
|
|
|
$this->subject->setUndefinedOperatingSystem($undefinedOp); |
|
122
|
|
|
|
|
123
|
|
|
$userAgent = $this->subject->parseUserAgent("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0"); |
|
124
|
|
|
|
|
125
|
|
|
$this->assertEquals(__CLASS__, $userAgent->os()); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @test |
|
130
|
|
|
*/ |
|
131
|
|
|
public function getWebClients() |
|
132
|
|
|
{ |
|
133
|
|
|
$subject = UserAgentParser::createInstance(); |
|
134
|
|
|
$this->assertCount(4, $subject->getWebClients()); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @test |
|
139
|
|
|
* @depends getWebClients |
|
140
|
|
|
*/ |
|
141
|
|
|
public function addWebClient() |
|
142
|
|
|
{ |
|
143
|
|
|
$this->assertCount(0, $this->subject->getWebClients()); |
|
144
|
|
|
|
|
145
|
|
|
$wc = $this->getMockBuilder(UserAgentParser\WebClient::class)->getMock(); |
|
146
|
|
|
$this->subject->addWebClient($wc); |
|
147
|
|
|
|
|
148
|
|
|
$this->assertCount(1, $this->subject->getWebClients()); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @test |
|
153
|
|
|
* @depends addWebClient |
|
154
|
|
|
*/ |
|
155
|
|
|
public function removeWebClient() |
|
156
|
|
|
{ |
|
157
|
|
|
$wc = $this->getMockBuilder(UserAgentParser\WebClient::class)->getMock(); |
|
158
|
|
|
|
|
159
|
|
|
$this->subject->addWebClient($wc); |
|
160
|
|
|
$this->assertCount(1, $this->subject->getWebClients()); |
|
161
|
|
|
|
|
162
|
|
|
$this->subject->removeWebClient($wc); |
|
163
|
|
|
$this->assertCount(0, $this->subject->getWebClients()); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @test |
|
168
|
|
|
* @depends addWebClient |
|
169
|
|
|
*/ |
|
170
|
|
|
public function removeWebClientByClassName() |
|
171
|
|
|
{ |
|
172
|
|
|
$wc = $this->getMockBuilder(UserAgentParser\WebClient::class)->getMock(); |
|
173
|
|
|
|
|
174
|
|
|
$this->subject->addWebClient($wc); |
|
175
|
|
|
$this->assertCount(1, $this->subject->getWebClients()); |
|
176
|
|
|
|
|
177
|
|
|
$this->subject->removeWebClientByClassName(get_class($wc)); |
|
178
|
|
|
$this->assertCount(0, $this->subject->getWebClients()); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @test |
|
183
|
|
|
*/ |
|
184
|
|
|
public function setGetUndefinedWebClient() |
|
185
|
|
|
{ |
|
186
|
|
|
$wc = $this->getMockBuilder(UserAgentParser\WebClient::class)->getMock(); |
|
187
|
|
|
$this->subject->setUndefinedWebClient($wc); |
|
188
|
|
|
$this->assertSame($wc, $this->subject->getUndefinedWebClient()); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @test |
|
193
|
|
|
* @depends removeWebClient |
|
194
|
|
|
* @depends addWebClient |
|
195
|
|
|
*/ |
|
196
|
|
|
public function detectWebClient_WithValidWebClient() |
|
197
|
|
|
{ |
|
198
|
|
|
$wc = $this->getMockBuilder(UserAgentParser\WebClient::class)->getMock(); |
|
199
|
|
|
$wc->expects($this->once()) |
|
200
|
|
|
->method('match') |
|
201
|
|
|
->will($this->returnValue(true)); |
|
202
|
|
|
$wc->expects($this->once()) |
|
203
|
|
|
->method('getName') |
|
204
|
|
|
->will($this->returnValue(__CLASS__)); |
|
205
|
|
|
|
|
206
|
|
|
$this->subject->addWebClient($wc); |
|
207
|
|
|
$userAgent = $this->subject->parseUserAgent("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0"); |
|
208
|
|
|
|
|
209
|
|
|
$this->assertEquals(__CLASS__, $userAgent->webClient()); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @test |
|
214
|
|
|
* @depends removeWebClient |
|
215
|
|
|
* @depends setGetUndefinedWebClient |
|
216
|
|
|
*/ |
|
217
|
|
|
public function detectWebClient_WithInvalidWebClient() |
|
218
|
|
|
{ |
|
219
|
|
|
$wc = $this->getMockBuilder(UserAgentParser\WebClient::class)->getMock(); |
|
220
|
|
|
$wc->expects($this->never()) |
|
221
|
|
|
->method('match') |
|
222
|
|
|
->will($this->returnValue(true)); |
|
223
|
|
|
$wc->expects($this->once()) |
|
224
|
|
|
->method('getName') |
|
225
|
|
|
->will($this->returnValue(__CLASS__)); |
|
226
|
|
|
|
|
227
|
|
|
$this->subject->setUndefinedWebClient($wc); |
|
228
|
|
|
$userAgent = $this->subject->parseUserAgent("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0"); |
|
229
|
|
|
|
|
230
|
|
|
$this->assertEquals(__CLASS__, $userAgent->webClient()); |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|