Skip to content

Commit b9bae1a

Browse files
committed
cq
1 parent 8ead19d commit b9bae1a

4 files changed

Lines changed: 87 additions & 7 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/composer.lock
22
/composer.phar
33
/vendor/
4+
.phpunit.result.cache

composer.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
}
2626
],
2727
"require": {
28-
"php": "~7.0",
28+
"php": "~7.2",
2929

3030
"twig/twig": "~2.11|~3.0"
3131
},
@@ -45,6 +45,11 @@
4545
"Shapecode\\": "src/"
4646
}
4747
},
48+
"autoload-dev": {
49+
"psr-4": {
50+
"Shapecode\\Tests\\": "tests/"
51+
}
52+
},
4853
"scripts": {
4954
"check": [
5055
"@crc",
@@ -60,7 +65,7 @@
6065
},
6166
"extra": {
6267
"branch-alias": {
63-
"dev-master": "1.0-dev"
68+
"dev-master": "1.1-dev"
6469
}
6570
},
6671
"minimum-stability": "dev",

src/Twig/Loader/StringLoader.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,55 @@
44

55
namespace Shapecode\Twig\Loader;
66

7+
use Twig\Error\LoaderError;
78
use Twig\Loader\LoaderInterface;
89
use Twig\Source;
10+
use function md5;
911
use function preg_match;
12+
use function sprintf;
1013

1114
class StringLoader implements LoaderInterface
1215
{
1316
/**
1417
* @inheritDoc
1518
*/
16-
public function getSourceContext($name)
19+
public function getSourceContext($name) : Source
1720
{
21+
if (! $this->exists($name)) {
22+
throw new LoaderError(sprintf('Template "%s" is not defined.', $name));
23+
}
24+
1825
return new Source($name, $name);
1926
}
2027

2128
/**
2229
* @inheritdoc
2330
*/
24-
public function getCacheKey($name)
31+
public function getCacheKey($name) : string
2532
{
26-
return $name;
33+
if (! $this->exists($name)) {
34+
throw new LoaderError(sprintf('Template "%s" is not defined.', $name));
35+
}
36+
37+
return md5($name);
2738
}
2839

2940
/**
3041
* @inheritdoc
3142
*/
32-
public function isFresh($name, $time)
43+
public function isFresh($name, $time) : bool
3344
{
45+
if (! $this->exists($name)) {
46+
throw new LoaderError(sprintf('Template "%s" is not defined.', $name));
47+
}
48+
3449
return true;
3550
}
3651

3752
/**
3853
* @inheritDoc
3954
*/
40-
public function exists($name)
55+
public function exists($name) : bool
4156
{
4257
return (bool) preg_match('/\s/', $name);
4358
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Shapecode\Tests\Twig\Loader;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Shapecode\Twig\Loader\StringLoader;
9+
use Twig\Error\LoaderError;
10+
use function time;
11+
12+
class StringLoaderTest extends TestCase
13+
{
14+
public function testGetSourceContextWhenTemplateDoesNotExist() : void
15+
{
16+
$this->expectException(LoaderError::class);
17+
$loader = new StringLoader();
18+
$loader->getSourceContext('foo');
19+
}
20+
21+
public function testGetCacheKey() : void
22+
{
23+
$loader = new StringLoader();
24+
self::assertEquals('327b6f07435811239bc47e1544353273', $loader->getCacheKey('foo bar'));
25+
}
26+
27+
public function testGetCacheKeyWhenTemplateDoesNotExist() : void
28+
{
29+
$this->expectException(LoaderError::class);
30+
$loader = new StringLoader();
31+
$loader->getCacheKey('foo');
32+
}
33+
34+
public function testIsFresh() : void
35+
{
36+
$loader = new StringLoader();
37+
self::assertTrue($loader->isFresh('foo bar', time()));
38+
}
39+
40+
public function testIsFreshWhenTemplateDoesNotExist() : void
41+
{
42+
$this->expectException(LoaderError::class);
43+
44+
$loader = new StringLoader();
45+
$loader->isFresh('foo', time());
46+
}
47+
48+
public function testExists() : void
49+
{
50+
$loader = new StringLoader();
51+
self::assertTrue($loader->exists('foo bar'));
52+
}
53+
54+
public function testExistsFails() : void
55+
{
56+
$loader = new StringLoader();
57+
self::assertFalse($loader->exists('foo'));
58+
}
59+
}

0 commit comments

Comments
 (0)