AntCMS/tests/MarkdownTest.php

82 lines
2.4 KiB
PHP
Raw Normal View History

2023-01-09 07:43:17 +00:00
<?php
use AntCMS\AntMarkdown;
use AntCMS\AntConfig;
2023-01-09 07:43:17 +00:00
use PHPUnit\Framework\TestCase;
2023-01-09 08:26:30 +00:00
use PHPUnit\Framework\Constraint\Callback;
2023-01-09 07:43:17 +00:00
include_once 'Includes' . DIRECTORY_SEPARATOR . 'Include.php';
class MarkdownTest extends TestCase
{
2023-01-09 08:26:30 +00:00
public function testCanRenderMarkdown()
2023-01-09 07:43:17 +00:00
{
$result = trim(AntMarkdown::renderMarkdown("# Test Content!"));
$this->assertEquals('<h1>Test Content!</h1>', $result);
}
2023-01-09 08:26:30 +00:00
public function testMarkdownIsFast()
{
$markdown = file_get_contents(antContentPath . DIRECTORY_SEPARATOR . 'index.md');
$totalTime = 0;
2023-01-15 20:42:37 +00:00
$currentConfig = AntConfig::currentConfig();
//Ensure cache is enabled
$currentConfig['cacheMode'] = 'auto';
2023-01-15 20:42:37 +00:00
AntConfig::saveConfig($currentConfig);
2023-01-09 08:26:30 +00:00
for ($i = 0; $i < 10; ++$i) {
$start = microtime(true);
AntMarkdown::renderMarkdown($markdown);
$end = microtime(true);
$totalTime += $end - $start;
}
2023-01-09 08:26:30 +00:00
$averageTime = $totalTime / 10;
$callback = new Callback(static function ($averageTime) {
return $averageTime < 0.015;
2023-01-09 08:26:30 +00:00
});
$this->assertThat($averageTime, $callback, 'AntMarkdown::renderMarkdown took too long on average!');
2023-01-09 08:26:30 +00:00
}
public function testMarkdownCacheWorks()
2023-01-09 08:26:30 +00:00
{
$markdown = file_get_contents(antContentPath . DIRECTORY_SEPARATOR . 'index.md');
$currentConfig = AntConfig::currentConfig();
2023-01-09 08:26:30 +00:00
//Disable cache
$currentConfig['cacheMode'] = 'none';
AntConfig::saveConfig($currentConfig);
2023-01-09 08:26:30 +00:00
$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['cacheMode'] = 'auto';
AntConfig::saveConfig($currentConfig);
2023-01-09 08:26:30 +00:00
$totalTime = 0;
for ($i = 0; $i < 10; ++$i) {
$start = microtime(true);
AntMarkdown::renderMarkdown($markdown);
$end = microtime(true);
$totalTime += $end - $start;
}
$withCache = $totalTime / 10;
2023-01-15 20:42:37 +00:00
echo "\n Markdown rendering speed with cache: {$withCache} VS without: {$withoutCache} \n\n";
$this->assertLessThan($withoutCache, $withCache, "Cache didn't speed up rendering!");
}
2023-01-09 07:43:17 +00:00
}