GitHubTest::getEmailTestMock()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
rs 9.4285
cc 1
eloc 9
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Oryzone PHPoAuthUserData package <https://github.com/Oryzone/PHPoAuthUserData>.
5
 *
6
 * (c) Oryzone, developed by Luciano Mammino <[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
namespace OAuth\Unit\UserData\Extractor;
13
14
use OAuth\UserData\Extractor\GitHub;
15
16
/**
17
 * Generated by PHPUnit_SkeletonGenerator 1.2.1 on 2014-02-08 at 00:52:53.
18
 */
19
class GitHubTest extends \PHPUnit_Framework_TestCase
20
{
21
    const PROFILE_RESPONSE =
22
        '{
23
          "login": "octocat",
24
          "id": 123,
25
          "avatar_url": "https://github.com/images/error/octocat_happy.gif",
26
          "gravatar_id": "somehexcode",
27
          "url": "https://api.github.com/users/octocat",
28
          "html_url": "https://github.com/octocat",
29
          "followers_url": "https://api.github.com/users/octocat/followers",
30
          "following_url": "https://api.github.com/users/octocat/following{/other_user}",
31
          "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
32
          "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
33
          "subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
34
          "organizations_url": "https://api.github.com/users/octocat/orgs",
35
          "repos_url": "https://api.github.com/users/octocat/repos",
36
          "events_url": "https://api.github.com/users/octocat/events{/privacy}",
37
          "received_events_url": "https://api.github.com/users/octocat/received_events",
38
          "type": "User",
39
          "site_admin": false,
40
          "name": "monalisa octocat",
41
          "company": "GitHub",
42
          "blog": "https://github.com/blog",
43
          "location": "San Francisco",
44
          "email": "[email protected]",
45
          "hireable": false,
46
          "bio": "There once was...",
47
          "public_repos": 2,
48
          "public_gists": 1,
49
          "followers": 20,
50
          "following": 0,
51
          "created_at": "2008-01-14T04:33:35Z",
52
          "updated_at": "2008-01-14T04:33:35Z",
53
          "total_private_repos": 100,
54
          "owned_private_repos": 100,
55
          "private_gists": 81,
56
          "disk_usage": 10000,
57
          "collaborators": 8,
58
          "plan": {
59
            "name": "Medium",
60
            "space": 400,
61
            "collaborators": 10,
62
            "private_repos": 20
63
          }
64
        }';
65
66
    const EMAIL_RESPONSE_PRIMARY_AND_VERIFIED =
67
        '[
68
          {
69
            "email": "[email protected]",
70
            "verified": false,
71
            "primary": false
72
          },
73
          {
74
            "email": "[email protected]",
75
            "verified": true,
76
            "primary": false
77
          },
78
          {
79
            "email": "[email protected]",
80
            "verified": true,
81
            "primary": true
82
          }
83
        ]';
84
85
    const EMAIL_RESPONSE_PRIMARY_BUT_NOT_VERIFIED =
86
        '[
87
          {
88
            "email": "[email protected]",
89
            "verified": false,
90
            "primary": false
91
          }
92
        ]';
93
94
    /**
95
     * @var GitHub
96
     */
97
    protected $extractor;
98
99
    /**
100
     * Sets up the fixture, for example, opens a network connection.
101
     * This method is called before a test is executed.
102
     */
103
    protected function setUp()
104
    {
105
        $this->extractor = new GitHub();
106
        $service = $this->getMockBuilder('\\OAuth\\OAuth2\\Service\\GitHub')
107
            ->disableOriginalConstructor()
108
            ->getMock();
109
        $service->expects($this->any())
110
            ->method('request')
111
            ->with(GitHub::REQUEST_PROFILE)
112
            ->will($this->returnValue(GitHubTest::PROFILE_RESPONSE));
113
114
        /**
115
         * @var \OAuth\Common\Service\ServiceInterface $service
116
         */
117
        $this->extractor->setService($service);
118
    }
119
120
    /**
121
     * Tears down the fixture, for example, closes a network connection.
122
     * This method is called after a test is executed.
123
     */
124
    protected function tearDown()
125
    {
126
    }
127
128
    public function testDoesNotSupportWebsites()
