Completed
Push — master ( 875955...9fc887 )
by Bradley
01:57
created

PacktpublrTest::testRunWithConsoleArgument()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 16

Duplication

Lines 27
Ratio 100 %

Importance

Changes 0
Metric Value
dl 27
loc 27
c 0
b 0
f 0
rs 8.8571
cc 1
eloc 16
nc 1
nop 0
1
<?php namespace tests\Cornford\Packtpublr;
2
3
use Cornford\Packtpublr\Packtpublr;
4
use Mockery;
5
use PHPUnit_Framework_TestCase as TestCase;
6
7
class PacktpublrTest extends TestCase
8
{
9
10
    const EMAIL = '[email protected]';
11
    const PASSWORD = 'Password123';
12
13
    const HTTP_RESPONSE_SUCCESS = 200;
14
    const HTTP_RESPONSE_ERROR = 500;
15
16
    const BODY_STRING = '<a href="/freelearning-claim/123/456" class="twelve-days-claim">';
17
18
    public function testConstruct()
19
    {
20
        $instance = new Packtpublr(['email' => self::EMAIL, 'password' => self::PASSWORD]);
21
22
        $this->assertInstanceOf(Packtpublr::class, $instance);
23
    }
24
25
    /**
26
     * @expectedException Cornford\Packtpublr\Exceptions\PacktpublrArgumentException
27
     */
28
    public function testConstructWithNoArguments()
29
    {
30
        $instance = new Packtpublr([]);
31
    }
32
33
    /**
34
     * @expectedException Cornford\Packtpublr\Exceptions\PacktpublrArgumentException
35
     */
36
    public function testConstructWithNoEmailArgument()
37
    {
38
        $instance = new Packtpublr([]);
39
    }
40
41
    /**
42
     * @expectedException Cornford\Packtpublr\Exceptions\PacktpublrArgumentException
43
     */
44
    public function testConstructWithInvalidEmailArgument()
45
    {
46
        $instance = new Packtpublr(['email' => false, 'password' => self::PASSWORD]);
47
    }
48
49
    /**
50
     * @expectedException Cornford\Packtpublr\Exceptions\PacktpublrArgumentException
51
     */
52
    public function testConstructWithNoPasswordArgument()
53
    {
54
        $instance = new Packtpublr(['email' => self::EMAIL, 'password' => false]);
55
    }
56
57
    /**
58
     * @expectedException Cornford\Packtpublr\Exceptions\PacktpublrArgumentException
59
     */
60
    public function testConstructWithInvalidPasswordArgument()
61
    {
62
        $instance = new Packtpublr(['email' => self::EMAIL]);
63
    }
64
65 View Code Duplication
    public function testLogin()
66
    {
67
        $instance = new Packtpublr(['email' => '[email protected]', 'password' => 'Password123']);
68
69
        $client = Mockery::mock('GuzzleHttp\Client');
70
71
        $body = Mockery::mock('stdClass');
72
        $body->shouldReceive('__toString')->andReturn('');
73
74
        $response = Mockery::mock('GuzzleHttp\Message\Response');
75
        $response->shouldReceive('getStatusCode')->andReturn(self::HTTP_RESPONSE_SUCCESS);
76
        $response->shouldReceive('getHeaders')->andReturn($body);
77
        $response->shouldReceive('getBody')->andReturnSelf();
78
79
        $client->shouldReceive('post')->andReturn($response);
80
81
        $instance->setHttpClient($client);
82
83
        $this->assertTrue($instance->login());
84
    }
85
86 View Code Duplication
    public function testLoginWithOptionalArguments()
87
    {
88
        $instance = new Packtpublr(['email' => '[email protected]', 'password' => 'Password123']);
89
90
        $client = Mockery::mock('GuzzleHttp\Client');
91
92
        $body = Mockery::mock('stdClass');
93
        $body->shouldReceive('__toString')->andReturn('');
94
95
        $response = Mockery::mock('GuzzleHttp\Message\Response');
96
        $response->shouldReceive('getStatusCode')->andReturn(self::HTTP_RESPONSE_SUCCESS);
97
        $response->shouldReceive('getHeaders')->andReturn($body);
98
        $response->shouldReceive('getBody')->andReturnSelf();
99
100
        $client->shouldReceive('post')->andReturn($response);
101
102
        $instance->setHttpClient($client);
103
104
        $this->assertTrue($instance->login('[email protected]', 'Password123'));
105
    }
106
107
    public function testRedeem()
108
    {
109
        $instance = new Packtpublr(['email' => '[email protected]', 'password' => 'Password123']);
110
111
        $client = Mockery::mock('GuzzleHttp\Client');
112
113
        $body = Mockery::mock('stdClass');
114
        $body->shouldReceive('__toString')->andReturn(self::BODY_STRING);
115
116
        $response = Mockery::mock('GuzzleHttp\Message\Response');
117
        $response->shouldReceive('getStatusCode')->andReturn(self::HTTP_RESPONSE_SUCCESS);
118
        $response->shouldReceive('getHeaders')->andReturn([]);
119
        $response->shouldReceive('getBody')->andReturn($body);
120
121
        $client->shouldReceive('post')->andReturn($response);
122
        $client->shouldReceive('get')->andReturn($response);
123
124
        $instance->setHttpClient($client);
125
126
        $this->assertTrue($instance->redeem());
127
    }
128
129
    /**
130
     * @expectedException Cornford\Packtpublr\Exceptions\PacktpublrRedeemException
131
     */
132
    public function testRedeemError()
133
    {
134
        $instance = new Packtpublr(['email' => '[email protected]', 'password' => 'Password123']);
135
136
        $client = Mockery::mock('GuzzleHttp\Client');
137
138
        $body = Mockery::mock('stdClass');
139
        $body->shouldReceive('__toString')->andReturn('');
140
141
        $response = Mockery::mock('GuzzleHttp\Message\Response');
142
        $response->shouldReceive('getStatusCode')->andReturn(self::HTTP_RESPONSE_SUCCESS);
143
        $response->shouldReceive('getHeaders')->andReturn([]);
144
        $response->shouldReceive('getBody')->andReturn($body);
145
146
        $client->shouldReceive('post')->andReturn($response);
147
        $client->shouldReceive('get')->andReturn($response);
148
149
        $instance->setHttpClient($client);
150
151
        $this->assertTrue($instance->redeem());
152
    }
153
154 View Code Duplication
    public function testLogout()
155
    {
156
        $instance = new Packtpublr(['email' => '[email protected]', 'password' => 'Password123']);
157
158
        $client = Mockery::mock('GuzzleHttp\Client');
159
160
        $body = Mockery::mock('stdClass');
161
        $body->shouldReceive('__toString')->andReturn('');
162
163
        $response = Mockery::mock('GuzzleHttp\Message\Response');
164
        $response->shouldReceive('getStatusCode')->andReturn(self::HTTP_RESPONSE_SUCCESS);
165
        $response->shouldReceive('getHeaders')->andReturn([]);
166
        $response->shouldReceive('getBody')->andReturn($body);
167
168
        $client->shouldReceive('get')->andReturn($response);
169
170
        $instance->setHttpClient($client);
171
172
        $this->assertTrue($instance->logout());
173
    }
174
175 View Code Duplication
    public function testRun()
176
    {
177
        $instance = new Packtpublr(['email' => '[email protected]', 'password' => 'Password123']);
178
179
        $client = Mockery::mock('GuzzleHttp\Client');
180
181
        $body = Mockery::mock('stdClass');
182
        $body->shouldReceive('__toString')->andReturn(self::BODY_STRING);
183
184
        $response = Mockery::mock('GuzzleHttp\Message\Response');
185
        $response->shouldReceive('getStatusCode')->andReturn(self::HTTP_RESPONSE_SUCCESS);
186
        $response->shouldReceive('getHeaders')->andReturn([]);
187
        $response->shouldReceive('getBody')->andReturn($body);
188
189
        $client->shouldReceive('get')->andReturn($response);
190
191
        $client->shouldReceive('post')->andReturn($response);
192
193
        $instance->setHttpClient($client);
194
195
        $consoleOutput = Mockery::mock('Symfony\Component\Console\Output\ConsoleOutput');
196
        $consoleOutput->shouldReceive('writeln')->andReturn(null);
197
198
        $instance->setConsoleOutput($consoleOutput);
199
200
        $this->assertTrue($instance->run());
201
    }
202
203
    /**
204
     * @outputBuffering enabled
205
     */
206 View Code Duplication
    public function testRunWithConsoleArgument()
207
    {
208
        $instance = new Packtpublr(['email' => '[email protected]', 'password' => 'Password123']);
209
210
        $client = Mockery::mock('GuzzleHttp\Client');
211
212
        $body = Mockery::mock('stdClass');
213
        $body->shouldReceive('__toString')->andReturn(self::BODY_STRING);
214
215
        $response = Mockery::mock('GuzzleHttp\Message\Response');
216
        $response->shouldReceive('getStatusCode')->andReturn(self::HTTP_RESPONSE_SUCCESS);
217
        $response->shouldReceive('getHeaders')->andReturn([]);
218
        $response->shouldReceive('getBody')->andReturn($body);
219
220
        $client->shouldReceive('get')->andReturn($response);
221
222
        $client->shouldReceive('post')->andReturn($response);
223
224
        $instance->setHttpClient($client);
225
226
        $consoleOutput = Mockery::mock('Symfony\Component\Console\Output\ConsoleOutput');
227
        $consoleOutput->shouldReceive('writeln')->andReturn(null);
228
229
        $instance->setConsoleOutput($consoleOutput);
230
231
        $this->assertTrue($instance->run());
232
    }
233
234 View Code Duplication
    public function testRunWithOptionalArguments()
235
    {
236
        $instance = new Packtpublr(['email' => '[email protected]', 'password' => 'Password123']);
237
238
        $client = Mockery::mock('GuzzleHttp\Client');
239
240
        $body = Mockery::mock('stdClass');
241
        $body->shouldReceive('__toString')->andReturn(self::BODY_STRING);
242
243
        $response = Mockery::mock('GuzzleHttp\Message\Response');
244
        $response->shouldReceive('getStatusCode')->andReturn(self::HTTP_RESPONSE_SUCCESS);
245
        $response->shouldReceive('getHeaders')->andReturn([]);
246
        $response->shouldReceive('getBody')->andReturn($body);
247
248
        $client->shouldReceive('get')->andReturn($response);
249
250
        $client->shouldReceive('post')->andReturn($response);
251
252
        $instance->setHttpClient($client);
253
254
        $consoleOutput = Mockery::mock('Symfony\Component\Console\Output\ConsoleOutput');
255
        $consoleOutput->shouldReceive('writeln')->andReturn(null);
256
257
        $instance->setConsoleOutput($consoleOutput);
258
259
        $this->assertTrue($instance->run('[email protected]', 'Password123'));
260
    }
261
262
    public function testSetAndGetHttpClient()
263
    {
264
        $instance = new Packtpublr(['email' => '[email protected]', 'password' => 'Password123']);
265
266
        $client = Mockery::mock('GuzzleHttp\Client');
267
268
        $instance->setHttpClient($client);
269
270
        $this->assertEquals($instance->getHttpClient(), $client);
271
    }
272
273
    public function testSetAndGetCookieSubscriber()
274
    {
275
        $instance = new Packtpublr(['email' => '[email protected]', 'password' => 'Password123']);
276
277
        $cookie = Mockery::mock('GuzzleHttp\Subscriber\Cookie');
278
279
        $cookie->shouldReceive('getEvents')->andReturnSelf();
280
281
        $instance->setCookieSubscriber($cookie);
282
283
        $this->assertEquals($instance->getCookieSubscriber(), $cookie);
284
    }
285
286 View Code Duplication
    public function testGetResponseBody()
287
    {
288
        $instance = new Packtpublr(['email' => '[email protected]', 'password' => 'Password123']);
289
290
        $client = Mockery::mock('GuzzleHttp\Client');
291
292
        $body = Mockery::mock('stdClass');
293
        $body->shouldReceive('__toString')->andReturn(self::BODY_STRING);
294
295
        $response = Mockery::mock('GuzzleHttp\Message\Response');
296
        $response->shouldReceive('getStatusCode')->andReturn(self::HTTP_RESPONSE_SUCCESS);
297
        $response->shouldReceive('getHeaders')->andReturn([]);
298
        $response->shouldReceive('getBody')->andReturn($body);
299
300
        $client->shouldReceive('post')->andReturn($response);
301
        $client->shouldReceive('get')->andReturn($response);
302
303
        $instance->setHttpClient($client);
304
305
        $instance->redeem();
306
307
        $this->assertEquals($instance->getResponseBody(), self::BODY_STRING);
308
    }
309
310 View Code Duplication
    public function testGetResponseCode()
311
    {
312
        $instance = new Packtpublr(['email' => '[email protected]', 'password' => 'Password123']);
313
314
        $client = Mockery::mock('GuzzleHttp\Client');
315
316
        $body = Mockery::mock('stdClass');
317
        $body->shouldReceive('__toString')->andReturn(self::BODY_STRING);
318
319
        $response = Mockery::mock('GuzzleHttp\Message\Response');
320
        $response->shouldReceive('getStatusCode')->andReturn(self::HTTP_RESPONSE_SUCCESS);
321
        $response->shouldReceive('getHeaders')->andReturn([]);
322
        $response->shouldReceive('getBody')->andReturn($body);
323
324
        $client->shouldReceive('post')->andReturn($response);
325
        $client->shouldReceive('get')->andReturn($response);
326
327
        $instance->setHttpClient($client);
328
329
        $instance->redeem();
330
331
        $this->assertEquals($instance->getResponseCode(), self::HTTP_RESPONSE_SUCCESS);
332
    }
333
334 View Code Duplication
    public function testGetResponseHeaders()
335
    {
336
        $instance = new Packtpublr(['email' => '[email protected]', 'password' => 'Password123']);
337
338
        $client = Mockery::mock('GuzzleHttp\Client');
339
340
        $body = Mockery::mock('stdClass');
341
        $body->shouldReceive('__toString')->andReturn(self::BODY_STRING);
342
343
        $response = Mockery::mock('GuzzleHttp\Message\Response');
344
        $response->shouldReceive('getStatusCode')->andReturn(self::HTTP_RESPONSE_SUCCESS);
345
        $response->shouldReceive('getHeaders')->andReturn(['Content-Type: text/html']);
346
        $response->shouldReceive('getBody')->andReturn($body);
347
348
        $client->shouldReceive('post')->andReturn($response);
349
        $client->shouldReceive('get')->andReturn($response);
350
351
        $instance->setHttpClient($client);
352
353
        $instance->redeem();
354
355
        $this->assertEquals($instance->getResponseHeaders(), ['Content-Type: text/html']);
356
    }
357
    
358
}