From 9bf91d7183e2c9d2c310ae7882308bfdc1df2e34 Mon Sep 17 00:00:00 2001 From: Aidan Woods Date: Sun, 3 Feb 2019 00:57:23 +0000 Subject: [PATCH] Cache spec locally for 5 minutes --- .gitignore | 1 + tests/CommonMarkTestStrict.php | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 725b855..4e728a7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ composer.lock vendor/ infection.log +tests/spec_cache.txt diff --git a/tests/CommonMarkTestStrict.php b/tests/CommonMarkTestStrict.php index c4c8f96..6275d39 100644 --- a/tests/CommonMarkTestStrict.php +++ b/tests/CommonMarkTestStrict.php @@ -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); }