Completed
Pull Request — master (#18)
by Harry
03:17
created

ResponseFactoryTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 8
Bugs 3 Features 1
Metric Value
wmc 6
c 8
b 3
f 1
lcom 1
cbo 9
dl 0
loc 79
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUpBeforeClass() 0 4 1
A setUp() 0 4 1
A tearDown() 0 4 1
A testAccountModel() 0 21 1
A testCollectionModel() 0 19 1
A testError403() 0 14 1
1
<?php
2
/**
3
 * This file is part of graze/gigya-client
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/gigya-client/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/gigya-client
12
 */
13
14
namespace Graze\Gigya\Test\Unit\Response;
15
16
use DateTimeImmutable;
17
use Graze\Gigya\Gigya;
18
use Graze\Gigya\Response\ResponseCollectionInterface;
19
use Graze\Gigya\Response\ResponseFactory;
20
use Graze\Gigya\Test\TestCase;
21
use Graze\Gigya\Test\TestFixtures;
22
use Mockery as m;
23
24
// use Psr\Http\Message\ResponseInterface; Guzzle v6
25
26
class ResponseFactoryTest extends TestCase
27
{
28
    /**
29
     * @var ResponseFactory
30
     */
31
    private $factory;
32
33
    public static function setUpBeforeClass()
34
    {
35
        date_default_timezone_set('UTC');
36
    }
37
38
    public function setUp()
39
    {
40
        $this->factory = new ResponseFactory();
41
    }
42
43
    public function tearDown()
44
    {
45
        $this->factory = null;
46
    }
47
48
    public function testAccountModel()
49
    {
50
        $response = m::mock('GuzzleHttp\Message\ResponseInterface');
51
        $response->shouldReceive('getBody')->andReturn(TestFixtures::getFixture('accounts.getAccountInfo'));
52
53
        $gigyaResponse = $this->factory->getResponse($response);
54
55
        static::assertInstanceOf('Graze\Gigya\Response\Response', $gigyaResponse);
56
        static::assertEquals(200, $gigyaResponse->getStatusCode());
57
        static::assertEquals(0, $gigyaResponse->getErrorCode());
58
        static::assertEquals('OK', $gigyaResponse->getStatusReason());
59
        static::assertEquals('e6f891ac17f24810bee6eb533524a152', $gigyaResponse->getCallId());
60
        static::assertInstanceOf('DateTimeInterface', $gigyaResponse->getTime());
61
        static::assertEquals(
62
            DateTimeImmutable::createFromFormat(Gigya::DATE_TIME_FORMAT, '2015-03-22T11:42:25.943Z'),
63
            $gigyaResponse->getTime()
64
        );
65
        $data = $gigyaResponse->getData();
66
        static::assertEquals('_gid_30A3XVJciH95WEEnoRmfZS7ee3MY+lUAtpVxvUWNseU=', $data->get('UID'));
67
        static::assertSame($response, $gigyaResponse->getOriginalResponse());
68
    }
69
70
    public function testCollectionModel()
71
    {
72
        $response = m::mock('GuzzleHttp\Message\ResponseInterface');
73
        $response->shouldReceive('getBody')->andReturn(TestFixtures::getFixture('accounts.search_simple'));
74
75
        /** @var ResponseCollectionInterface $gigyaResponse */
76
        $gigyaResponse = $this->factory->getResponse($response);
77
78
        static::assertInstanceOf('Graze\Gigya\Response\ResponseCollection', $gigyaResponse);
79
        static::assertEquals(200, $gigyaResponse->getStatusCode());
80
        static::assertEquals(1840, $gigyaResponse->getTotal());
81
        static::assertEquals(5, $gigyaResponse->getCount());
82
        static::assertNull($gigyaResponse->getNextCursor());
83
84
        $results = $gigyaResponse->getData();
85
86
        static::assertEquals(5, $results->count());
87
        static::assertEquals('[email protected]', $results[0]->profile->email);
88
    }
89
90
    public function testError403()
91
    {
92
        $response = m::mock('GuzzleHttp\Message\ResponseInterface');
93
        $response->shouldReceive('getBody')->andReturn(TestFixtures::getFixture('failure_403'));
94
95
        $gigyaResponse = $this->factory->getResponse($response);
96
97
        static::assertInstanceOf('Graze\Gigya\Response\Response', $gigyaResponse);
98
        static::assertEquals(403, $gigyaResponse->getStatusCode());
99
        static::assertEquals(403005, $gigyaResponse->getErrorCode());
100
        static::assertEquals('Forbidden', $gigyaResponse->getStatusReason());
101
        static::assertEquals('Unauthorized user', $gigyaResponse->getErrorMessage());
102
        static::assertEquals('The user billyBob cannot login', $gigyaResponse->getErrorDetails());
103
    }
104
}
105