|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Github\Tests\HttpClient; |
| 4 | + |
| 5 | +use Github\HttpClient\CachedHttpClient; |
| 6 | +use Github\HttpClient\Message\Response; |
| 7 | +use Github\HttpClient\Message\Request; |
| 8 | + |
| 9 | +class CachedHttpClientTest extends HttpClientTest |
| 10 | +{ |
| 11 | + /** |
| 12 | + * @test |
| 13 | + */ |
| 14 | + public function shouldCacheResponseAtFirstTime() |
| 15 | + { |
| 16 | + $cache = $this->getCacheMock(); |
| 17 | + $httpClient = new CachedHttpClient( |
| 18 | + array('base_url' => ''), |
| 19 | + $this->getMock('Buzz\Client\ClientInterface'), |
| 20 | + $cache |
| 21 | + ); |
| 22 | + |
| 23 | + $cache->expects($this->once())->method('set')->with('test', new Response); |
| 24 | + |
| 25 | + $httpClient->get('test'); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * @test |
| 30 | + */ |
| 31 | + public function shouldGetCachedResponseWhileResourceNotModified() |
| 32 | + { |
| 33 | + $client = $this->getMock('Buzz\Client\ClientInterface'); |
| 34 | + $client->expects($this->once())->method('send'); |
| 35 | + |
| 36 | + $cache = $this->getCacheMock(); |
| 37 | + |
| 38 | + $httpClient = new CachedHttpClient( |
| 39 | + array('base_url' => ''), |
| 40 | + $client, |
| 41 | + $cache |
| 42 | + ); |
| 43 | + |
| 44 | + $cache->expects($this->once())->method('get')->with('test'); |
| 45 | + |
| 46 | + $response = new Response; |
| 47 | + $response->addHeader('HTTP/1.1 304 Not Modified'); |
| 48 | + |
| 49 | + $httpClient->request('test', array(), 'GET', array(), $response); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @test |
| 54 | + */ |
| 55 | + public function shouldRenewCacheWhenResourceHasChanged() |
| 56 | + { |
| 57 | + $client = $this->getMock('Buzz\Client\ClientInterface'); |
| 58 | + $client->expects($this->once())->method('send'); |
| 59 | + |
| 60 | + $cache = $this->getCacheMock(); |
| 61 | + |
| 62 | + $httpClient = new CachedHttpClient( |
| 63 | + array('base_url' => ''), |
| 64 | + $client, |
| 65 | + $cache |
| 66 | + ); |
| 67 | + |
| 68 | + $response = new Response; |
| 69 | + $response->addHeader('HTTP/1.1 200 OK'); |
| 70 | + |
| 71 | + $cache->expects($this->once())->method('set')->with('test', $response); |
| 72 | + $cache->expects($this->once())->method('getModifiedSince')->with('test')->will($this->returnValue(1256953732)); |
| 73 | + |
| 74 | + $httpClient->request('test', array(), 'GET', array(), $response); |
| 75 | + } |
| 76 | + |
| 77 | + public function getCacheMock() |
| 78 | + { |
| 79 | + return $this->getMock('Github\HttpClient\Cache\CacheInterface'); |
| 80 | + } |
| 81 | +} |
0 commit comments