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

ResponseFactoryTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 9
Bugs 4 Features 1
Metric Value
wmc 6
c 9
b 4
f 1
lcom 1
cbo 9
dl 0
loc 82
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 22 1
A testCollectionModel() 0 20 1
A testError403() 0 15 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('json')
52
                 ->andReturn(json_decode(TestFixtures::getFixture('accounts.getAccountInfo')));
53
54
        $gigyaResponse = $this->factory->getResponse($response);
55
56
        static::assertInstanceOf('Graze\Gigya\Response\Response', $gigyaResponse);
57
        static::assertEquals(200, $gigyaResponse->getStatusCode());
58
        static::assertEquals(0, $gigyaResponse->getErrorCode());
59
        static::assertEquals('OK', $gigyaResponse->getStatusReason());
60
        static::assertEquals('e6f891ac17f24810bee6eb533524a152', $gigyaResponse->getCallId());
61
        static::assertInstanceOf('DateTimeInterface', $gigyaResponse->getTime());
62
        static::assertEquals(
63
            DateTimeImmutable::createFromFormat(Gigya::DATE_TIME_FORMAT, '2015-03-22T11:42:25.943Z'),
64
            $gigyaResponse->getTime()
65
        );
66
        $data = $gigyaResponse->getData();
67
        static::assertEquals('_gid_30A3XVJciH95WEEnoRmfZS7ee3MY+lUAtpVxvUWNseU=', $data->get('UID'));
68
        static::assertSame($response, $gigyaResponse->getOriginalResponse());
69
    }
70
71
    public function testCollectionModel()
72
    {
73
        $response = m::mock('GuzzleHttp\Message\ResponseInterface');
74
        $response->shouldReceive('json')
75
                 ->andReturn(json_decode(TestFixtures::getFixture('accounts.search_simple')));
76
77
        /** @var ResponseCollectionInterface $gigyaResponse */
78
        $gigyaResponse = $this->factory->getResponse($response);
79
80
        static::assertInstanceOf('Graze\Gigya\Response\ResponseCollection', $gigyaResponse);
81
        static::assertEquals(200, $gigyaResponse->getStatusCode());
82
        static::assertEquals(1840, $gigyaResponse->getTotal());
83
        static::assertEquals(5, $gigyaResponse->getCount());
84
        static::assertNull($gigyaResponse->getNextCursor());
85
86
        $results = $gigyaResponse->getData();
87
88
        static::assertEquals(5, $results->count());
89
        static::assertEquals('[email protected]', $results[0]->profile->email);
90
    }
91
92
    public function testError403()
93
    {
94
        $response = m::mock('GuzzleHttp\Message\ResponseInterface');
95
        $response->shouldReceive('json')
96
                 ->andReturn(json_decode(TestFixtures::getFixture('failure_403')));
97
98
        $gigyaResponse = $this->factory->getResponse($response);
99
100
        static::assertInstanceOf('Graze\Gigya\Response\Response', $gigyaResponse);
101
        static::assertEquals(403, $gigyaResponse->getStatusCode());
102
        static::assertEquals(403005, $gigyaResponse->getErrorCode());
103
        static::assertEquals('Forbidden', $gigyaResponse->getStatusReason());
104
        static::assertEquals('Unauthorized user', $gigyaResponse->getErrorMessage());
105
        static::assertEquals('The user billyBob cannot login', $gigyaResponse->getErrorDetails());
106
    }
107
}
108