129
    {
130
        $this->assertFalse($this->extractor->supportsWebsites());
131
        $this->assertNull($this->extractor->getWebsites());
132
    }
133
134
    public function testGetUniqueId()
135
    {
136
        $this->assertEquals(123, $this->extractor->getUniqueId());
137
    }
138
139
    public function testGetUsername()
140
    {
141
        $this->assertEquals('octocat', $this->extractor->getUsername());
142
    }
143
144
    public function testGetFistName()
145
    {
146
        $this->assertEquals('monalisa', $this->extractor->getFirstName());
147
    }
148
149
    public function testGetLastName()
150
    {
151
        $this->assertEquals('octocat', $this->extractor->getLastName());
152
    }
153
154
    public function testGetFullName()
155
    {
156
        $this->assertEquals('monalisa octocat', $this->extractor->getFullName());
157
    }
158
159
    public function testGetEmailWithPrimaryAndVerifiedAvailable()
160
    {
161
        $this->extractor->setService($this->getEmailTestMock(GitHubTest::EMAIL_RESPONSE_PRIMARY_AND_VERIFIED));
162
163
        $this->assertEquals("[email protected]", $this->extractor->getEmail());
164
    }
165
166
    public function testGetEmailWithPrimaryButNotVerifiedAvailable()
167
    {
168
        $this->extractor->setService($this->getEmailTestMock(GitHubTest::EMAIL_RESPONSE_PRIMARY_BUT_NOT_VERIFIED));
169
170
        $this->assertEquals("[email protected]", $this->extractor->getEmail());
171
    }
172
173
    public function testGetLocation()
174
    {
175
        $this->assertEquals('San Francisco', $this->extractor->getLocation());
176
    }
177
178
    public function testGetDescription()
179
    {
180
        $this->assertEquals('There once was...', $this->extractor->getDescription());
181
    }
182
183
    public function testGetImageUrl()
184
    {
185
        $this->assertEquals('https://github.com/images/error/octocat_happy.gif', $this->extractor->getImageUrl());
186
    }
187
188
    public function testGetProfileUrl()
189
    {
190
        $this->assertEquals('https://github.com/octocat', $this->extractor->getProfileUrl());
191
    }
192
193
    public function testIsEmailVerifiedIfVerified()
194
    {
195
        $this->extractor->setService($this->getEmailTestMock(GitHubTest::EMAIL_RESPONSE_PRIMARY_AND_VERIFIED));
196
197
        $this->assertEquals(true, $this->extractor->isEmailVerified());
198
    }
199
200
    public function testIsEmailVerifiedIfNotVerified()
201
    {
202
        $this->extractor->setService($this->getEmailTestMock(GitHubTest::EMAIL_RESPONSE_PRIMARY_BUT_NOT_VERIFIED));
203
204
        $this->assertEquals(false, $this->extractor->isEmailVerified());
205
    }
206
207
    public function testGetExtra()
208
    {
209
        $extra = $this->extractor->getExtras();
210
211
        $this->assertArrayHasKey('gravatar_id', $extra);
212
        $this->assertArrayHasKey('disk_usage', $extra);
213
        $this->assertArrayHasKey('type', $extra);
214
215
        $this->assertArrayNotHasKey('id', $extra);
216
        $this->assertArrayNotHasKey('login', $extra);
217
        $this->assertArrayNotHasKey('name', $extra);
218
        $this->assertArrayNotHasKey('location', $extra);
219
        $this->assertArrayNotHasKey('bio', $extra);
220
        $this->assertArrayNotHasKey('avatar_url', $extra);
221
        $this->assertArrayNotHasKey('html_url', $extra);
222
    }
223
224
    /**
225
     * @return \OAuth\Common\Service\ServiceInterface
226
     */
227
    private function getEmailTestMock($returnValue)
228
    {
229
        $service = $this->getMockBuilder('\\OAuth\\OAuth2\\Service\\GitHub')
230
            ->disableOriginalConstructor()
231
            ->getMock();
232
        $service->expects($this->any())
233
            ->method('request')
234
            ->with(GitHub::REQUEST_EMAIL, 'GET', array(), array('Accept' => 'application/vnd.github.v3'))
235
            ->will($this->returnValue($returnValue));
236
237
        return $service;
238
    }
239
}
240