Completed
Pull Request — master (#10)
by Chad
04:36 queued 03:11
created

ServiceTest::getSignupUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
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();
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...
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)
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...
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);
0 ignored issues
show
Documentation introduced by
$guzzleClient is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Guzzle\Http\ClientInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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)
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...
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);
0 ignored issues
show
Documentation introduced by
$guzzleClient is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Guzzle\Http\ClientInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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