Cache test
Errietta Kostala [Wed, 27 Aug 2014 17:16:21 +0000 (18:16 +0100)]
Makefile.PL
t/04cache.t [new file with mode: 0644]
t/data/cachetest/share/pages/cachetest.conf [new file with mode: 0644]
t/data/cachetest/share/pages/cachetest/1.html [new file with mode: 0644]
t/data/cachetest/share/pages/cachetest2.conf [new file with mode: 0644]
t/data/cachetest/share/templates/alternate.html [new file with mode: 0644]
t/data/cachetest/share/templates/layout.html [new file with mode: 0644]
t/data/cachetest/share/templates/third.html [new file with mode: 0644]

index 3fa8805..b19ee23 100644 (file)
@@ -18,6 +18,7 @@ WriteMakefile(
     'Plack::Middleware::SSI' => '0.12',
     'JSON::MaybeXS' => 0,
     'Hash::Merge' => 0,
-    'JSONY' => 0
+    'JSONY' => 0,
+    'File::Tempdir' => 0,
   }
 );
diff --git a/t/04cache.t b/t/04cache.t
new file mode 100644 (file)
index 0000000..4475a84
--- /dev/null
@@ -0,0 +1,52 @@
+use warnings;
+use strict;
+use Test::More;
+use App::SCS;
+use IO::All;
+
+use File::Tempdir;
+
+my $tmpdir = File::Tempdir->new();
+my $dir = io($tmpdir->name);
+
+$dir->catdir('share/pages/cachetest')->mkpath;
+$dir->catdir('share/templates')->mkpath;
+
+io('t/data/cachetest/share/pages/cachetest/1.html') > io($dir->catfile('share/pages/cachetest/1.html'));
+io('t/data/cachetest/share/pages/cachetest.conf') > io($dir->catfile('share/pages/cachetest.conf'));
+io('t/data/cachetest/share/templates/layout.html') > io($dir->catfile('share/templates/layout.html'));
+io('t/data/cachetest/share/templates/alternate.html') > io($dir->catfile('share/templates/alternate.html'));
+io('t/data/cachetest/share/templates/third.html') > io($dir->catfile('share/templates/third.html'));
+
+my $app = App::SCS->new(
+  root_dir => $dir
+);
+
+my $cachetest1 = $app->pages->get({ path => 'cachetest/1' });
+
+ok($cachetest1, 'Got a page object');
+
+is(
+  $cachetest1->html,
+  io->file('t/data/cachetest/share/pages/cachetest/1.html')->all,
+  "Correct file loaded"
+);
+
+like ($cachetest1->_html_zoom->to_html,
+  qr{<body>.*<h2>Heading 2</h2>.*</body>}s,
+  'First layout used'
+);
+
+# We want to make sure our config is newer than the cached version
+sleep 1;
+
+io('t/data/cachetest/share/pages/cachetest2.conf') > io($dir->catfile('share/pages/cachetest.conf'));
+
+$cachetest1 = $app->pages->get({ path => 'cachetest/1' });
+
+like ($cachetest1->_html_zoom->to_html,
+  qr{<body>.*<h3>Heading 3</h3>.*</body>}s,
+  'Changing the config invalidates cache, updates content'
+);
+
+done_testing;
diff --git a/t/data/cachetest/share/pages/cachetest.conf b/t/data/cachetest/share/pages/cachetest.conf
new file mode 100644 (file)
index 0000000..1aadd6c
--- /dev/null
@@ -0,0 +1,4 @@
+[
+    "Template",
+    { "name":"alternate" }
+]
diff --git a/t/data/cachetest/share/pages/cachetest/1.html b/t/data/cachetest/share/pages/cachetest/1.html
new file mode 100644 (file)
index 0000000..3bbd9f6
--- /dev/null
@@ -0,0 +1,10 @@
+<html >
+  <head>
+    <title>Config 1</title>
+  </head>
+  <body>
+    <h1>Heading 1</h1>
+    <h2>Heading 2</h2>
+    <h3>Heading 3</h3>
+  </body>
+</html>
diff --git a/t/data/cachetest/share/pages/cachetest2.conf b/t/data/cachetest/share/pages/cachetest2.conf
new file mode 100644 (file)
index 0000000..b4549fa
--- /dev/null
@@ -0,0 +1,4 @@
+[
+    "Template",
+    { "name":"third" }
+]
diff --git a/t/data/cachetest/share/templates/alternate.html b/t/data/cachetest/share/templates/alternate.html
new file mode 100644 (file)
index 0000000..4a8cfda
--- /dev/null
@@ -0,0 +1,8 @@
+<html data-wrap="html">
+  <head data-wrap="head">
+    <title class="page title" />
+  </head>
+  <body>
+    <div data-replace="h2"></div>
+  </body>
+</html>
diff --git a/t/data/cachetest/share/templates/layout.html b/t/data/cachetest/share/templates/layout.html
new file mode 100644 (file)
index 0000000..18e4ce4
--- /dev/null
@@ -0,0 +1,10 @@
+<html data-wrap="html">
+  <head>
+    <title class="page title" />
+  </head>
+  <body>
+    <div id="content">
+      <div data-replace="body" />
+    </div>
+  </body>
+</html>
diff --git a/t/data/cachetest/share/templates/third.html b/t/data/cachetest/share/templates/third.html
new file mode 100644 (file)
index 0000000..8076ac8
--- /dev/null
@@ -0,0 +1,8 @@
+<html data-wrap="html">
+  <head data-wrap="head">
+    <title class="page title" />
+  </head>
+  <body>
+      <div data-replace="h3" />
+  </body>
+</html>