AntCMS/tests/MarkdownTest.php
Belle Aerni 38e51166c3
Implemented APCu caching (#44)
* Start implementing APCu caching

* Fix tests

* Now make PHPStan happy :)

* Fix tests

* Also enable the APCu extension in the tests

* Set maxlife to 7 days & clear with cron

* Implement a more correct way to clear the cache

* Also mention the APCu caching in the readme
2023-05-26 23:46:42 -07:00

81 lines
2.4 KiB
PHP

<?php
use AntCMS\AntMarkdown;
use AntCMS\AntConfig;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Constraint\Callback;
include_once 'Includes' . DIRECTORY_SEPARATOR . 'Include.php';
class MarkdownTest extends TestCase
{
public function testCanRenderMarkdown()
{
$result = trim(AntMarkdown::renderMarkdown("# Test Content!"));
$this->assertEquals('<h1>Test Content!</h1>', $result);
}
public function testMarkdownIsFast()
{
$markdown = file_get_contents(antContentPath . DIRECTORY_SEPARATOR . 'index.md');
$totalTime = 0;
$currentConfig = AntConfig::currentConfig();
//Ensure cache is enabled
$currentConfig['cacheMode'] = 'auto';
AntConfig::saveConfig($currentConfig);
for ($i = 0; $i < 10; ++$i) {
$start = microtime(true);
AntMarkdown::renderMarkdown($markdown);
$end = microtime(true);
$totalTime += $end - $start;
}
$averageTime = $totalTime / 10;
$callback = new Callback(static function ($averageTime) {
return $averageTime < 0.015;
});
$this->assertThat($averageTime, $callback, 'AntMarkdown::renderMarkdown took too long on average!');
}
public function testMarkdownCacheWorks()
{
$markdown = file_get_contents(antContentPath . DIRECTORY_SEPARATOR . 'index.md');
$currentConfig = AntConfig::currentConfig();
//Disable cache
$currentConfig['cacheMode'] = 'none';
AntConfig::saveConfig($currentConfig);
$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);
$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!");
}
}