Skip to content

Commit

Permalink
test: add test for real dependency template check
Browse files Browse the repository at this point in the history
  • Loading branch information
SonyPradana committed Nov 11, 2023
1 parent bcbe55c commit 7b78b21
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 2 deletions.
72 changes: 70 additions & 2 deletions tests/View/TemplatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,17 @@ protected function tearDown(): void
unlink($file);
}
}
if (file_exists(__DIR__ . '/caches/manifest.json')) {
unlink(__DIR__ . '/caches/manifest.json');
$manifests = glob(__DIR__ . '/caches/*.json');
foreach ($manifests as $manifest) {
if (is_file($manifest)) {
unlink($manifest);
}
}
$manifests = glob(__DIR__ . '/caches_fixed/*.json');
foreach ($manifests as $manifest) {
if (is_file($manifest)) {
unlink($manifest);
}
}
}

Expand Down Expand Up @@ -291,4 +300,63 @@ public function itCanRenderRepeatTemplate(): void
$out = $view->render('repeat.include.php', [], false);
$this->assertEquals(6, substr_count($out, 'some text'));
}

/**
* @test
*/
public function itCheckDepencyIsUpdate()
{
$manifest = new Manifestor(
__DIR__ . '/caches_fixed', // templateDir
__DIR__ . '/caches/', // cache_path
'manifestor.dependency.test.json' // manifest_name
);
$templator = new Templator(
__DIR__ . '/caches_fixed', // templateDir
__DIR__ . '/caches/', // cache_path
'manifestor.dependency.test.json' // manifest_name
);

$template_name = 'include.php';
$cache_name = md5($template_name) . '.php';
$manifest->putManifest([
$cache_name => ['include2.php', 'include3.php'],
]);

file_put_contents(__DIR__ . '/caches/' . $cache_name, 'no render required');
$out = $templator->render($template_name, []);

$this->assertTrue($manifest->isDependencyUptodate($cache_name));
$this->assertTrue(Str::contains($out, 'no render required'));
}

/**
* @test
*/
public function itCheckDepencyIsNotUpdate()
{
$manifest = new Manifestor(
__DIR__ . '/caches_fixed', // templateDir
__DIR__ . '/caches/', // cache_path
'manifestor.dependency.test.json' // manifest_name
);
$templator = new Templator(
__DIR__ . '/caches_fixed', // templateDir
__DIR__ . '/caches/', // cache_path
'manifestor.dependency.test.json' // manifest_name
);

$template_name = 'new_file.php';
$cache_name = md5($template_name) . '.php';
$manifest->putManifest([
$cache_name => ['new_file.php'],
]);

file_put_contents(__DIR__ . '/caches_fixed/new_file.php', 'rerender required');
file_put_contents(__DIR__ . '/caches/' . $cache_name, 'no render required');
$out = $templator->render($template_name, []);

$this->assertFalse($manifest->isDependencyUptodate($cache_name));
$this->assertTrue(Str::contains($out, 'rerender required'));
}
}
1 change: 1 addition & 0 deletions tests/View/caches/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.php
*.json
1 change: 1 addition & 0 deletions tests/View/caches_fixed/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*
!old*.php
!include*.php
2 changes: 2 additions & 0 deletions tests/View/caches_fixed/include.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2023-11-11 19:12
{% include('old2.php') %}
2 changes: 2 additions & 0 deletions tests/View/caches_fixed/include2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2023-11-11 19:11
{% include('include3.php') %}
1 change: 1 addition & 0 deletions tests/View/caches_fixed/include3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2023-11-11 18:10

0 comments on commit 7b78b21

Please sign in to comment.