forked from ben-xo/dir2cast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCached_Dir_PodcastTest.php
182 lines (138 loc) · 5.1 KB
/
Cached_Dir_PodcastTest.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
class Cached_Dir_PodcastTest extends Dir_PodcastTest
{
public static function setUpBeforeClass(): void
{
Dir_PodcastTest::setUpBeforeClass();
Cached_Dir_Podcast::$MIN_CACHE_TIME = 5;
Cached_Dir_Podcast::$DEBUG = false;
}
public function setUp(): void
{
parent::setUp();
mkdir('temp');
}
public function newPodcast($offset=0)
{
$podcast = new Cached_Dir_Podcast('.', './temp');
$podcast->setClockOffset($offset);
$podcast->init();
return $podcast;
}
public function test_generate_saves_a_cache_file()
{
$this->createTestItems();
age_dir_by('.', 3600);
$this->assertEmpty(glob('temp' . DIRECTORY_SEPARATOR . '*'));
$mp = $this->newPodcast();
$content = $mp->generate();
$this->assertNotEmpty(glob('temp' . DIRECTORY_SEPARATOR . '*'));
}
public function test_uses_generated_cache_file_if_min_time_not_elapsed_yet()
{
$this->createTestItems();
age_dir_by('.', 3600);
$mp = $this->newPodcast();
$content = $mp->generate();
unset($mp); // release lock, in sub tests
age_dir_by('.', 2);
// this should be ignored
file_put_contents('extra.mp3', 'new data');
$mp2 = $this->newPodcast(2);
$content2 = $mp2->generate();
// should not pick up extra.mp3 as the cache file isn't old enough
$this->assertEquals($content, $content2);
$this->assertEquals(0, preg_match('/extra\.mp3/', $content2));
}
public function test_does_not_use_generated_cache_file_if_min_time_has_elapsed_and_theres_new_content()
{
$this->createTestItems();
age_dir_by('.', 3600);
$mp = $this->newPodcast();
$content = $mp->generate();
unset($mp); // release lock, in sub tests
age_dir_by('.', 10);
// this should be considered
file_put_contents('extra.mp3', 'new data');
$mp2 = $this->newPodcast(10);
$content2 = $mp2->generate();
// should pick up extra.mp3 as the cache file is older than the min, and there's new content
$this->assertNotEquals($content, $content2);
$this->assertEquals(1, preg_match('/extra\.mp3/', $content2));
}
public function test_does_not_use_generated_cache_file_if_min_time_has_elapsed_and_theres_additional_old_content()
{
$this->createTestItems();
age_dir_by('.', 3600);
$mp = $this->newPodcast();
$content = $mp->generate();
unset($mp); // release lock, in sub tests
age_dir_by('.', 10);
// this should be considered
file_put_contents('extra.mp3', 'new data');
touch('extra.mp3', time() - 86400);
$mp2 = $this->newPodcast(10);
$content2 = $mp2->generate();
// should pick up extra.mp3 as the cache file is older than the min, and there's new content
$this->assertNotEquals($content, $content2);
$this->assertEquals(1, preg_match('/extra\.mp3/', $content2));
}
public function test_renews_cache_if_old_but_not_stale()
{
$this->createTestItems();
age_dir_by('.', 3600);
$mp = $this->newPodcast();
$content = $mp->generate();
unset($mp); // release lock, in sub tests
age_dir_by('.', 3600);
$mp2 = $this->newPodcast(3600);
$content2 = $mp2->generate();
// should have used cache file anyway
$this->assertEquals($content, $content2);
clearstatcache();
foreach(glob(temp_xml_glob()) as $filename)
{
// cache file should have been refreshed
$this->assertGreaterThan(time() - 3, filemtime($filename));
}
}
public function test_lastBuildDate_is_valid_whether_served_from_cache_or_not()
{
$this->createTestItems();
age_dir_by('.', 3600);
$mp = $this->newPodcast();
$this->assertFalse($mp->isCached());
$lastBuildDate = date('r');
$mp->generate();
clearstatcache();
$this->assertTrue($mp->isCached());
$this->assertEquals($lastBuildDate, $mp->getLastBuildDate());
unset($mp); // release lock, in sub tests
age_dir_by('.', 3600);
$mp2 = $this->newPodcast(3600);
$this->assertTrue($mp2->isCached());
$mp2->generate();
clearstatcache();
$this->assertTrue($mp2->isCached());
$this->assertEquals($lastBuildDate, $mp2->getLastBuildDate());
unset($mp2);
clearstatcache();
age_dir_by('.', 3600);
sleep(1); // not much choice here!
$mp3 = $this->newPodcast(3600+3600);
$mp3->generate();
clearstatcache();
$this->assertEquals($lastBuildDate, $mp3->getLastBuildDate());
}
public function tearDown(): void
{
file_exists('extra.mp3') && unlink('extra.mp3');
is_dir('temp') && rmrf('temp');
parent::tearDown();
}
public static function tearDownAfterClass(): void
{
Dir_PodcastTest::tearDownAfterClass();
}
}