Completed
Pull Request — master (#10)
by Chad
05:48
created

ServiceTest::checkAnswerEmptyArgumentsData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
namespace TraderInteractive\SolveMedia;
3
4
use GuzzleHttp\ClientInterface;
5
use GuzzleHttp\Psr7\Response;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, TraderInteractive\SolveMedia\Response.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are 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.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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
            [null, null, null],
31
            ['', null, null],
32
            [0, null, null],
33
            [false, null, null],
34
            ['test', null, null],
35
            ['test', '', null],
36
            ['test', 0, null],
37
            ['test', false, null],
38
        ];
39
    }
40
41
    /**
42
     * @test
43
     * @covers ::getHtml
44
     */
45
    public function getHtmlDefault()
46
    {
47
        $pubkey = 'MyTestPubKeyStringToTestFor';
48
        $service = new Service($this->getGuzzleClient(), $pubkey, 'notest');
49
50
        $html = $service->getHtml();
51
        $this->assertRegExp("/k={$pubkey}/", $html);
52
        $this->assertNotRegExp('/;error=1/', $html);
53
        $this->assertRegExp('/' . preg_quote(Service::ADCOPY_API_SERVER, '/') . '/', $html);
54
    }
55
56
    /**
57
     * @test
58
     * @covers ::getHtml
59
     */
60
    public function getHtmlWithArguments()
61
    {
62
        $service = new Service($this->getGuzzleClient(), 'notest', 'notest');
63
        $html = $service->getHtml('test', true);
64
        $this->assertRegExp('/;error=1/', $html);
65
        $this->assertRegExp('/' . preg_quote(Service::ADCOPY_API_SECURE_SERVER, '/') . '/', $html);
66
    }
67
68
    /**
69
     * @test
70
     * @dataProvider checkAnswerNoRemoteIpData
71
     * @expectedException Exception
72
     * @covers ::checkAnswer
73
     */
74
    public function checkAnswerNoRemoteIp($remoteIp)
75
    {
76
        $service = new Service($this->getGuzzleClient(), 'notest', 'notest');
77
        $service->checkAnswer($remoteIp, null, null);
78
    }
79
80
    public function checkAnswerNoRemoteIpData()
81
    {
82
        return [
83
            [null],
84
            [''],
85
        ];
86
    }
87
88
    /**
89
     * @test
90
     * @dataProvider checkAnswerEmptyArgumentsData
91
     * @covers ::checkAnswer
92
     */
93 View Code Duplication
    public function checkAnswerEmptyArguments($challenge, $response)
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...
94
    {
95
        $service = new Service($this->getGuzzleClient(), 'notest', 'notest');
96
        $response = $service->checkAnswer('notest', $challenge, $response);
97
98
        $this->assertInstanceOf('\TraderInteractive\SolveMedia\Response', $response);
99
        $this->assertFalse($response->valid());
100
        $this->assertSame('incorrect-solution', $response->getMessage());
101
    }
102
103
    public function checkAnswerEmptyArgumentsData()
104
    {
105
        return [
106
            [null, null],
107
            ['', null],
108
            [0, null],
109
            [false, null],
110
            ['test', null],
111
            ['test', ''],
112
            ['test', 0],
113
            ['test', false],
114
        ];
115
    }
116
117
    /**
118
     * @test
119
     * @dataProvider checkAnswerErrorResponseData
120
     * @covers ::checkAnswer
121
     */
122 View Code Duplication
    public function checkAnswerErrorResponse($hashKey, Response $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...
123
    {
124
        $service = new Service($this->getGuzzleClient($guzzleResponse), 'notest', 'notest', $hashKey);
125
        $response = $service->checkAnswer('notest', 'foo', 'bar');
126
        $this->assertFalse($response->valid());
127
        $this->assertSame($message, $response->getMessage());
128
    }
129
130
    public function checkAnswerErrorResponseData()
131
    {
132
        return [
133
            ['', new Response(400), 'Bad Request'],
134
            ['', new Response(200, [], "false\nfailure-message"), 'failure-message'],
135
            ['hashKey', new Response(200, [], "true\nfailure-message\nnot-the-right-hash"), 'hash-fail'],
136
            ['hashKey', new Response(200, [], "false\nfailure-message\nnot-the-right-hash"), 'hash-fail'],
137
            ['hashKey', new Response(200, [], "false\nfailure-message\n" . sha1('falsefoohashKey')), 'failure-message'],
138
        ];
139
    }
140
141
    /**
142
     * @test
143
     * @dataProvider checkAnswerValidResponseData
144
     * @covers ::checkAnswer
145
     */
146
    public function checkAnswerValidResponse($hashKey, Response $guzzleResponse)
147
    {
148
        $service = new Service($this->getGuzzleClient($guzzleResponse), 'notest', 'notest', $hashKey);
149
        $response = $service->checkAnswer('notest', 'foo', 'bar');
150
        $this->assertTrue($response->valid());
151
    }
152
153
    public function checkAnswerValidResponseData()
154
    {
155
        return [
156
            ['', new Response(200, [], 'true')],
157
            ['hashKey', new Response(200, [], "true\n\n" . sha1('truefoohashKey'))],
158
        ];
159
    }
160
161
    /**
162
     * @test
163
     * @covers ::getSignupUrl
164
     */
165
    public function getSignupUrl()
166
    {
167
        $service = new Service($this->getGuzzleClient(), 'notest', 'notest');
168
        $this->assertNotEmpty($service->getSignupUrl());
169
    }
170
171
    private function getGuzzleClient(Response $response = null) : ClientInterface
172
    {
173
        $mock = $this->getMockBuilder(ClientInterface::class)->getMock();
174
        $mock->method('request')->willReturn($response);
175
        return $mock;
176
    }
177
}
178