forked from ben-xo/dir2cast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCaching_updates_to_feed_metadataTest.php
159 lines (128 loc) · 5.94 KB
/
Caching_updates_to_feed_metadataTest.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
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
class Caching_updates_to_feed_metadataTest extends TestCase
{
public $file = 'out.xml';
public $output = '';
public $returncode = 0;
public $content = '';
public function setUp(): void
{
prepare_testing_dir();
if(is_link('dir2cast.php')) {
// replace symlink with a real file for this test, if it isn't already.
// It will generally be a symlink if we're doing code coverage (i.e. not on Windows)
$source_file = readlink('dir2cast.php');
unlink('dir2cast.php');
copy($source_file, 'dir2cast.php');
}
if(file_exists('dir2cast.ini')) {
// interferes with this test.
unlink('dir2cast.ini');
}
exec('php dir2cast.php --output=out.xml --dont-uncache --ignore-dir2cast-mtime', $this->output, $this->returncode);
$this->content = file_get_contents($this->file);
age_dir_by('.', 86400);
}
protected function _update_feed()
{
$cached_output_files = glob(temp_xml_glob());
clearstatcache();
$old_mtime = filemtime($cached_output_files[0]);
exec('php dir2cast.php --output=out.xml --dont-uncache --ignore-dir2cast-mtime --min-file-age=0 --debug', $debug_out);
// print(implode("\n", $debug_out));
clearstatcache();
$cached_output_files = glob(temp_xml_glob());
$new_mtime = filemtime($cached_output_files[0]);
// cache file should be refreshed
$this->assertGreaterThan($old_mtime, $new_mtime);
$new_content = file_get_contents($this->file);
return $new_content;
}
public function test_update_to_description_txt_invalidates_cache(): void
{
file_put_contents('description.txt', 'Magic Word');
$new_content = $this->_update_feed();
$this->assertNotEquals($this->content, $new_content);
$this->assertEquals(0, preg_match('/Magic Word/', $this->content));
$this->assertEquals(1, preg_match('/Magic Word/', $new_content));
}
public function test_update_to_itunes_summary_invalidates_cache(): void
{
file_put_contents('itunes_summary.txt', 'Magic Word');
$new_content = $this->_update_feed();
$this->assertNotEquals($this->content, $new_content);
$this->assertEquals(0, preg_match('/Magic Word/', $this->content));
$this->assertEquals(1, preg_match('/Magic Word/', $new_content));
}
public function test_update_to_itunes_subtitle_invalidates_cache(): void
{
file_put_contents('itunes_subtitle.txt', 'Magic Word');
$new_content = $this->_update_feed();
$this->assertNotEquals($this->content, $new_content);
$this->assertEquals(0, preg_match('/Magic Word/', $this->content));
$this->assertEquals(1, preg_match('/Magic Word/', $new_content));
}
public function test_update_to_image_jpg_invalidates_cache(): void
{
file_put_contents('image.jpg', 'Magic Word');
$new_content = $this->_update_feed();
$this->assertNotEquals($this->content, $new_content);
$this->assertEquals(0, preg_match('/image\.jpg/', $this->content));
$this->assertEquals(1, preg_match('/image\.jpg/', $new_content));
}
public function test_update_to_image_png_invalidates_cache(): void
{
file_put_contents('image.png', 'Magic Word');
$new_content = $this->_update_feed();
$this->assertNotEquals($this->content, $new_content);
$this->assertEquals(0, preg_match('/image\.png/', $this->content));
$this->assertEquals(1, preg_match('/image\.png/', $new_content));
}
public function test_update_to_itunes_image_jpg_invalidates_cache(): void
{
file_put_contents('itunes_image.jpg', 'Magic Word');
$new_content = $this->_update_feed();
$this->assertNotEquals($this->content, $new_content);
$this->assertEquals(0, preg_match('/itunes_image\.jpg/', $this->content));
$this->assertEquals(1, preg_match('/itunes_image\.jpg/', $new_content));
}
public function test_update_to_itunes_image_png_invalidates_cache(): void
{
file_put_contents('itunes_image.png', 'Magic Word');
$new_content = $this->_update_feed();
$this->assertNotEquals($this->content, $new_content);
$this->assertEquals(0, preg_match('/itunes_image\.png/', $this->content));
$this->assertEquals(1, preg_match('/itunes_image\.png/', $new_content));
}
public function test_image_ignored_if_ancient(): void
{
file_put_contents('image.jpg', 'Magic Word');
touch('image.jpg', time() - 86400 - 60);
$new_content = $this->_update_feed();
$this->assertEquals($this->content, $new_content);
$this->assertEquals(0, preg_match('/image\.png/', $this->content));
$this->assertEquals(0, preg_match('/image\.jpg/', $this->content));
// an update to png was detected, but jpg still outranks png if both exist
$this->assertEquals(0, preg_match('/image\.png/', $new_content));
$this->assertEquals(0, preg_match('/image\.jpg/', $new_content));
}
public function test_png_ignored_if_jpg_exists(): void
{
file_put_contents('image.png', 'Magic Word');
file_put_contents('image.jpg', 'Magic Word');
touch('image.jpg', time() - 86400 - 60);
$new_content = $this->_update_feed();
$this->assertNotEquals($this->content, $new_content);
$this->assertEquals(0, preg_match('/image\.png/', $this->content));
$this->assertEquals(0, preg_match('/image\.jpg/', $this->content));
// an update to png was detected, but jpg still outranks png if both exist
$this->assertEquals(0, preg_match('/image\.png/', $new_content));
$this->assertEquals(1, preg_match('/image\.jpg/', $new_content));
}
public function tearDown(): void
{
unlink($this->file);
chdir('..');
}
}