Skip to content

Commit 20a6ccc

Browse files
committed
UPDATE re-implement cache
NEW DBMarkdownTest for partial coverage NEW travis, scrutinizer and test configuration files
1 parent a9623c4 commit 20a6ccc

9 files changed

+199
-9
lines changed

.gitattributes

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/tests export-ignore
2+
/.gitattributes export-ignore
3+
/.travis.yml export-ignore
4+
/.scrutinizer.yml export-ignore

.scrutinizer.yml

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
inherit: true
2+
3+
#Copied from https://www.adayinthelifeof.nl/2013/11/20/external-code-coverage-with-travis-scrutinizer/
4+
tools:
5+
external_code_coverage:
6+
timeout: 600
7+
php_code_sniffer:
8+
config:
9+
standard: PSR2
10+
php_cs_fixer:
11+
extensions:
12+
# Default:
13+
- php
14+
fixers: []
15+
enabled: false
16+
filter:
17+
paths: [tests/*,src/*]
18+
excluded_paths: []
19+
coding_style:
20+
php:
21+
indentation:
22+
general:
23+
use_tabs: false
24+
25+
checks:
26+
php:
27+
code_rating: true
28+
duplication: true
29+
30+
build:
31+
nodes:
32+
analysis:
33+
tests:
34+
override: [php-scrutinizer-run]
35+
36+
filter:
37+
paths: [tests/*,src/*]

.travis.yml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
language: php
2+
3+
env:
4+
global:
5+
- COMPOSER_ROOT_VERSION=4.0.x-dev
6+
- CODECOV_TOKEN=
7+
- SCRUT_TOKEN=
8+
9+
matrix:
10+
include:
11+
- php: 7.1
12+
env: DB=MYSQL PHPUNIT_TEST=1
13+
- php: 7.2
14+
env: DB=MYSQL PHPCS_TEST=1 PHPUNIT_TEST=1
15+
- php: 7.3
16+
env: DB=MYSQL PHPUNIT_COVERAGE_TEST=1
17+
18+
before_script:
19+
# Init PHP
20+
- phpenv rehash
21+
- phpenv config-rm xdebug.ini
22+
- echo 'memory_limit = 2048M' >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
23+
24+
# Install composer dependencies
25+
- composer require --prefer-dist --no-update silverstripe-themes/simple:~3.2
26+
- composer update --no-suggest --prefer-dist
27+
28+
script:
29+
- if [[ $PHPUNIT_TEST ]]; then vendor/bin/phpunit; fi
30+
- if [[ $PHPUNIT_COVERAGE_TEST ]]; then phpdbg -qrr vendor/bin/phpunit --coverage-clover=coverage.xml && wget https://scrutinizer-ci.com/ocular.phar; fi
31+
- if [[ $PHPCS_TEST ]]; then vendor/bin/phpcs src/ tests/; fi
32+
33+
after_success:
34+
- if [[ $PHPUNIT_COVERAGE_TEST ]]; then bash <(curl -s https://codecov.io/bash) -f coverage.xml -t $CODECOV_TOKEN && travis_retry php ocular.phar code-coverage:upload --format=php-clover --access-token=$SCRUT_TOKEN coverage.xml; fi

_config/cache.yml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
Name: markdown-cache-config
3+
---
4+
SilverStripe\Core\Injector\Injector:
5+
Psr\SimpleCache\CacheInterface.dbMarkdownCache:
6+
factory: SilverStripe\Core\Cache\CacheFactory
7+
constructor:
8+
namespace: 'dbmarkdowncache'

composer.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
"ext-json": "*",
1919
"ext-curl": "*"
2020
},
21+
"require-dev": {
22+
"phpunit/phpunit": "^5.7",
23+
"squizlabs/php_codesniffer": "*"
24+
},
2125
"support": {
2226
"issues": "https://github.com/undefinedoffset/silverstripe-markdown/issues"
2327
},
@@ -32,7 +36,8 @@
3236
},
3337
"autoload": {
3438
"psr-4": {
35-
"UndefinedOffset\\Markdown\\": "src/"
39+
"UndefinedOffset\\Markdown\\": "src/",
40+
"UndefinedOffset\\Markdown\\Test\\": "tests/"
3641
}
3742
}
3843
}

phpcs.xml.dist

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="SS4">
3+
<description>Coding standard for SilverStripe 4.x</description>
4+
5+
<!-- Don't sniff third party libraries -->
6+
<exclude-pattern>*/vendor/*</exclude-pattern>
7+
<exclude-pattern>*/thirdparty/*</exclude-pattern>
8+
9+
<!-- Show progress and output sniff names on violation, and add colours -->
10+
<arg value="sp"/>
11+
<arg name="colors"/>
12+
13+
<!-- Use PSR-2 as a base standard -->
14+
<rule ref="PSR2">
15+
<!-- Allow classes to not declare a namespace -->
16+
<exclude name="PSR1.Classes.ClassDeclaration.MissingNamespace"/>
17+
18+
<!-- Allow underscores in class names -->
19+
<exclude name="Squiz.Classes.ValidClassName.NotCamelCaps"/>
20+
21+
<!-- Allow non camel cased method names -->
22+
<exclude name="PSR1.Methods.CamelCapsMethodName.NotCamelCaps"/>
23+
</rule>
24+
</ruleset>

phpunit.xml.dist

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<phpunit bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true">
2+
<testsuite name="silverstripe-markdown">
3+
<directory>tests</directory>
4+
</testsuite>
5+
6+
<filter>
7+
<whitelist addUncoveredFilesFromWhitelist="true">
8+
<directory suffix=".php">src/</directory>
9+
<exclude>
10+
<directory suffix=".php">tests/</directory>
11+
</exclude>
12+
</whitelist>
13+
</filter>
14+
</phpunit>

src/Model/FieldTypes/DBMarkdown.php

+34-8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@
1515
*/
1616
class DBMarkdown extends DBText
1717
{
18+
/**
19+
* @var string
20+
*/
21+
private $cache_key;
22+
23+
/**
24+
* @var int Cache length for field value in seconds
25+
*/
26+
private static $cache_seconds = 86400;
27+
1828
/**
1929
* {@inheritDoc}
2030
*/
@@ -72,12 +82,11 @@ public function AsHTML($useGFM = false)
7282
}
7383

7484
//Init cache stuff
75-
/*$cacheKey = $this->getCacheKey();
76-
$cache = Injector::inst()->get(CacheInterface::class . '.markdown');
77-
$cachedHTML = $cache->load($cacheKey);//*/
85+
$cacheKey = $this->getCacheKey();
86+
$cache = Injector::inst()->get(CacheInterface::class . '.dbMarkdownCache');
7887

7988
//Check cache, if it's good use it instead
80-
if (isset($cachedHTML) && $cachedHTML !== false) {
89+
if ($cachedHTML = $cache->get($cacheKey)) {
8190
$this->parsedHTML = $cachedHTML;
8291

8392
return $this->parsedHTML;
@@ -95,9 +104,7 @@ public function AsHTML($useGFM = false)
95104
$this->parsedHTML = $response;
96105

97106
//Cache response to file system
98-
if (isset($cache) && isset($cacheKey)) {
99-
$cache->save($this->parsedHTML, $cacheKey);
100-
}
107+
$cache->set($cacheKey, $this->parsedHTML, $this->config()->get('cache_seconds'));
101108

102109
//Reset GFM
103110
if ($renderer instanceof GithubMarkdownRenderer && isset($beforeUseGFM)) {
@@ -151,6 +158,25 @@ private function getRenderer()
151158
*/
152159
public function getCacheKey()
153160
{
154-
return md5('Markdown_' . $this->tableName . '_' . $this->name . '_' . $this->value);
161+
if (!$this->cache_key) {
162+
$this->setCacheKey();
163+
}
164+
165+
return $this->cache_key;
166+
}
167+
168+
/**
169+
* @param null $key
170+
* @return $this
171+
*/
172+
public function setCacheKey($key = null)
173+
{
174+
if ($key === null) {
175+
$key = md5('Markdown_' . $this->tableName . '_' . $this->name . '_' . $this->value);
176+
}
177+
178+
$this->cache_key = $key;
179+
180+
return $this;
155181
}
156182
}

tests/DBMarkdownTest.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace UndefinedOffset\Markdown\Test;
4+
5+
use SilverStripe\Dev\SapphireTest;
6+
use SilverStripe\ORM\FieldType\DBField;
7+
use UndefinedOffset\Markdown\Model\FieldTypes\DBMarkdown;
8+
9+
/**
10+
* Class DBMarkdownTest
11+
* @package UndefinedOffset\Markdown\Test
12+
*/
13+
class DBMarkdownTest extends SapphireTest
14+
{
15+
/**
16+
*
17+
*/
18+
public function testSetCacheKey()
19+
{
20+
/** @var DBMarkdown $field */
21+
$field = DBField::create_field(DBMarkdown::class, '###Headline', 'Markdown');
22+
$this->assertEquals('9ec6af9103bea4520f8f7a2300c23a3b', $field->getCacheKey());
23+
24+
$field->setCacheKey('foo');
25+
$this->assertEquals('foo', $field->getCacheKey());
26+
}
27+
28+
/**
29+
*
30+
*/
31+
public function testKeySeconds()
32+
{
33+
$this->assertEquals(86400, DBMarkdown::config()->get('cache_seconds'));
34+
35+
DBMarkdown::config()->set('cache_seconds', 500);
36+
$this->assertEquals(500, DBMarkdown::config()->get('cache_seconds'));
37+
}
38+
}

0 commit comments

Comments
 (0)