name_is_reset_after_each_usage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 9
c 1
b 1
f 0
dl 0
loc 14
rs 9.9666
cc 1
nc 1
nop 0
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');
0 ignored issues
show
Documentation Bug introduced by
It seems like Mockery::mock('\Unirest\Request') of type Mockery\LegacyMockInterface or Mockery\MockInterface is incompatible with the declared type Unirest\Request of property $request.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
45
        $this->client = new GenderizeClient($this->request);
0 ignored issues
show
Bug introduced by
$this->request of type Mockery\LegacyMockInterface|Mockery\MockInterface is incompatible with the type Unirest\Request expected by parameter $request of Pixelpeter\Genderize\Gen...zeClient::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
        $this->client = new GenderizeClient(/** @scrutinizer ignore-type */ $this->request);
Loading history...
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
    public function name_works_correctly_with_arrays()
68
    {
69
        $this->client->name(['John', 'Jane']);
0 ignored issues
show
Bug introduced by
array('John', 'Jane') of type array<integer,string> is incompatible with the type string expected by parameter $name of Pixelpeter\Genderize\GenderizeClient::name(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

69
        $this->client->name(/** @scrutinizer ignore-type */ ['John', 'Jane']);
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
    public function names_works_correctly_with_arrays()
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
    public function country_works_correctly()
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
    public function lang_works_correctly()
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 exist on Unirest\Request. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

125
        $this->request->/** @scrutinizer ignore-call */ 
126
                        shouldReceive('get')->once()->andReturn($this->response);

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