Browse Source

Tests

Test, tests, baby (insert sounds from ice, ice, baby)
Belle Aerni 2 years ago
parent
commit
b60439a9a9
4 changed files with 74 additions and 36 deletions
  1. 29 20
      src/AntCMS/AntCMS.php
  2. 2 2
      src/index.php
  3. 10 2
      tests/CMSTest.php
  4. 33 12
      tests/MarkdownTest.php

+ 29 - 20
src/AntCMS/AntCMS.php

@@ -37,8 +37,7 @@ class AntCMS
             $pageTemplate = str_replace('<!--AntCMS-Debug-->', '<p>Took ' . $elapsed_time . ' seconds to render the page. </p>', $pageTemplate);
             $pageTemplate = str_replace('<!--AntCMS-Debug-->', '<p>Took ' . $elapsed_time . ' seconds to render the page. </p>', $pageTemplate);
         }
         }
 
 
-        echo $pageTemplate;
-        exit;
+        return $pageTemplate;
     }
     }
 
 
     public function getPageLayout($theme = null)
     public function getPageLayout($theme = null)
@@ -108,26 +107,36 @@ class AntCMS
 
 
         $templates = AntTools::getFileList($templatePath, 'html');
         $templates = AntTools::getFileList($templatePath, 'html');
 
 
-        if (in_array($layout . '.html', $templates)) {
-            $template = file_get_contents(AntTools::repairFilePath($templatePath . '/' . $layout . '.html'));
-        } else {
-            $template = file_get_contents(AntTools::repairFilePath($defaultTemplates . '/' . $layout . '.html'));
+        try {
+            if (in_array($layout . '.html', $templates)) {
+                $template = file_get_contents(AntTools::repairFilePath($templatePath . '/' . $layout . '.html'));
+            } else {
+                $template = file_get_contents(AntTools::repairFilePath($defaultTemplates . '/' . $layout . '.html'));
+            }
+        } catch (\Exception $e) {
         }
         }
 
 
-        if ($layout == 'default_layout' && !$template) {
-            $template = '
-            <!DOCTYPE html>
-            <html>
-                <head>
-                    <title><!--AntCMS-Title--></title>
-                    <meta name="description" content="<!--AntCMS-Description-->">
-                    <meta name="author" content="<!--AntCMS-Author-->">
-                    <meta name="keywords" content="<!--AntCMS-Keywords-->">
-                </head>
-                <body>
-                    <!--AntCMS-Body-->
-                </body>
-            </html>';
+        if (empty($template)) {
+            if ($layout == 'default_layout') {
+                $template = '
+                <!DOCTYPE html>
+                <html>
+                    <head>
+                        <title><!--AntCMS-Title--></title>
+                        <meta name="description" content="<!--AntCMS-Description-->">
+                        <meta name="author" content="<!--AntCMS-Author-->">
+                        <meta name="keywords" content="<!--AntCMS-Keywords-->">
+                    </head>
+                    <body>
+                        <p>AntCMS had an error when fetching the page template, please contact the site administrator.</p>
+                        <!--AntCMS-Body-->
+                    </body>
+                </html>';
+            } else {
+                $template = '
+                <h1>There was an error</h1>
+                <p>AntCMS had an error when fetching the page template, please contact the site administrator.</p>';
+            }
         }
         }
 
 
         return $template;
         return $template;

+ 2 - 2
src/index.php

@@ -78,9 +78,9 @@ if ($segments[0] === 'plugin') {
 
 
 $indexes = ['/', '/index.php', '/index.html'];
 $indexes = ['/', '/index.php', '/index.html'];
 if (in_array($segments[0], $indexes)) {
 if (in_array($segments[0], $indexes)) {
-    $antCms->renderPage('/');
+    echo $antCms->renderPage('/');
     exit;
     exit;
 } else {
 } else {
-    $antCms->renderPage($requestedPage);
+    echo $antCms->renderPage($requestedPage);
     exit;
     exit;
 }
 }

+ 10 - 2
tests/CMSTest.php

@@ -17,7 +17,6 @@ class CMSTest extends TestCase
         $this->assertEquals('AntCMS', $siteInfo['siteTitle']);
         $this->assertEquals('AntCMS', $siteInfo['siteTitle']);
     }
     }
 
 
