|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Github\Tests\Functional; |
| 4 | + |
| 5 | +use Cache\Adapter\PHPArray\ArrayCachePool; |
| 6 | +use Github\Client; |
| 7 | +use GuzzleHttp\Psr7\Response; |
| 8 | +use function GuzzleHttp\Psr7\stream_for; |
| 9 | + |
| 10 | +/** |
| 11 | + * @group functional |
| 12 | + * |
| 13 | + * @author Tobias Nyholm <[email protected]> |
| 14 | + */ |
| 15 | +class CacheTest extends \PHPUnit_Framework_TestCase |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @test |
| 19 | + */ |
| 20 | + public function shouldServeCachedResponse() |
| 21 | + { |
| 22 | + $mockClient = new \Http\Mock\Client(); |
| 23 | + $mockClient->addResponse($this->getCurrentUserResponse('nyholm')); |
| 24 | + $mockClient->addResponse($this->getCurrentUserResponse('octocat')); |
| 25 | + |
| 26 | + $github = Client::createWithHttpClient($mockClient); |
| 27 | + $github->addCache(new ArrayCachePool(), ['default_ttl'=>60]); |
| 28 | + |
| 29 | + $github->authenticate('fake_token_aaa', Client::AUTH_HTTP_TOKEN); |
| 30 | + $userA = $github->currentUser()->show(); |
| 31 | + $this->assertEquals('nyholm', $userA['login']); |
| 32 | + |
| 33 | + $userB = $github->currentUser()->show(); |
| 34 | + $this->assertEquals('nyholm', $userB['login']); |
| 35 | + } |
| 36 | + /** |
| 37 | + * @test |
| 38 | + */ |
| 39 | + public function shouldVaryOnAuthorization() |
| 40 | + { |
| 41 | + $mockClient = new \Http\Mock\Client(); |
| 42 | + $mockClient->addResponse($this->getCurrentUserResponse('nyholm')); |
| 43 | + $mockClient->addResponse($this->getCurrentUserResponse('octocat')); |
| 44 | + |
| 45 | + $github = Client::createWithHttpClient($mockClient); |
| 46 | + $github->addCache(new ArrayCachePool(), ['default_ttl'=>60]); |
| 47 | + |
| 48 | + $github->authenticate('fake_token_aaa', Client::AUTH_HTTP_TOKEN); |
| 49 | + $userA = $github->currentUser()->show(); |
| 50 | + $this->assertEquals('nyholm', $userA['login']); |
| 51 | + |
| 52 | + $github->authenticate('fake_token_bbb', Client::AUTH_HTTP_TOKEN); |
| 53 | + $userB = $github->currentUser()->show(); |
| 54 | + $this->assertEquals('octocat', $userB['login']); |
| 55 | + } |
| 56 | + |
| 57 | + private function getCurrentUserResponse($username) |
| 58 | + { |
| 59 | + $headers = [ |
| 60 | + 'Content-Type' => 'application/json', |
| 61 | + ]; |
| 62 | + |
| 63 | + $body = stream_for(json_encode([ |
| 64 | + 'login' => $username, |
| 65 | + 'id' => 1, |
| 66 | + 'avatar_url' => 'https://github.com/images/error/'.$username.'_happy.gif', |
| 67 | + 'gravatar_id' => '', |
| 68 | + 'url' => 'https://api.github.com/users/'.$username, |
| 69 | + 'html_url' => 'https://github.com/'.$username, |
| 70 | + 'followers_url' => 'https://api.github.com/users/'.$username.'/followers', |
| 71 | + 'following_url' => 'https://api.github.com/users/'.$username.'/following{/other_user}', |
| 72 | + 'gists_url' => 'https://api.github.com/users/'.$username.'/gists{/gist_id}', |
| 73 | + 'starred_url' => 'https://api.github.com/users/'.$username.'/starred{/owner}{/repo}', |
| 74 | + 'subscriptions_url' => 'https://api.github.com/users/'.$username.'/subscriptions', |
| 75 | + 'organizations_url' => 'https://api.github.com/users/'.$username.'/orgs', |
| 76 | + 'repos_url' => 'https://api.github.com/users/'.$username.'/repos', |
| 77 | + 'events_url' => 'https://api.github.com/users/'.$username.'/events{/privacy}', |
| 78 | + 'received_events_url' => 'https://api.github.com/users/'.$username.'/received_events', |
| 79 | + 'type' => 'User', |
| 80 | + 'site_admin' => false, |
| 81 | + 'name' => 'monalisa '.$username, |
| 82 | + 'company' => 'GitHub', |
| 83 | + 'blog' => 'https://github.com/blog', |
| 84 | + 'location' => 'San Francisco', |
| 85 | + 'email' => $username.'@github.com', |
| 86 | + 'hireable' => false, |
| 87 | + 'bio' => 'There once was...', |
| 88 | + 'public_repos' => 2, |
| 89 | + 'public_gists' => 1, |
| 90 | + 'followers' => 20, |
| 91 | + 'following' => 0, |
| 92 | + 'created_at' => '2008-01-14T04:33:35Z', |
| 93 | + 'updated_at' => '2008-01-14T04:33:35Z', |
| 94 | + 'total_private_repos' => 100, |
| 95 | + 'owned_private_repos' => 100, |
| 96 | + 'private_gists' => 81, |
| 97 | + 'disk_usage' => 10000, |
| 98 | + 'collaborators' => 8, |
| 99 | + 'two_factor_authentication' => true, |
| 100 | + 'plan' => [ |
| 101 | + 'name' => 'Medium', |
| 102 | + 'space' => 400, |
| 103 | + 'private_repos' => 20, |
| 104 | + 'collaborators' => 0, |
| 105 | + ], |
| 106 | + ])); |
| 107 | + |
| 108 | + return new Response(200, $headers, $body); |
| 109 | + } |
| 110 | +} |
0 commit comments