1
|
|
|
<?php |
2
|
|
|
namespace TraderInteractive\SolveMedia; |
3
|
|
|
|
4
|
|
|
use Guzzle\Http\Client as GuzzleClient; |
5
|
|
|
use Guzzle\Http\Message\Response as GuzzleResponse; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @coversDefaultClass \TraderInteractive\SolveMedia\Service |
10
|
|
|
*/ |
11
|
|
|
class ServiceTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
private $_realGuzzleClient; |
14
|
|
|
|
15
|
|
|
public function setUp() |
16
|
|
|
{ |
17
|
|
|
$this->_realGuzzleClient = new GuzzleClient(); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @test |
22
|
|
|
* @dataProvider constructWithInvalidArgumentsData |
23
|
|
|
* @expectedException Exception |
24
|
|
|
* @covers ::__construct |
25
|
|
|
*/ |
26
|
|
|
public function constructWithInvalidArguments($pubkey, $privkey, $hashkey) |
27
|
|
|
{ |
28
|
|
|
new Service($this->_realGuzzleClient, $pubkey, $privkey, $hashkey); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function constructWithInvalidArgumentsData() |
32
|
|
|
{ |
33
|
|
|
return [ |
34
|
|
|
[$this->_realGuzzleClient, null, null, null], |
35
|
|
|
[$this->_realGuzzleClient, '', null, null], |
36
|
|
|
[$this->_realGuzzleClient, 0, null, null], |
37
|
|
|
[$this->_realGuzzleClient, false, null, null], |
38
|
|
|
[$this->_realGuzzleClient, 'test', null, null], |
39
|
|
|
[$this->_realGuzzleClient, 'test', '', null], |
40
|
|
|
[$this->_realGuzzleClient, 'test', 0, null], |
41
|
|
|
[$this->_realGuzzleClient, 'test', false, null], |
42
|
|
|
]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @test |
47
|
|
|
* @covers ::__construct |
48
|
|
|
*/ |
49
|
|
|
public function constructWithValidArguments() |
50
|
|
|
{ |
51
|
|
|
$this->assertNotNull(new Service($this->_realGuzzleClient, 'test', 'test')); |
52
|
|
|
$this->assertNotNull(new Service($this->_realGuzzleClient, 'test', 'test', 'test')); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @test |
57
|
|
|
* @covers ::getHtml |
58
|
|
|
*/ |
59
|
|
|
public function getHtmlDefault() |
60
|
|
|
{ |
61
|
|
|
$client = new GuzzleClient(); |
|
|
|
|
62
|
|
|
$pubkey = 'MyTestPubKeyStringToTestFor'; |
63
|
|
|
$service = new Service($this->_realGuzzleClient, $pubkey, 'notest'); |
64
|
|
|
|
65
|
|
|
$html = $service->getHtml(); |
66
|
|
|
$this->assertRegExp("/k={$pubkey}/", $html); |
67
|
|
|
$this->assertNotRegExp('/;error=1/', $html); |
68
|
|
|
$this->assertRegExp('/' . preg_quote(Service::ADCOPY_API_SERVER, '/') . '/', $html); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @test |
73
|
|
|
* @covers ::getHtml |
74
|
|
|
*/ |
75
|
|
|
public function getHtmlWithArguments() |
76
|
|
|
{ |
77
|
|
|
$service = new Service($this->_realGuzzleClient, 'notest', 'notest'); |
78
|
|
|
$html = $service->getHtml('test', true); |
79
|
|
|
$this->assertRegExp('/;error=1/', $html); |
80
|
|
|
$this->assertRegExp('/' . preg_quote(Service::ADCOPY_API_SECURE_SERVER, '/') . '/', $html); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @test |
85
|
|
|
* @dataProvider checkAnswerNoRemoteIpData |
86
|
|
|
* @expectedException Exception |
87
|
|
|
* @covers ::checkAnswer |
88
|
|
|
*/ |
89
|
|
|
public function checkAnswerNoRemoteIp($remoteIp) |
90
|
|
|
{ |
91
|
|
|
$service = new Service($this->_realGuzzleClient, 'notest', 'notest'); |
92
|
|
|
$service->checkAnswer($remoteIp, null, null); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function checkAnswerNoRemoteIpData() |
96
|
|
|
{ |
97
|
|
|
return [ |
98
|
|
|
[null], |
99
|
|
|
[''], |
100
|
|
|
]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @test |
105
|
|
|
* @dataProvider checkAnswerEmptyArgumentsData |
106
|
|
|
* @covers ::checkAnswer |
107
|
|
|
*/ |
108
|
|
|
public function checkAnswerEmptyArguments($challenge, $response) |
109
|
|
|
{ |
110
|
|
|
$service = new Service($this->_realGuzzleClient, 'notest', 'notest'); |
111
|
|
|
$response = $service->checkAnswer('notest', $challenge, $response); |
112
|
|
|
|
113
|
|
|
$this->assertInstanceOf('\TraderInteractive\SolveMedia\Response', $response); |
114
|
|
|
$this->assertFalse($response->valid()); |
115
|
|
|
$this->assertSame('incorrect-solution', $response->getMessage()); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function checkAnswerEmptyArgumentsData() |
119
|
|
|
{ |
120
|
|
|
return [ |
121
|
|
|
[null, null], |
122
|
|
|
['', null], |
123
|
|
|
[0, null], |
124
|
|
|
[false, null], |
125
|
|
|
['test', null], |
126
|
|
|
['test', ''], |
127
|
|
|
['test', 0], |
128
|
|
|
['test', false], |
129
|
|
|
]; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @test |
134
|
|
|
* @dataProvider checkAnswerErrorResponseData |
135
|
|
|
* @covers ::checkAnswer |
136
|
|
|
*/ |
137
|
|
View Code Duplication |
public function checkAnswerErrorResponse($hashKey, GuzzleResponse $guzzleResponse, $message) |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$guzzleRequest = $this->getMockForAbstractClass('\Guzzle\Http\Message\RequestInterface'); |
140
|
|
|
$guzzleRequest->expects($this->once())->method('send')->will($this->returnValue($guzzleResponse)); |
141
|
|
|
|
142
|
|
|
$guzzleClient = $this->getMockForAbstractClass('\Guzzle\Http\ClientInterface'); |
143
|
|
|
$guzzleClient->expects($this->once())->method('post')->will($this->returnValue($guzzleRequest)); |
144
|
|
|
|
145
|
|
|
$service = new Service($guzzleClient, 'notest', 'notest', $hashKey); |
|
|
|
|
146
|
|
|
$response = $service->checkAnswer('notest', 'foo', 'bar'); |
147
|
|
|
$this->assertFalse($response->valid()); |
148
|
|
|
$this->assertSame($message, $response->getMessage()); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function checkAnswerErrorResponseData() |
152
|
|
|
{ |
153
|
|
|
return [ |
154
|
|
|
['', new GuzzleResponse(400), 'Bad Request'], |
155
|
|
|
['', new GuzzleResponse(200, [], "false\nfailure-message"), 'failure-message'], |
156
|
|
|
['hashKey', new GuzzleResponse(200, [], "true\nfailure-message\nnot-the-right-hash"), 'hash-fail'], |
157
|
|
|
['hashKey', new GuzzleResponse(200, [], "false\nfailure-message\nnot-the-right-hash"), 'hash-fail'], |
158
|
|
|
['hashKey', new GuzzleResponse(200, [], "false\nfailure-message\n" . sha1('falsefoohashKey')), 'failure-message'], |
159
|
|
|
]; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @test |
164
|
|
|
* @dataProvider checkAnswerValidResponseData |
165
|
|
|
* @covers ::checkAnswer |
166
|
|
|
*/ |
167
|
|
View Code Duplication |
public function checkAnswerValidResponse($hashKey, GuzzleResponse $guzzleResponse) |
|
|
|
|
168
|
|
|
{ |
169
|
|
|
$guzzleRequest = $this->getMockForAbstractClass('\Guzzle\Http\Message\RequestInterface'); |
170
|
|
|
$guzzleRequest->expects($this->once())->method('send')->will($this->returnValue($guzzleResponse)); |
171
|
|
|
|
172
|
|
|
$guzzleClient = $this->getMockForAbstractClass('\Guzzle\Http\ClientInterface'); |
173
|
|
|
$guzzleClient->expects($this->once())->method('post')->will($this->returnValue($guzzleRequest)); |
174
|
|
|
|
175
|
|
|
$service = new Service($guzzleClient, 'notest', 'notest', $hashKey); |
|
|
|
|
176
|
|
|
$response = $service->checkAnswer('notest', 'foo', 'bar'); |
177
|
|
|
$this->assertTrue($response->valid()); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function checkAnswerValidResponseData() |
181
|
|
|
{ |
182
|
|
|
return [ |
183
|
|
|
['', new GuzzleResponse(200, [], 'true')], |
184
|
|
|
['hashKey', new GuzzleResponse(200, [], "true\n\n" . sha1('truefoohashKey'))], |
185
|
|
|
]; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @test |
190
|
|
|
* @covers ::getSignupUrl |
191
|
|
|
*/ |
192
|
|
|
public function getSignupUrl() |
193
|
|
|
{ |
194
|
|
|
$service = new Service($this->_realGuzzleClient, 'notest', 'notest'); |
195
|
|
|
$this->assertNotEmpty($service->getSignupUrl()); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.