Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Silex Service Provider #72

Merged
merged 3 commits into from
Aug 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/Bridge/Silex/SlugifyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Cocur\Slugify\Bridge\Silex;

use Cocur\Slugify\Bridge\Twig\SlugifyExtension;
use Cocur\Slugify\Slugify;
use Silex\Application;
use Silex\ServiceProviderInterface;
Expand All @@ -31,11 +32,20 @@ class SlugifyServiceProvider implements ServiceProviderInterface
*/
public function register(Application $app)
{
$app['slugify'] = $app->share(function (Application $app) {
$app->flush();
$app['slugify.regex'] = null;
$app['slugify.options'] = array();

return new Slugify();
$app['slugify'] = $app->share(function ($app) {
return new Slugify($app['slugify.regex'], $app['slugify.options']);
});

if (isset($app['twig'])) {
$app['twig'] = $app->share($app->extend('twig', function (\Twig_Environment $twig, $app) {
$twig->addExtension(new SlugifyExtension($app['slugify']));

return $twig;
}));
}
}

/**
Expand Down
36 changes: 17 additions & 19 deletions tests/Bridge/Silex/SlugifySilexProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Cocur\Slugify\Bridge\Silex;

use Cocur\Slugify\Bridge\Silex\SlugifyServiceProvider;
use Silex\Application;
use Silex\Provider\TwigServiceProvider;

/**
* SlugifyServiceProviderTest
Expand All @@ -26,36 +28,32 @@
*/
class SlugifyServiceProviderTest extends \PHPUnit_Framework_TestCase
{
/** @var SlugifyServiceProvider */
private $provider;

public function setUp()
{
$this->provider = new SlugifyServiceProvider();
}

/**
* @test
* @covers Cocur\Slugify\Bridge\Silex\SlugifyServiceProvider::boot()
* @covers Cocur\Slugify\Bridge\Silex\SlugifyServiceProvider
*/
public function boot()
public function register()
{
// it seems like Application is not mockable.
$app = new \Silex\Application();
$this->provider->boot($app);
$app = new Application();
$app->register(new SlugifyServiceProvider());
$app->boot();

$this->assertArrayHasKey('slugify', $app);
$this->assertArrayHasKey('slugify.regex', $app);
$this->assertArrayHasKey('slugify.options', $app);
$this->assertInstanceOf('Cocur\Slugify\Slugify', $app['slugify']);
}

/**
* @test
* @covers Cocur\Slugify\Bridge\Silex\SlugifyServiceProvider::register()
*/
public function register()
public function registerWithTwig()
{
// it seems like Application is not mockable.
$app = new \Silex\Application();
$this->provider->register($app);
$app = new Application();
$app->register(new TwigServiceProvider());
$app->register(new SlugifyServiceProvider());

$this->assertArrayHasKey('slugify', $app);
$this->assertInstanceOf('Cocur\Slugify\Slugify', $app['slugify']);
$this->assertTrue($app['twig']->hasExtension('slugify_extension'));
}
}