1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pixelpeter\Genderize; |
4
|
|
|
|
5
|
|
|
use Mockery; |
6
|
|
|
use Unirest\Response; |
7
|
|
|
|
8
|
|
|
class GenderizeClientTest extends TestCase |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var \Pixelpeter\Genderize\GenderizeClient |
12
|
|
|
*/ |
13
|
|
|
protected $client; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var \Unirest\Request |
17
|
|
|
*/ |
18
|
|
|
protected $request; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var \Unirest\Response |
22
|
|
|
*/ |
23
|
|
|
protected $response; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Set up |
27
|
|
|
*/ |
28
|
|
|
protected function setUp(): void |
29
|
|
|
{ |
30
|
|
|
parent::setUp(); |
31
|
|
|
|
32
|
|
|
$this->response = new Response( |
33
|
|
|
200, |
34
|
|
|
'{"name":"B\u00e4rbel","gender":"female","probability":"0.75","count":4,"country_id":"DE"}', |
35
|
|
|
"content-type: text/html; charset=UTF-8\r\n". |
36
|
|
|
"X-Frame-Options: SAMEORIGIN\r\n". |
37
|
|
|
"X-Powered-By: PHP/5.5.9-1ubuntu4.6\r\n". |
38
|
|
|
"X-Rate-Limit-Limit: 1000\r\n". |
39
|
|
|
"X-Rate-Limit-Remaining: 970\r\n". |
40
|
|
|
"X-Rate-Reset: 79614\r\n" |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
$this->request = Mockery::mock('\Unirest\Request'); |
44
|
|
|
$this->client = new GenderizeClient($this->request); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Name sets names correctly when given as a string |
49
|
|
|
* |
50
|
|
|
* @test |
51
|
|
|
*/ |
52
|
|
|
public function name_works_correctly_with_string() |
53
|
|
|
{ |
54
|
|
|
$this->client->name('John'); |
55
|
|
|
$names = $this->getPrivateProperty(\Pixelpeter\Genderize\GenderizeClient::class, 'names'); |
56
|
|
|
|
57
|
|
|
$this->assertTrue(is_array($names->getValue($this->client))); |
58
|
|
|
$this->assertSame('John', $names->getValue($this->client)[0]); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Name sets names correctly when given as an array |
63
|
|
|
* |
64
|
|
|
* @test |
65
|
|
|
*/ |
66
|
|
|
public function name_works_correctly_with_arrays() |
67
|
|
|
{ |
68
|
|
|
$this->client->name(['John', 'Jane']); |
69
|
|
|
$names = $this->getPrivateProperty(\Pixelpeter\Genderize\GenderizeClient::class, 'names'); |
70
|
|
|
|
71
|
|
|
$this->assertTrue(is_array($names->getValue($this->client))); |
72
|
|
|
$this->assertSame('John', $names->getValue($this->client)[0]); |
73
|
|
|
$this->assertSame('Jane', $names->getValue($this->client)[1]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Names sets names correctly when given as an array |
78
|
|
|
* |
79
|
|
|
* @test |
80
|
|
|
*/ |
81
|
|
|
public function names_works_correctly_with_arrays() |
82
|
|
|
{ |
83
|
|
|
$this->client->names(['John', 'Jane']); |
84
|
|
|
$names = $this->getPrivateProperty(\Pixelpeter\Genderize\GenderizeClient::class, 'names'); |
85
|
|
|
|
86
|
|
|
$this->assertTrue(is_array($names->getValue($this->client))); |
87
|
|
|
$this->assertSame('John', $names->getValue($this->client)[0]); |
88
|
|
|
$this->assertSame('Jane', $names->getValue($this->client)[1]); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Country sets country correctly |
93
|
|
|
* |
94
|
|
|
* @test |
95
|
|
|
*/ |
96
|
|
|
public function country_works_correctly() |
97
|
|
|
{ |
98
|
|
|
$this->client->name('John')->country('US')->lang('EN'); |
99
|
|
|
$country = $this->getPrivateProperty(\Pixelpeter\Genderize\GenderizeClient::class, 'country'); |
100
|
|
|
|
101
|
|
|
$this->assertSame('US', $country->getValue($this->client)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Lang set lang correctly |
106
|
|
|
* |
107
|
|
|
* @test |
108
|
|
|
*/ |
109
|
|
|
public function lang_works_correctly() |
110
|
|
|
{ |
111
|
|
|
$this->client->name('John')->country('US')->lang('EN'); |
112
|
|
|
$lang = $this->getPrivateProperty(\Pixelpeter\Genderize\GenderizeClient::class, 'lang'); |
113
|
|
|
|
114
|
|
|
$this->assertSame('EN', $lang->getValue($this->client)); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Getting a response works correctly |
119
|
|
|
* |
120
|
|
|
* @test |
121
|
|
|
*/ |
122
|
|
|
public function get_works_correctly() |
123
|
|
|
{ |
124
|
|
|
$this->request->shouldReceive('get')->once()->andReturn($this->response); |
125
|
|
|
$response = $this->client->name('John')->get(); |
126
|
|
|
|
127
|
|
|
$this->assertInstanceOf('\Pixelpeter\Genderize\Models\GenderizeResponse', $response); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Name variable is reset after each call to get() |
132
|
|
|
* |
133
|
|
|
* @test |
134
|
|
|
*/ |
135
|
|
|
public function name_is_reset_after_each_usage() |
136
|
|
|
{ |
137
|
|
|
$this->client->name('John'); |
138
|
|
|
$names = $this->getPrivateProperty(\Pixelpeter\Genderize\GenderizeClient::class, 'names'); |
139
|
|
|
|
140
|
|
|
$this->assertTrue(is_array($names->getValue($this->client))); |
141
|
|
|
$this->assertSame('John', $names->getValue($this->client)[0]); |
142
|
|
|
|
143
|
|
|
$this->client->name('Jane'); |
144
|
|
|
$names = $this->getPrivateProperty(\Pixelpeter\Genderize\GenderizeClient::class, 'names'); |
145
|
|
|
|
146
|
|
|
$this->assertTrue(is_array($names->getValue($this->client))); |
147
|
|
|
$this->assertCount(1, $names->getValue($this->client)); |
148
|
|
|
$this->assertSame('Jane', $names->getValue($this->client)[0]); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Tear down |
153
|
|
|
*/ |
154
|
|
|
protected function tearDown(): void |
155
|
|
|
{ |
156
|
|
|
Mockery::close(); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|