Completed
Push — master ( 999735...b5caaa )
by Peter
06:49
created

GenderizeClientTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 151
Duplicated Lines 21.19 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 32
loc 151
rs 10
c 1
b 1
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 18 1
A name_works_correctly_with_string() 0 8 1
A name_works_correctly_with_arrays() 9 9 1
A names_works_correctly_with_arrays() 9 9 1
A country_works_correctly() 7 7 1
A lang_works_correctly() 7 7 1
A get_works_correctly() 0 7 1
A name_is_reset_after_each_usage() 0 15 1
A tearDown() 0 4 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
3
namespace Pixelpeter\Genderize;
4
5
use Mockery;
6
use Unirest\Request;
7
use Unirest\Response;
8
9
class GenderizeClientTest extends TestCase
10
{
11
    /**
12
     * @var \Pixelpeter\Genderize\GenderizeClient
13
     */
14
    protected $client;
15
16
    /**
17
     * @var \Unirest\Request
18
     */
19
    protected $request;
20
21
    /**
22
     * @var \Unirest\Response
23
     */
24
    protected $response;
25
26
    /**
27
     * Set up
28
     */
29
    public function setUp()
30
    {
31
        parent::setUp();
32
33
        $this->response = new Response(
34
            200,
35
            '{"name":"B\u00e4rbel","gender":"female","probability":"0.75","count":4,"country_id":"DE"}',
36
            "content-type: text/html; charset=UTF-8\r\n" .
37
            "X-Frame-Options: SAMEORIGIN\r\n" .
38
            "X-Powered-By: PHP/5.5.9-1ubuntu4.6\r\n" .
39
            "X-Rate-Limit-Limit: 1000\r\n" .
40
            "X-Rate-Limit-Remaining: 970\r\n" .
41
            "X-Rate-Reset: 79614\r\n"
42
        );
43
44
        $this->request = Mockery::mock('\Unirest\Request');
45
        $this->client = new GenderizeClient($this->request);
46
    }
47
48
    /**
49
     * Name sets names correctly when given as a string
50
     *
51
     * @test
52
     */
53
    public function name_works_correctly_with_string()
54
    {
55
        $this->client->name('John');
56
        $names = $this->getPrivateProperty(\Pixelpeter\Genderize\GenderizeClient::class, 'names');
57
58
        $this->assertTrue(is_array($names->getValue($this->client)));
59
        $this->assertSame('John', $names->getValue($this->client)[0]);
60
    }
61
62
    /**
63
     * Name sets names correctly when given as an array
64
     *
65
     * @test
66
     */
67 View Code Duplication
    public function name_works_correctly_with_arrays()
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...
68
    {
69
        $this->client->name(['John', 'Jane']);
0 ignored issues
show
Documentation introduced by
array('John', 'Jane') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string.

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...
70
        $names = $this->getPrivateProperty(\Pixelpeter\Genderize\GenderizeClient::class, 'names');
71
72
        $this->assertTrue(is_array($names->getValue($this->client)));
73
        $this->assertSame('John', $names->getValue($this->client)[0]);
74
        $this->assertSame('Jane', $names->getValue($this->client)[1]);
75
    }
76
77
    /**
78
     * Names sets names correctly when given as an array
79
     *
80
     * @test
81
     */
82 View Code Duplication
    public function names_works_correctly_with_arrays()
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...
83
    {
84
        $this->client->names(['John', 'Jane']);
85
        $names = $this->getPrivateProperty(\Pixelpeter\Genderize\GenderizeClient::class, 'names');
86
87
        $this->assertTrue(is_array($names->getValue($this->client)));
88
        $this->assertSame('John', $names->getValue($this->client)[0]);
89
        $this->assertSame('Jane', $names->getValue($this->client)[1]);
90
    }
91
92
    /**
93
     * Country sets country correctly
94
     *
95
     * @test
96
     */
97 View Code Duplication
    public function country_works_correctly()
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...
98
    {
99
        $this->client->name('John')->country('US')->lang('EN');
100
        $country = $this->getPrivateProperty(\Pixelpeter\Genderize\GenderizeClient::class, 'country');
101
102
        $this->assertSame('US', $country->getValue($this->client));
103
    }
104
105
    /**
106
     * Lang set lang correctly
107
     *
108
     * @test
109
     */
110 View Code Duplication
    public function lang_works_correctly()
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...
111
    {
112
        $this->client->name('John')->country('US')->lang('EN');
113
        $lang = $this->getPrivateProperty(\Pixelpeter\Genderize\GenderizeClient::class, 'lang');
114
115
        $this->assertSame('EN', $lang->getValue($this->client));
116
    }
117
118
    /**
119
     * Getting a response works correctly
120
     *
121
     * @test
122
     */
123
    public function get_works_correctly()
124
    {
125
        $this->request->shouldReceive('get')->once()->andReturn($this->response);
0 ignored issues
show
Bug introduced by
The method shouldReceive() does not seem to exist on object<Unirest\Request>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
126
        $response = $this->client->name('John')->get();
127
128
        $this->assertInstanceOf('\Pixelpeter\Genderize\Models\GenderizeResponse', $response);
129
    }
130
131
    /**
132
     * Name variable is reset after each call to get()
133
     *
134
     * @test
135
     */
136
    public function name_is_reset_after_each_usage()
137
    {
138
        $this->client->name('John');
139
        $names = $this->getPrivateProperty(\Pixelpeter\Genderize\GenderizeClient::class, 'names');
140
141
        $this->assertTrue(is_array($names->getValue($this->client)));
142
        $this->assertSame('John', $names->getValue($this->client)[0]);
143
144
        $this->client->name('Jane');
145
        $names = $this->getPrivateProperty(\Pixelpeter\Genderize\GenderizeClient::class, 'names');
146
147
        $this->assertTrue(is_array($names->getValue($this->client)));
148
        $this->assertCount(1,$names->getValue($this->client));
149
        $this->assertSame('Jane', $names->getValue($this->client)[0]);
150
    }
151
152
    /**
153
     * Tear down
154
     */
155
    public function tearDown()
156
    {
157
        Mockery::close();
158
    }
159
}
160