Completed
Push — master ( 5f86bb...797bc6 )
by Beñat
04:08
created

ReadTest::testRead()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Stack Exchange Api Client library.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Test\BenatEspina\StackExchangeApiClient\AccessToken;
15
16
use BenatEspina\StackExchangeApiClient\Model\AccessToken;
17
use BenatEspina\StackExchangeApiClient\Serializer\ToModelSerializer;
18
use BenatEspina\StackExchangeApiClient\StackExchange;
19
20
/**
21
 * @author Beñat Espiña <[email protected]>
22
 */
23
class ReadTest extends \PHPUnit_Framework_TestCase
24
{
25
    private $stackExchange;
26
    private $authStackExchange;
27
28
    public function setUp()
29
    {
30
        $this->stackExchange = StackExchange::withoutAuth();
31
        $this->authStackExchange = StackExchange::withAuth(KEY, ACCESS_TOKEN);
32
    }
33
34
    public function testRead() : void
35
    {
36
        $response = $this->stackExchange->accessToken()->read(ACCESS_TOKEN);
37
38
        $this->assertEquals(self::RESPONSE, $response);
39
    }
40
41
    public function testReadReturningAccessTokenObject() : void
42
    {
43
        $accessToken = $this->stackExchange->accessToken(
44
            new ToModelSerializer(AccessToken::class)
45
        )->read(ACCESS_TOKEN);
46
47
        $this->assertInstanceOf(AccessToken::class, $accessToken);
48
        $this->assertEquals(self::RESPONSE, [$accessToken->jsonSerialize()]);
49
        $this->assertEquals(2737661, $accessToken->getAccountId());
50
        $this->assertEquals(ACCESS_TOKEN, $accessToken->getAccessToken());
51
        $this->assertEquals(['no_expiry', 'write_access', 'private_info'], $accessToken->getScope());
52
    }
53
54
    public function testReadWithAuth() : void
55
    {
56
        $response = $this->authStackExchange->accessToken()->read(ACCESS_TOKEN);
57
58
        $this->assertEquals(self::RESPONSE, $response);
59
    }
60
61
    public function testReadWithAuthReturningAccessTokenObject() : void
62
    {
63
        $accessToken = $this->authStackExchange->accessToken(
64
            new ToModelSerializer(AccessToken::class)
65
        )->read(ACCESS_TOKEN);
66
67
        $this->assertInstanceOf(AccessToken::class, $accessToken);
68
        $this->assertEquals(self::RESPONSE, [$accessToken->jsonSerialize()]);
69
        $this->assertEquals(2737661, $accessToken->getAccountId());
70
        $this->assertEquals(ACCESS_TOKEN, $accessToken->getAccessToken());
71
        $this->assertEquals(['no_expiry', 'write_access', 'private_info'], $accessToken->getScope());
72
    }
73
74
    private const RESPONSE = [
75
        [
76
            'account_id'   => 2737661,
77
            'access_token' => ACCESS_TOKEN,
78
            'scope'        => [
79
                'no_expiry',
80
                'write_access',
81
                'private_info',
82
            ],
83
        ],
84
    ];
85
}
86