-    /* Since this function echos the page and exists processing, we don't get the chance to test the return. Will revist.
     public function testRenderPage(){
     public function testRenderPage(){
         $antCMS = new AntCMS;
         $antCMS = new AntCMS;
         $pagePath = '/index.md';
         $pagePath = '/index.md';
@@ -25,7 +24,7 @@ class CMSTest extends TestCase
 
 
         $this->assertNotEmpty($result);
         $this->assertNotEmpty($result);
         $this->assertIsString($result);
         $this->assertIsString($result);
-    }*/
+    }
 
 
     public function testGetPageLayout()
     public function testGetPageLayout()
     {
     {
@@ -70,4 +69,13 @@ class CMSTest extends TestCase
         $this->assertNotEmpty($result);
         $this->assertNotEmpty($result);
         $this->assertIsString($result);
         $this->assertIsString($result);
     }
     }
+
+    public function testGetThemeTemplateFallback()
+    {
+        $antCMS = new AntCMS;
+        $result = $antCMS->getThemeTemplate('atemplatethatjusdoesntexist');
+
+        $this->assertNotEmpty($result);
+        $this->assertIsString($result);
+    }
 }
 }

+ 33 - 12
tests/MarkdownTest.php

@@ -1,6 +1,7 @@
 <?php
 <?php
 
 
 use AntCMS\AntMarkdown;
 use AntCMS\AntMarkdown;
+use AntCMS\AntConfig;
 use PHPUnit\Framework\TestCase;
 use PHPUnit\Framework\TestCase;
 use PHPUnit\Framework\Constraint\Callback;
 use PHPUnit\Framework\Constraint\Callback;
 
 
@@ -26,7 +27,7 @@ class MarkdownTest extends TestCase
             $totalTime += $end - $start;
             $totalTime += $end - $start;
         }
         }
 
 
-        $averageTime = $totalTime / 5;
+        $averageTime = $totalTime / 10;
 
 
         $constraint = new Callback(function ($averageTime) {
         $constraint = new Callback(function ($averageTime) {
             return $averageTime < 0.015;
             return $averageTime < 0.015;
@@ -36,20 +37,40 @@ class MarkdownTest extends TestCase
     }
     }
 
 
 
 
-    /*public function testMarkdownCacheWorks()
+    public function testMarkdownCacheWorks()
     {
     {
         $markdown = file_get_contents(antContentPath . DIRECTORY_SEPARATOR . 'index.md');
         $markdown = file_get_contents(antContentPath . DIRECTORY_SEPARATOR . 'index.md');
+        $currentConfig = AntConfig::currentConfig();
 
 
-        $start = microtime(true);
-        AntMarkdown::renderMarkdown($markdown);
-        $end = microtime(true);
-        $firstTime = $end - $start;
+        //Disable cache
+        $currentConfig['enableCache'] = false;
+        AntConfig::saveConfig($currentConfig);
 
 
-        $start = microtime(true);
-        AntMarkdown::renderMarkdown($markdown);
-        $end = microtime(true);
-        $secondTime = $end - $start;
+        $totalTime = 0;
+        for ($i = 0; $i < 10; $i++) {
+            $start = microtime(true);
+            AntMarkdown::renderMarkdown($markdown);
+            $end = microtime(true);
+            $totalTime += $end - $start;
+        }
+
+        $withoutCache = $totalTime / 10;
+
+        //Enable cache
+        $currentConfig['enableCache'] = true;
+        AntConfig::saveConfig($currentConfig);
 
 
-        $this->assertLessThan($secondTime, $firstTime, 'Cache didn\'t speed up rendering!');
-    }*/
+        $totalTime = 0;
+        for ($i = 0; $i < 10; $i++) {
+            $start = microtime(true);
+            AntMarkdown::renderMarkdown($markdown);
+            $end = microtime(true);
+            $totalTime += $end - $start;
+        }
+
+        $withCache = $totalTime / 10;
+        
+        echo "\n Markdown rendering speed with cache: $withCache VS without: $withoutCache \n\n";
+        $this->assertLessThan($withoutCache, $withCache, 'Cache didn\'t speed up rendering!');
+    }
 }
 }