Skip to content

Commit

Permalink
Update tests to trick content hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-xo committed May 25, 2022
1 parent a4a6f2b commit fad9b40
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 20 deletions.
37 changes: 30 additions & 7 deletions test/Cached_Dir_PodcastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public static function setUpBeforeClass(): void
{
Dir_PodcastTest::setUpBeforeClass();
Cached_Dir_Podcast::$MIN_CACHE_TIME = 5;
Cached_Dir_Podcast::$DEBUG = false;
}

public function setUp(): void
Expand All @@ -17,9 +18,10 @@ public function setUp(): void
mkdir('temp');
}

public function newPodcast()
public function newPodcast($offset=0)
{
$podcast = new Cached_Dir_Podcast('.', './temp');
$podcast->setClockOffset($offset);
$podcast->init();
return $podcast;
}
Expand Down Expand Up @@ -49,7 +51,7 @@ public function test_uses_generated_cache_file_if_min_time_not_elapsed_yet()
// this should be ignored
file_put_contents('extra.mp3', 'new data');

$mp2 = $this->newPodcast();
$mp2 = $this->newPodcast(2);
$content2 = $mp2->generate();

// should not pick up extra.mp3 as the cache file isn't old enough
Expand All @@ -71,14 +73,36 @@ public function test_does_not_use_generated_cache_file_if_min_time_has_elapsed_a
// this should be considered
file_put_contents('extra.mp3', 'new data');

