1
0
mirror of https://github.com/erusev/parsedown.git synced 2023-08-10 21:13:06 +03:00

Cache spec locally for 5 minutes

This commit is contained in:
Aidan Woods 2019-02-03 00:57:23 +00:00
parent f95c3bb154
commit 9bf91d7183
No known key found for this signature in database
GPG Key ID: 9A6A8EFAA512BBB9
2 changed files with 19 additions and 1 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
composer.lock
vendor/
infection.log
tests/spec_cache.txt

View File

@ -17,6 +17,8 @@ use PHPUnit\Framework\TestCase;
class CommonMarkTestStrict extends TestCase
{
const SPEC_URL = 'https://raw.githubusercontent.com/jgm/CommonMark/master/spec.txt';
const SPEC_LOCAL_CACHE = __DIR__ .'/spec_cache.txt';
const SPEC_CACHE_SECONDS = 5 * 60;
protected $parsedown;
@ -41,12 +43,27 @@ class CommonMarkTestStrict extends TestCase
$this->assertEquals($expectedHtml, $actualHtml);
}
public static function getSpec()
{
if (
\is_file(self::SPEC_LOCAL_CACHE)
&& \time() - \filemtime(self::SPEC_LOCAL_CACHE) < self::SPEC_CACHE_SECONDS
) {
$spec = \file_get_contents(self::SPEC_LOCAL_CACHE);
} else {
$spec = \file_get_contents(self::SPEC_URL);
\file_put_contents(self::SPEC_LOCAL_CACHE, $spec);
}
return $spec;
}
/**
* @return array
*/
public function data()
{
$spec = \file_get_contents(self::SPEC_URL);
$spec = self::getSpec();
if ($spec === false) {
$this->fail('Unable to load CommonMark spec from ' . self::SPEC_URL);
}