forked from ben-xo/dir2cast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDir_PodcastTest.php
230 lines (182 loc) · 7.36 KB
/
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
class Dir_PodcastTest extends PodcastTest
{
public static function setUpBeforeClass(): void
{
PodcastTest::setUpBeforeClass();
prepare_testing_dir();
}
public function setUp(): void
{
$this->delete_test_files();
parent::setUp();
Dir_Podcast::$RECURSIVE_DIRECTORY_ITERATOR = false;
Dir_Podcast::$ITEM_COUNT = 10;
Dir_Podcast::$DEBUG = false;
}
public function newPodcast()
{
return new Dir_Podcast('.');
}
public function createTestItems()
{
file_put_contents('test1.mp3', 'content');
file_put_contents('test2.mp4', 'content');
file_put_contents('test3.m4a', 'content');
file_put_contents('test4.other', 'content');
file_put_contents('test5.m4b', 'content');
$filemtime = time();
touch('test1.mp3', $filemtime+50);
touch('test2.mp4', $filemtime);
touch('test3.m4a', $filemtime-50);
touch('test4.other', $filemtime-100);
touch('test5.m4b', $filemtime-75);
return $filemtime;
}
public function createEmptyTestItems()
{
file_put_contents('test1.mp3', '');
file_put_contents('test2.mp4', '');
file_put_contents('test3.m4a', '');
file_put_contents('test4.other', '');
file_put_contents('test5.m4b', '');
$filemtime = time();
touch('test1.mp3', $filemtime+50);
touch('test2.mp4', $filemtime);
touch('test3.m4a', $filemtime-50);
touch('test4.other', $filemtime-100);
touch('test5.m4b', $filemtime-75);
return $filemtime;
}
public function test_empty_dir_leads_to_empty_podcast()
{
$mp = $this->newPodcast();
$content = $mp->generate();
$this->assertEmpty($mp->getItems());
$this->assertEquals(0, $mp->getMaxMtime());
}
public function test_four_supported_files_of_zero_length_not_added_to_podcast()
{
$filemtime = $this->createEmptyTestItems();
$mp = $this->newPodcast();
$content = $mp->generate();
$this->assertEmpty($mp->getItems());
$this->assertEquals(0, $mp->getMaxMtime());
}
public function test_four_supported_files_added_to_podcast()
{
$filemtime = $this->createTestItems();
$mp = $this->newPodcast();
$content = $mp->generate();
$this->assertCount(4, $mp->getItems());
$items = $mp->getItems();
$this->assertInstanceOf(MP3_RSS_Item::class, $items[0]);
$this->assertInstanceOf(MP4_RSS_Item::class, $items[1]);
$this->assertInstanceOf(M4A_RSS_Item::class, $items[2]);
$this->assertInstanceOf(M4A_RSS_Item::class, $items[3]);
$this->assertEquals($filemtime+50, $mp->getMaxMtime());
}
public function test_generating_twice_doesnt_rescan()
{
$filemtime = $this->createTestItems();
$mp = $this->newPodcast();
$mp->generate();
// delete all the files
$this->delete_test_files();
$content = $mp->generate(); // generate again
$this->assertCount(4, $mp->getItems());
$items = $mp->getItems();
$this->assertInstanceOf(MP3_RSS_Item::class, $items[0]);
$this->assertInstanceOf(MP4_RSS_Item::class, $items[1]);
$this->assertInstanceOf(M4A_RSS_Item::class, $items[2]);
$this->assertInstanceOf(M4A_RSS_Item::class, $items[3]);
$this->assertEquals($filemtime+50, $mp->getMaxMtime());
}
public function test_regenerates_if_metadata_files_added()
{
Media_RSS_Item::$DESCRIPTION_SOURCE = 'summary';
$filemtime = $this->createTestItems();
age_dir_by('.', 200);
$mp = $this->newPodcast();
$before = $mp->generate();
$items = $mp->getItems();
$this->assertInstanceOf(MP3_RSS_Item::class, $items[0]);
$this->assertInstanceOf(MP4_RSS_Item::class, $items[1]);
$this->assertInstanceOf(M4A_RSS_Item::class, $items[2]);
$this->assertInstanceOf(M4A_RSS_Item::class, $items[3]);
$this->assertEquals($filemtime+50 - 200, $mp->getMaxMtime());
unset($mp); // releases locks
$this->assertEquals(0, preg_match('/party123/', $before));
$now = time();
age_dir_by('.', 500); // whizz past min cache time
file_put_contents('test2.txt', 'party123');
touch('test2.txt', $now);
age_dir_by('.', 500); // whizz past min file age
$mp = $this->newPodcast();
$after = $mp->generate(); // generate again
$items = $mp->getItems();
// ordering should be preserved
$this->assertInstanceOf(MP3_RSS_Item::class, $items[0]);
$this->assertInstanceOf(MP4_RSS_Item::class, $items[1]);
$this->assertInstanceOf(M4A_RSS_Item::class, $items[2]);
$this->assertInstanceOf(M4A_RSS_Item::class, $items[3]);
$this->assertEquals($now-500, $mp->getMaxMtime());
$this->assertEquals('party123', $items[1]->getSummary());
$this->assertEquals(1, preg_match('/party123/', $after));
unset($mp);
}
public function test_helpers_added_to_found_items()
{
$filemtime = $this->createTestItems();
$mp = $this->newPodcast();
$helper = $this->createMock(Podcast_Helper::class);
$helper->expects($this->atLeastOnce())->method('id')->willReturn('Mock1');
$helper->expects($this->exactly(4))->method('appendToItem');
$helper2 = $this->createMock(Podcast_Helper::class);
$helper2->expects($this->atLeastOnce())->method('id')->willReturn('Mock2');
$helper2->expects($this->exactly(4))->method('appendToItem');
$mp->addHelper($helper);
$mp->addHelper($helper2);
$content = $mp->generate();
}
public function test_files_added_to_podcast_obeys_ITEM_COUNT()
{
Dir_Podcast::$ITEM_COUNT = 2;
$filemtime = $this->createTestItems();
$mp = $this->newPodcast();
$helper = $this->createMock(Podcast_Helper::class);
$helper->expects($this->atLeastOnce())->method('id')->willReturn('Mock1');
$helper->expects($this->exactly(2))->method('appendToItem');
$helper2 = $this->createMock(Podcast_Helper::class);
$helper2->expects($this->atLeastOnce())->method('id')->willReturn('Mock2');
$helper2->expects($this->exactly(2))->method('appendToItem');
$mp->addHelper($helper);
$mp->addHelper($helper2);
$content = $mp->generate();
$this->assertCount(2, $mp->getItems());
$items = $mp->getItems();
$this->assertInstanceOf(MP3_RSS_Item::class, $items[0]);
$this->assertInstanceOf(MP4_RSS_Item::class, $items[1]);
}
protected function delete_test_files()
{
file_exists('test1.mp3') && unlink('test1.mp3');
file_exists('test2.mp4') && unlink('test2.mp4');
file_exists('test3.m4a') && unlink('test3.m4a');
file_exists('test4.other') && unlink('test4.other');
file_exists('test2.txt') && unlink('test2.txt');
file_exists('test5.m4b') && unlink('test5.m4b');
}
public function tearDown(): void
{
Media_RSS_Item::$DESCRIPTION_SOURCE = 'comment';
$this->delete_test_files();
parent::tearDown();
}
public static function tearDownAfterClass(): void
{
PodcastTest::tearDownAfterClass();
chdir('..');
}
}