Completed
Pull Request — master (#10)
by Chad
03:58
created

ServiceTest::checkAnswerValidResponseData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
namespace TraderInteractive\SolveMedia;
3
use Guzzle\Http\Client as GuzzleClient;
4
use Guzzle\Http\Message\Response as GuzzleResponse;
5
6
/**
7
 * @coversDefaultClass \TraderInteractive\SolveMedia\Service
8
 */
9
class ServiceTest extends \PHPUnit_Framework_TestCase
10
{
11
    private $_realGuzzleClient;
12
13
    public function setUp()
14
    {
15
        $this->_realGuzzleClient = new GuzzleClient();
16
    }
17
18
    /**
19
     * @test
20
     * @dataProvider constructWithInvalidArgumentsData
21
     * @expectedException Exception
22
     * @covers ::__construct
23
     */
24
    public function constructWithInvalidArguments($pubkey, $privkey, $hashkey)
25
    {
26
        new Service($this->_realGuzzleClient, $pubkey, $privkey, $hashkey);
27
    }
28
29
    public function constructWithInvalidArgumentsData()
30
    {
31
        return [
32
            [$this->_realGuzzleClient, null, null, null],
33
            [$this->_realGuzzleClient, '', null, null],
34
            [$this->_realGuzzleClient, 0, null, null],
35
            [$this->_realGuzzleClient, false, null, null],
36
            [$this->_realGuzzleClient, 'test', null, null],
37
            [$this->_realGuzzleClient, 'test', '', null],
38
            [$this->_realGuzzleClient, 'test', 0, null],
39
            [$this->_realGuzzleClient, 'test', false, null],
40
        ];
41
    }
42
43
    /**
44
     * @test
45
     * @covers ::__construct
46
     */
47
    public function constructWithValidArguments()
48
    {
49
        $this->assertNotNull(new Service($this->_realGuzzleClient, 'test', 'test'));
50
        $this->assertNotNull(new Service($this->_realGuzzleClient, 'test', 'test', 'test'));
51
    }
52
53
    /**
54
     * @test
55
     * @uses \TraderInteractive\SolveMedia\Service::__construct
56
     * @covers ::getHtml
57
     */
58
    public function getHtmlDefault()
59
    {
60
        $client = new GuzzleClient();
0 ignored issues
show
Unused Code introduced by
$client is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
61
        $pubkey = 'MyTestPubKeyStringToTestFor';
62
        $service = new Service($this->_realGuzzleClient, $pubkey, 'notest');
63
64
        $html = $service->getHtml();
65
        $this->assertRegExp("/k={$pubkey}/", $html);
66
        $this->assertNotRegExp('/;error=1/', $html);
67
        $this->assertRegExp('/' . preg_quote(Service::ADCOPY_API_SERVER, '/') . '/', $html);
68
    }
69
70
    /**
71
     * @test
72
     * @uses \TraderInteractive\SolveMedia\Service::__construct
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
     * @uses \TraderInteractive\SolveMedia\Service::__construct
86
     * @dataProvider checkAnswerNoRemoteIpData
87
     * @expectedException Exception
88
     * @covers ::checkAnswer
89
     */
90
    public function checkAnswerNoRemoteIp($remoteIp)
91
    {
92
        $service = new Service($this->_realGuzzleClient, 'notest', 'notest');
93
        $service->checkAnswer($remoteIp, null, null);
94
    }
95
96
    public function checkAnswerNoRemoteIpData()
97
    {
98
        return [
99
            [null],
100
            [''],
101
        ];
102
    }
103
104
    /**
105
     * @test
106
     * @uses \TraderInteractive\SolveMedia\Service::__construct
107
     * @uses \TraderInteractive\SolveMedia\Response::<public>
108
     * @dataProvider checkAnswerEmptyArgumentsData
109
     * @covers ::checkAnswer
110
     */
111
    public function checkAnswerEmptyArguments($challenge, $response)
112
    {
113
        $service = new Service($this->_realGuzzleClient, 'notest', 'notest');
114
        $response = $service->checkAnswer('notest', $challenge, $response);
115
116
        $this->assertInstanceOf('\TraderInteractive\SolveMedia\Response', $response);
117
        $this->assertFalse($response->valid());
118
        $this->assertSame('incorrect-solution', $response->getMessage());
119
    }
120
121
    public function checkAnswerEmptyArgumentsData()
122
    {
123
        return [
124
            [null, null],
125
            ['', null],
126
            [0, null],
127
            [false, null],
128
            ['test', null],
129
            ['test', ''],
130
            ['test', 0],
131
            ['test', false],
132
        ];
133
    }
134
135
    /**
136
     * @test
137
     * @uses \TraderInteractive\SolveMedia\Service::__construct
138
     * @uses \TraderInteractive\SolveMedia\Response::__construct
139
     * @uses \TraderInteractive\SolveMedia\Response::valid
140
     * @uses \TraderInteractive\SolveMedia\Response::getMessage
141
     * @dataProvider checkAnswerErrorResponseData
142
     * @covers ::checkAnswer
143
     */
144 View Code Duplication
    public function checkAnswerErrorResponse($hashKey, GuzzleResponse $guzzleResponse, $message)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
145
    {
146
        $guzzleRequest = $this->getMockForAbstractClass('\Guzzle\Http\Message\RequestInterface');
147
        $guzzleRequest->expects($this->once())->method('send')->will($this->returnValue($guzzleResponse));
148
149
        $guzzleClient = $this->getMockForAbstractClass('\Guzzle\Http\ClientInterface');
150
        $guzzleClient->expects($this->once())->method('post')->will($this->returnValue($guzzleRequest));
151
152
        $service = new Service($guzzleClient, 'notest', 'notest', $hashKey);
153
        $response = $service->checkAnswer('notest', 'foo', 'bar');
154
        $this->assertFalse($response->valid());
155
        $this->assertSame($message, $response->getMessage());
156
    }
157
158
    public function checkAnswerErrorResponseData()
159
    {
160
        return [
161
            ['', new GuzzleResponse(400), 'Bad Request'],
162
            ['', new GuzzleResponse(200, [], "false\nfailure-message"), 'failure-message'],
163
            ['hashKey', new GuzzleResponse(200, [], "true\nfailure-message\nnot-the-right-hash"), 'hash-fail'],
164
            ['hashKey', new GuzzleResponse(200, [], "false\nfailure-message\nnot-the-right-hash"), 'hash-fail'],
165
            ['hashKey', new GuzzleResponse(200, [], "false\nfailure-message\n" . sha1('falsefoohashKey')), 'failure-message'],
166
        ];
167
    }
168
169
    /**
170
     * @test
171
     * @uses \TraderInteractive\SolveMedia\Service::__construct
172
     * @uses \TraderInteractive\SolveMedia\Response::__construct
173
     * @uses \TraderInteractive\SolveMedia\Response::valid
174
     * @dataProvider checkAnswerValidResponseData
175
     * @covers ::checkAnswer
176
     */
177 View Code Duplication
    public function checkAnswerValidResponse($hashKey, GuzzleResponse $guzzleResponse)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
178
    {
179
        $guzzleRequest = $this->getMockForAbstractClass('\Guzzle\Http\Message\RequestInterface');
180
        $guzzleRequest->expects($this->once())->method('send')->will($this->returnValue($guzzleResponse));
181
182
        $guzzleClient = $this->getMockForAbstractClass('\Guzzle\Http\ClientInterface');
183
        $guzzleClient->expects($this->once())->method('post')->will($this->returnValue($guzzleRequest));
184
185
        $service = new Service($guzzleClient, 'notest', 'notest', $hashKey);
186
        $response = $service->checkAnswer('notest', 'foo', 'bar');
187
        $this->assertTrue($response->valid());
188
    }
189
190
    public function checkAnswerValidResponseData()
191
    {
192
        return [
193
            ['', new GuzzleResponse(200, [], 'true')],
194
            ['hashKey', new GuzzleResponse(200, [], "true\n\n" . sha1('truefoohashKey'))],
195
        ];
196
    }
197
198
    /**
199
     * @test
200
     * @uses \TraderInteractive\SolveMedia\Service::__construct
201
     * @covers ::getSignupUrl
202
     */
203
    public function getSignupUrl()
204
    {
205
        $service = new Service($this->_realGuzzleClient, 'notest', 'notest');
206
        $this->assertNotEmpty($service->getSignupUrl());
207
    }
208
}
209