|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the PhpM3u8 package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Chrisyue <http://chrisyue.com/> |
|
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 Chrisyue\PhpM3u8\Loader; |
|
13
|
|
|
|
|
14
|
|
|
function file_get_contents($uri) |
|
|
|
|
|
|
15
|
|
|
{ |
|
16
|
|
|
return 'hello world'; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
namespace Chrisyue\PhpM3u8\Tests\Loader; |
|
20
|
|
|
|
|
21
|
|
|
use Chrisyue\PhpM3u8\Loader\CachableLoader; |
|
|
|
|
|
|
22
|
|
|
use PHPUnit\Framework\TestCase; |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
class CachableLoaderTest extends TestCase |
|
25
|
|
|
{ |
|
26
|
|
View Code Duplication |
public function testLoadWithoutCache() |
|
|
|
|
|
|
27
|
|
|
{ |
|
28
|
|
|
$uri = 'http://example.com'; |
|
29
|
|
|
$expiresAfter = 1800; |
|
30
|
|
|
$content = \Chrisyue\PhpM3u8\Loader\file_get_contents($uri); |
|
31
|
|
|
$cachePool = $this->prophesizeCachePool($uri, false, $content, $expiresAfter); |
|
32
|
|
|
|
|
33
|
|
|
$loader = new CachableLoader($cachePool->reveal(), array('ttl' => $expiresAfter)); |
|
34
|
|
|
$this->assertEquals($loader->load($uri), $content); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
View Code Duplication |
public function testLoadWithCache() |
|
|
|
|
|
|
38
|
|
|
{ |
|
39
|
|
|
$uri = 'http://site.com'; |
|
40
|
|
|
$expiresAfter = 1800; |
|
41
|
|
|
$content = \Chrisyue\PhpM3u8\Loader\file_get_contents($uri); |
|
42
|
|
|
$cachePool = $this->prophesizeCachePool($uri, true, $content, $expiresAfter); |
|
43
|
|
|
|
|
44
|
|
|
$loader = new CachableLoader($cachePool->reveal(), array('ttl' => $expiresAfter)); |
|
45
|
|
|
$this->assertEquals($loader->load($uri), $content); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function testLoadCacheWithDefaultTtlOption() |
|
49
|
|
|
{ |
|
50
|
|
|
$uri = 'http://another.site.com'; |
|
51
|
|
|
$content = \Chrisyue\PhpM3u8\Loader\file_get_contents($uri); |
|
52
|
|
|
$cachePool = $this->prophesizeCachePool($uri, false, $content, 3600); |
|
53
|
|
|
|
|
54
|
|
|
$loader = new CachableLoader($cachePool->reveal()); |
|
55
|
|
|
$this->assertEquals($loader->load($uri), $content); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
private function prophesizeCachePool($uri, $isHit, $content, $expiresAfter) |
|
59
|
|
|
{ |
|
60
|
|
|
$cachePool = $this->prophesize('Psr\Cache\CacheItemPoolInterface'); |
|
61
|
|
|
$cacheItem = $this->prophesize('Psr\Cache\CacheItemInterface'); |
|
62
|
|
|
|
|
63
|
|
|
$key = md5($uri); |
|
64
|
|
|
$cachePool->getItem($key)->shouldBeCalledTimes(1)->willReturn($cacheItem->reveal()); |
|
65
|
|
|
$cacheItem->isHit()->shouldBeCalledTimes(1)->willReturn($isHit); |
|
66
|
|
|
|
|
67
|
|
|
if ($isHit) { |
|
68
|
|
|
$cacheItem->get()->shouldBeCalledTimes(1)->willReturn($content); |
|
69
|
|
|
|
|
70
|
|
|
return $cachePool; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$cacheItem->set($content)->shouldBeCalledTimes(1); |
|
74
|
|
|
$cacheItem->expiresAfter($expiresAfter)->shouldBeCalledTimes(1); |
|
75
|
|
|
|
|
76
|
|
|
$cachePool->save($cacheItem->reveal())->shouldBeCalledTimes(1); |
|
77
|
|
|
|
|
78
|
|
|
return $cachePool; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.