Skip to content
This repository was archived by the owner on Nov 8, 2020. It is now read-only.

Commit 90bb21e

Browse files
committed
Update Twig extension for Twig 2. Add unit tests
1 parent b71ec44 commit 90bb21e

File tree

4 files changed

+56
-4
lines changed

4 files changed

+56
-4
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
}
1818
},
1919
"require-dev": {
20-
"phpunit/phpunit": "^4.8"
20+
"phpunit/phpunit": "^4.8",
21+
"twig/twig": "^2.4"
2122
}
2223
}

src/Gravatar/Extension/Twig/GravatarExtension.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public function __construct(Service $service)
2222
public function getFunctions()
2323
{
2424
return array(
25-
'gravatar' => new \Twig_Function_Method($this, 'get'),
26-
'gravatar_exist' => new \Twig_Function_Method($this, 'exist'),
25+
'gravatar' => new \Twig_Function('gravatar', array($this, 'get'), array('is_safe' => array('html'))),
26+
'gravatar_exist' => new \Twig_Function('gravatar_exist', array($this, 'exist')),
2727
);
2828
}
2929

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Gravatar\Tests;
4+
5+
use Gravatar\Service;
6+
use Gravatar\Extension\Twig\GravatarExtension;
7+
8+
use Twig_Environment;
9+
use Twig_Loader_Array;
10+
11+
class TwigExtensionTest extends \PHPUnit_Framework_TestCase
12+
{
13+
public function testRegisterExtension()
14+
{
15+
$twig = new Twig_Environment(new Twig_Loader_Array(array()));
16+
$gravatarService = new Service();
17+
$twig->addExtension(new GravatarExtension($gravatarService));
18+
}
19+
20+
public function testRenderGravatarUrl()
21+
{
22+
$twig = new Twig_Environment(new Twig_Loader_Array(array(
23+
'index.html' => '{{gravatar(email, {"size": 50})}}',
24+
)));
25+
26+
$gravatarService = new Service();
27+
28+
$twig->addExtension(new GravatarExtension($gravatarService));
29+
30+
$url = $twig->render('index.html', array(
31+
'email' => '[email protected]'
32+
));
33+
34+
$this->assertEquals('http://www.gravatar.com/avatar/b58996c504c5638798eb6b511e6f49af?s=50&r=g', $url);
35+
}
36+
37+
public function testRenderGravatarExists()
38+
{
39+
$twig = new Twig_Environment(new Twig_Loader_Array(array(
40+
'exists' => '{{gravatar_exist(email)}}',
41+
)));
42+
43+
$gravatarService = new Service();
44+
45+
$twig->addExtension(new GravatarExtension($gravatarService));
46+
47+
$this->assertEquals("", $twig->render('exists', array('email' => '[email protected]')));
48+
$this->assertEquals("1", $twig->render('exists', array('email' => '[email protected]')));
49+
}
50+
51+
}

tests/bootstrap.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<?php
22

3-
require_once __DIR__ . '/../src/Gravatar/Service.php';
3+
require_once __DIR__ . '/../vendor/autoload.php';

0 commit comments

Comments
 (0)