$mp2 = $this->newPodcast();
$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()
{
Expand All @@ -91,8 +115,7 @@ public function test_renews_cache_if_old_but_not_stale()

age_dir_by('.', 3600);

$mp2 = $this->newPodcast();

$mp2 = $this->newPodcast(3600);
$content2 = $mp2->generate();

// should have used cache file anyway
Expand Down Expand Up @@ -126,7 +149,7 @@ public function test_lastBuildDate_is_valid_whether_served_from_cache_or_not()

age_dir_by('.', 3600);

$mp2 = $this->newPodcast();
$mp2 = $this->newPodcast(3600);
$this->assertTrue($mp2->isCached());
$mp2->generate();
clearstatcache();
Expand All @@ -138,7 +161,7 @@ public function test_lastBuildDate_is_valid_whether_served_from_cache_or_not()
clearstatcache();
age_dir_by('.', 3600);
sleep(1); // not much choice here!
$mp3 = $this->newPodcast();
$mp3 = $this->newPodcast(3600+3600);
$mp3->generate();

clearstatcache();
Expand Down
18 changes: 9 additions & 9 deletions test/CachingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function test_default_empty_podcast_doesnt_regenerate_in_first_MIN_CACHE_
clearstatcache();
$cached_mtime_before = filemtime($cached_output_files[0]);

exec('php dir2cast.php --output=out.xml --dont-uncache --ignore-dir2cast-mtime', $new_output, $this->returncode);
exec('php dir2cast.php --output=out.xml --dont-uncache --ignore-dir2cast-mtime --clock-offset=7', $new_output, $this->returncode);

clearstatcache();
$cached_output_files = glob(temp_xml_glob());
Expand Down Expand Up @@ -71,7 +71,7 @@ public function test_default_empty_podcast_renews_cache_file_mtime_after_MIN_CAC
clearstatcache();
$cached_mtime_before = filemtime($cached_output_files[0]);

exec('php dir2cast.php --output=out.xml --dont-uncache --ignore-dir2cast-mtime', $new_output, $this->returncode);
exec('php dir2cast.php --output=out.xml --dont-uncache --ignore-dir2cast-mtime --clock-offset=7', $new_output, $this->returncode);

clearstatcache();
$cached_output_files = glob(temp_xml_glob());
Expand Down Expand Up @@ -104,7 +104,7 @@ public function test_default_empty_podcast_doesnt_regenerate_before_MIN_CACHE_TI
clearstatcache();
$cached_mtime_before = filemtime($cached_output_files[0]);

exec('php dir2cast.php --output=out.xml --dont-uncache --ignore-dir2cast-mtime', $new_output, $this->returncode);
exec('php dir2cast.php --output=out.xml --dont-uncache --ignore-dir2cast-mtime --clock-offset=7', $new_output, $this->returncode);

clearstatcache();
$cached_output_files = glob(temp_xml_glob());
Expand Down Expand Up @@ -138,7 +138,7 @@ public function test_default_empty_podcast_regenerates_after_MIN_CACHE_TIME_with
clearstatcache();
$cached_mtime_before = filemtime($cached_output_files[0]);

exec('php dir2cast.php --output=out.xml --dont-uncache --ignore-dir2cast-mtime', $new_output, $this->returncode);
exec('php dir2cast.php --output=out.xml --dont-uncache --ignore-dir2cast-mtime --clock-offset=90', $new_output, $this->returncode);

clearstatcache();
$cached_output_files = glob(temp_xml_glob());
Expand Down Expand Up @@ -168,7 +168,7 @@ public function test_default_empty_podcast_obeys_minimum_cache_time_not_elapsed(

// --dont-uncache: tells dir2cast not use the default caching rules, not ignore them due to CLI
// --min-file-age=0 : tells dir2cast to include files that are brand new
exec('php dir2cast.php --output=out.xml --dont-uncache --min-file-age=0 --ignore-dir2cast-mtime', $this->output, $this->returncode);
exec('php dir2cast.php --output=out.xml --dont-uncache --min-file-age=0 --ignore-dir2cast-mtime --clock-offset=2', $this->output, $this->returncode);

$new_content = file_get_contents($this->file);
$this->assertEquals($this->content, $new_content);
Expand All @@ -184,7 +184,7 @@ public function test_default_empty_podcast_obeys_minimum_cache_time_elapsed(): v

// --dont-uncache: tells dir2cast not use the default caching rules, not ignore them due to CLI
// --min-file-age=0 : tells dir2cast to include files that are brand new
exec('php dir2cast.php --output=out.xml --dont-uncache --min-file-age=0 --ignore-dir2cast-mtime', $this->output, $this->returncode);
exec('php dir2cast.php --output=out.xml --dont-uncache --min-file-age=0 --ignore-dir2cast-mtime --clock-offset=3600', $this->output, $this->returncode);

$new_content = file_get_contents($this->file);
$this->assertNotEquals($this->content, $new_content);
Expand All @@ -198,7 +198,7 @@ public function test_default_empty_podcast_uncaches_without_dont_uncache_even_if
// too new to bust the cache, but cli runner uncaches anyway
file_put_contents('empty.mp3', 'test');

exec('php dir2cast.php --output=out.xml --min-file-age=0 --ignore-dir2cast-mtime', $this->output, $this->returncode);
exec('php dir2cast.php --output=out.xml --min-file-age=0 --ignore-dir2cast-mtime --clock-offset=2', $this->output, $this->returncode);

$new_content = file_get_contents($this->file);
$this->assertNotEquals($this->content, $new_content);
Expand All @@ -217,7 +217,7 @@ public function test_expired_podcast_is_regenerated(): void
file_put_contents('empty.mp3', 'test');
touch('empty.mp3', time()-3600); // busts cache as older than min-file-age

exec('php dir2cast.php --output=out.xml --dont-uncache --min-file-age=30 --ignore-dir2cast-mtime');
exec('php dir2cast.php --output=out.xml --dont-uncache --min-file-age=30 --ignore-dir2cast-mtime --clock-offset=86400');
$new_content = file_get_contents($this->file); // should have empty.mp3
$this->assertNotEquals($this->content, $new_content);
$this->assertEquals(1, preg_match('/empty\.mp3/', $new_content));
Expand Down Expand Up @@ -246,7 +246,7 @@ public function test_too_new_file_not_included_in_podcast(): void
// (which it shouldn't be!)
sleep(1);

exec('php dir2cast.php --output=out.xml --dont-uncache --min-file-age=30 --ignore-dir2cast-mtime');
exec('php dir2cast.php --output=out.xml --dont-uncache --min-file-age=30 --ignore-dir2cast-mtime --clock-offset=86400');

$new_content = file_get_contents($this->file); // should not have empty.mp3

Expand Down
3 changes: 1 addition & 2 deletions test/Dir_PodcastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function setUp(): void
parent::setUp();
Dir_Podcast::$RECURSIVE_DIRECTORY_ITERATOR = false;
Dir_Podcast::$ITEM_COUNT = 10;
Dir_Podcast::$DEBUG = false;
}

public function newPodcast()
Expand Down Expand Up @@ -110,7 +111,6 @@ public function test_generating_twice_doesnt_rescan()

public function test_regenerates_if_metadata_files_added()
{
Dir_Podcast::$DEBUG = false;
Media_RSS_Item::$DESCRIPTION_SOURCE = 'summary';
$filemtime = $this->createTestItems();
age_dir_by('.', 200);
Expand Down Expand Up @@ -204,7 +204,6 @@ protected function delete_test_files()

public function tearDown(): void
{
Dir_Podcast::$DEBUG = false;
Media_RSS_Item::$DESCRIPTION_SOURCE = 'comment';
$this->delete_test_files();
parent::tearDown();
Expand Down
1 change: 0 additions & 1 deletion test/Dir_Podcast_RecursiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ protected function delete_test_files()

public function test_regenerates_if_metadata_files_added()
{
Dir_Podcast::$DEBUG = false;
Media_RSS_Item::$DESCRIPTION_SOURCE = 'summary';
$filemtime = $this->createTestItems();
age_dir_by('.', 200);
Expand Down
3 changes: 2 additions & 1 deletion test/Locking_Cached_Dir_PodcastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ public static function setUpBeforeClass(): void
Cached_Dir_PodcastTest::setUpBeforeClass();
}

public function newPodcast()
public function newPodcast($offset=0)
{
$podcast = new Locking_Cached_Dir_Podcast('.', './temp');
$podcast->setClockOffset($offset);
$podcast->init();
return $podcast;
}
Expand Down

0 comments on commit fad9b40

Please sign in to comment.