Completed
Push — master ( 7a6150...2f2994 )
by Chad
10s
created

ServiceTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 159
Duplicated Lines 10.06 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 5
dl 16
loc 159
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A constructWithInvalidArguments() 0 4 1
A constructWithInvalidArgumentsData() 0 7 1
A getHtmlDefault() 0 10 1
A getHtmlWithArguments() 0 7 1
A checkAnswerNoRemoteIp() 0 5 1
A checkAnswerNoRemoteIpData() 0 6 1
A checkAnswerEmptyArguments() 9 9 1
A checkAnswerEmptyArgumentsData() 0 13 1
A checkAnswerErrorResponse() 7 7 1
A checkAnswerErrorResponseData() 0 10 1
A checkAnswerValidResponse() 0 6 1
A checkAnswerValidResponseData() 0 7 1
A getSignupUrl() 0 5 1
A getGuzzleClient() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
            '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)
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...
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)
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...
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