forked from KnpLabs/php-github-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCacheTest.php
68 lines (56 loc) · 2.12 KB
/
CacheTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
namespace Github\Tests\Functional;
use Cache\Adapter\PHPArray\ArrayCachePool;
use Github\Client;
use GuzzleHttp\Psr7\Response;
/**
* @group functional
*
* @author Tobias Nyholm <[email protected]>
*/
class CacheTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
*/
public function shouldServeCachedResponse()
{
$mockClient = new \Http\Mock\Client();
$mockClient->addResponse($this->getCurrentUserResponse('nyholm'));
$mockClient->addResponse($this->getCurrentUserResponse('octocat'));
$github = Client::createWithHttpClient($mockClient);
$github->addCache(new ArrayCachePool(), ['default_ttl'=>600]);
$github->authenticate('fake_token_aaa', Client::AUTH_HTTP_TOKEN);
$userA = $github->currentUser()->show();
$this->assertEquals('nyholm', $userA['login']);
$userB = $github->currentUser()->show();
$this->assertEquals('nyholm', $userB['login'], 'Two request following each other should be cached.');
}
/**
* @test
*/
public function shouldVaryOnAuthorization()
{
$mockClient = new \Http\Mock\Client();
$mockClient->addResponse($this->getCurrentUserResponse('nyholm'));
$mockClient->addResponse($this->getCurrentUserResponse('octocat'));
$github = Client::createWithHttpClient($mockClient);
$github->addCache(new ArrayCachePool(), ['default_ttl'=>600]);
$github->authenticate('fake_token_aaa', Client::AUTH_HTTP_TOKEN);
$userA = $github->currentUser()->show();
$this->assertEquals('nyholm', $userA['login']);
$github->authenticate('fake_token_bbb', Client::AUTH_HTTP_TOKEN);
$userB = $github->currentUser()->show();
$this->assertEquals('octocat', $userB['login'], 'We must vary on the Authorization header.');
}
private function getCurrentUserResponse($username)
{
$headers = [
'Content-Type' => 'application/json',
];
$body = \GuzzleHttp\Psr7\stream_for(json_encode([
'login' => $username,
]));
return new Response(200, $headers, $body);
}
}