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() |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
$this->client->name(['John', 'Jane']); |
|
|
|
|
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() |
|
|
|
|
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() |
|
|
|
|
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() |
|
|
|
|
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); |
|
|
|
|
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
|
|
|
|
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.