|
| 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 | + |
| 9 | +/** |
| 10 | + * @group functional |
| 11 | + * |
| 12 | + * @author Tobias Nyholm <[email protected]> |
| 13 | + */ |
| 14 | +class CacheTest extends \PHPUnit_Framework_TestCase |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @test |
| 18 | + */ |
| 19 | + public function shouldServeCachedResponse() |
| 20 | + { |
| 21 | + $mockClient = new \Http\Mock\Client(); |
| 22 | + $mockClient->addResponse($this->getCurrentUserResponse('nyholm')); |
| 23 | + $mockClient->addResponse($this->getCurrentUserResponse('octocat')); |
| 24 | + |
| 25 | + $github = Client::createWithHttpClient($mockClient); |
| 26 | + $github->addCache(new ArrayCachePool(), ['default_ttl'=>600]); |
| 27 | + |
| 28 | + $github->authenticate('fake_token_aaa', Client::AUTH_HTTP_TOKEN); |
| 29 | + $userA = $github->currentUser()->show(); |
| 30 | + $this->assertEquals('nyholm', $userA['login']); |
| 31 | + |
| 32 | + $userB = $github->currentUser()->show(); |
| 33 | + $this->assertEquals('nyholm', $userB['login'], 'Two request following each other should be cached.'); |
| 34 | + } |
| 35 | + /** |
| 36 | + * @test |
| 37 | + */ |
| 38 | + public function shouldVaryOnAuthorization() |
| 39 | + { |
| 40 | + $mockClient = new \Http\Mock\Client(); |
| 41 | + $mockClient->addResponse($this->getCurrentUserResponse('nyholm')); |
| 42 | + $mockClient->addResponse($this->getCurrentUserResponse('octocat')); |
| 43 | + |
| 44 | + $github = Client::createWithHttpClient($mockClient); |
| 45 | + $github->addCache(new ArrayCachePool(), ['default_ttl'=>600]); |
| 46 | + |
| 47 | + $github->authenticate('fake_token_aaa', Client::AUTH_HTTP_TOKEN); |
| 48 | + $userA = $github->currentUser()->show(); |
| 49 | + $this->assertEquals('nyholm', $userA['login']); |
| 50 | + |
| 51 | + $github->authenticate('fake_token_bbb', Client::AUTH_HTTP_TOKEN); |
| 52 | + $userB = $github->currentUser()->show(); |
| 53 | + $this->assertEquals('octocat', $userB['login'], 'We must vary on the Authorization header.'); |
| 54 | + } |
| 55 | + |
| 56 | + private function getCurrentUserResponse($username) |
| 57 | + { |
| 58 | + $headers = [ |
| 59 | + 'Content-Type' => 'application/json', |
| 60 | + ]; |
| 61 | + |
| 62 | + $body = \GuzzleHttp\Psr7\stream_for(json_encode([ |
| 63 | + 'login' => $username, |
| 64 | + ])); |
| 65 | + |
| 66 | + return new Response(200, $headers, $body); |
| 67 | + } |
| 68 | +} |
0 commit comments