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

Commit

Permalink
Update Twig extension for Twig 2. Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mheap committed Oct 10, 2017
1 parent b71ec44 commit 90bb21e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
}
},
"require-dev": {
"phpunit/phpunit": "^4.8"
"phpunit/phpunit": "^4.8",
"twig/twig": "^2.4"
}
}
4 changes: 2 additions & 2 deletions src/Gravatar/Extension/Twig/GravatarExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public function __construct(Service $service)
public function getFunctions()
{
return array(
'gravatar' => new \Twig_Function_Method($this, 'get'),
'gravatar_exist' => new \Twig_Function_Method($this, 'exist'),
'gravatar' => new \Twig_Function('gravatar', array($this, 'get'), array('is_safe' => array('html'))),
'gravatar_exist' => new \Twig_Function('gravatar_exist', array($this, 'exist')),
);
}

Expand Down
51 changes: 51 additions & 0 deletions tests/Gravatar/Tests/TwigExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Gravatar\Tests;

use Gravatar\Service;
use Gravatar\Extension\Twig\GravatarExtension;

use Twig_Environment;
use Twig_Loader_Array;

class TwigExtensionTest extends \PHPUnit_Framework_TestCase
{
public function testRegisterExtension()
{
$twig = new Twig_Environment(new Twig_Loader_Array(array()));
$gravatarService = new Service();
$twig->addExtension(new GravatarExtension($gravatarService));
}

public function testRenderGravatarUrl()
{
$twig = new Twig_Environment(new Twig_Loader_Array(array(
'index.html' => '{{gravatar(email, {"size": 50})}}',
)));

$gravatarService = new Service();

$twig->addExtension(new GravatarExtension($gravatarService));

$url = $twig->render('index.html', array(
'email' => 'user@example.com'
));

$this->assertEquals('http://www.gravatar.com/avatar/b58996c504c5638798eb6b511e6f49af?s=50&r=g', $url);
}

public function testRenderGravatarExists()
{
$twig = new Twig_Environment(new Twig_Loader_Array(array(
'exists' => '{{gravatar_exist(email)}}',
)));

$gravatarService = new Service();

$twig->addExtension(new GravatarExtension($gravatarService));

$this->assertEquals("", $twig->render('exists', array('email' => 'user@example.com')));
$this->assertEquals("1", $twig->render('exists', array('email' => 'm@michaelheap.com')));
}

}
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php

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

0 comments on commit 90bb21e

Please sign in to comment.