|
1
|
|
|
<?php |
|
2
|
|
|
namespace TraderInteractive\SolveMedia; |
|
3
|
|
|
|
|
4
|
|
|
use GuzzleHttp\ClientInterface; |
|
5
|
|
|
use GuzzleHttp\Psr7\Response; |
|
|
|
|
|
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @coversDefaultClass \TraderInteractive\SolveMedia\Service |
|
10
|
|
|
* @covers ::__construct |
|
11
|
|
|
*/ |
|
12
|
|
|
class ServiceTest extends TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
private $_realGuzzleClient; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @test |
|
18
|
|
|
* @dataProvider constructWithInvalidArgumentsData |
|
19
|
|
|
* @expectedException Exception |
|
20
|
|
|
* @covers ::__construct |
|
21
|
|
|
*/ |
|
22
|
|
|
public function constructWithInvalidArguments($pubkey, $privkey, $hashkey) |
|
23
|
|
|
{ |
|
24
|
|
|
new Service($this->getGuzzleClient(), $pubkey, $privkey, $hashkey); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function constructWithInvalidArgumentsData() |
|
28
|
|
|
{ |
|
29
|
|
|
return [ |
|
30
|
|
|
'empty pubKey' => ['', 'privKey', ''], |
|
31
|
|
|
'empty privKey' => ['pubKey', '', ''], |
|
32
|
|
|
]; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @test |
|
37
|
|
|
* @covers ::getHtml |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getHtmlDefault() |
|
40
|
|
|
{ |
|
41
|
|
|
$pubkey = 'MyTestPubKeyStringToTestFor'; |
|
42
|
|
|
$service = new Service($this->getGuzzleClient(), $pubkey, 'notest'); |
|
43
|
|
|
|
|
44
|
|
|
$html = $service->getHtml(); |
|
45
|
|
|
$this->assertRegExp("/k={$pubkey}/", $html); |
|
46
|
|
|
$this->assertNotRegExp('/;error=1/', $html); |
|
47
|
|
|
$this->assertRegExp('/' . preg_quote(Service::ADCOPY_API_SERVER, '/') . '/', $html); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @test |
|
52
|
|
|
* @covers ::getHtml |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getHtmlWithArguments() |
|
55
|
|
|
{ |
|
56
|
|
|
$service = new Service($this->getGuzzleClient(), 'notest', 'notest'); |
|
57
|
|
|
$html = $service->getHtml('test', true); |
|
58
|
|
|
$this->assertRegExp('/;error=1/', $html); |
|
59
|
|
|
$this->assertRegExp('/' . preg_quote(Service::ADCOPY_API_SECURE_SERVER, '/') . '/', $html); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @test |
|
64
|
|
|
* @dataProvider checkAnswerNoRemoteIpData |
|
65
|
|
|
* @expectedException Exception |
|
66
|
|
|
* @covers ::checkAnswer |
|
67
|
|
|
*/ |
|
68
|
|
|
public function checkAnswerNoRemoteIp($remoteIp) |
|
69
|
|
|
{ |
|
70
|
|
|
$service = new Service($this->getGuzzleClient(), 'notest', 'notest'); |
|
71
|
|
|
$service->checkAnswer($remoteIp, null, null); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function checkAnswerNoRemoteIpData() |
|
75
|
|
|
{ |
|
76
|
|
|
return [ |
|
77
|
|
|
[''], |
|
78
|
|
|
]; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @test |
|
83
|
|
|
* @dataProvider checkAnswerEmptyArgumentsData |
|
84
|
|
|
* @covers ::checkAnswer |
|
85
|
|
|
*/ |
|
86
|
|
View Code Duplication |
public function checkAnswerEmptyArguments($challenge, $response) |
|
|
|
|
|
|
87
|
|
|
{ |
|
88
|
|
|
$service = new Service($this->getGuzzleClient(), 'notest', 'notest'); |
|
89
|
|
|
$response = $service->checkAnswer('notest', $challenge, $response); |
|
90
|
|
|
|
|
91
|
|
|
$this->assertInstanceOf('\TraderInteractive\SolveMedia\Response', $response); |
|
92
|
|
|
$this->assertFalse($response->valid()); |
|
93
|
|
|
$this->assertSame('incorrect-solution', $response->getMessage()); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function checkAnswerEmptyArgumentsData() |
|
97
|
|
|
{ |
|
98
|
|
|
return [ |
|
99
|
|
|
[null, null], |
|
100
|
|
|
['', null], |
|
101
|
|
|
[0, null], |
|
102
|
|
|
[false, null], |
|
103
|
|
|
['test', null], |
|
104
|
|
|
['test', ''], |
|
105
|
|
|
['test', 0], |
|
106
|
|
|
['test', false], |
|
107
|
|
|
]; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @test |
|
112
|
|
|
* @dataProvider checkAnswerErrorResponseData |
|
113
|
|
|
* @covers ::checkAnswer |
|
114
|
|
|
*/ |
|
115
|
|
View Code Duplication |
public function checkAnswerErrorResponse($hashKey, Response $guzzleResponse, $message) |
|
|
|
|
|
|
116
|
|
|
{ |
|
117
|
|
|
$service = new Service($this->getGuzzleClient($guzzleResponse), 'notest', 'notest', $hashKey); |
|
118
|
|
|
$response = $service->checkAnswer('notest', 'foo', 'bar'); |
|
119
|
|
|
$this->assertFalse($response->valid()); |
|
120
|
|
|
$this->assertSame($message, $response->getMessage()); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function checkAnswerErrorResponseData() |
|
124
|
|
|
{ |
|
125
|
|
|
return [ |
|
126
|
|
|
['', new Response(400), 'Bad Request'], |
|
127
|
|
|
['', new Response(200, [], "false\nfailure-message"), 'failure-message'], |
|
128
|
|
|
['hashKey', new Response(200, [], "true\nfailure-message\nnot-the-right-hash"), 'hash-fail'], |
|
129
|
|
|
['hashKey', new Response(200, [], "false\nfailure-message\nnot-the-right-hash"), 'hash-fail'], |
|
130
|
|
|
['hashKey', new Response(200, [], "false\nfailure-message\n" . sha1('falsefoohashKey')), 'failure-message'], |
|
131
|
|
|
]; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @test |
|
136
|
|
|
* @dataProvider checkAnswerValidResponseData |
|
137
|
|
|
* @covers ::checkAnswer |
|
138
|
|
|
*/ |
|
139
|
|
|
public function checkAnswerValidResponse($hashKey, Response $guzzleResponse) |
|
140
|
|
|
{ |
|
141
|
|
|
$service = new Service($this->getGuzzleClient($guzzleResponse), 'notest', 'notest', $hashKey); |
|
142
|
|
|
$response = $service->checkAnswer('notest', 'foo', 'bar'); |
|
143
|
|
|
$this->assertTrue($response->valid()); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function checkAnswerValidResponseData() |
|
147
|
|
|
{ |
|
148
|
|
|
return [ |
|
149
|
|
|
['', new Response(200, [], 'true')], |
|
150
|
|
|
['hashKey', new Response(200, [], "true\n\n" . sha1('truefoohashKey'))], |
|
151
|
|
|
]; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @test |
|
156
|
|
|
* @covers ::getSignupUrl |
|
157
|
|
|
*/ |
|
158
|
|
|
public function getSignupUrl() |
|
159
|
|
|
{ |
|
160
|
|
|
$service = new Service($this->getGuzzleClient(), 'notest', 'notest'); |
|
161
|
|
|
$this->assertNotEmpty($service->getSignupUrl()); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
private function getGuzzleClient(Response $response = null) : ClientInterface |
|
165
|
|
|
{ |
|
166
|
|
|
$mock = $this->getMockBuilder(ClientInterface::class)->getMock(); |
|
167
|
|
|
$mock->method('request')->willReturn($response); |
|
168
|
|
|
return $mock; |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: