Skip to content

Commit

Permalink
Merge pull request #72 from CarsonF/update-silex-sp
Browse files Browse the repository at this point in the history
Update Silex Service Provider
  • Loading branch information
Florian Eckerstorfer committed Aug 21, 2015
2 parents 64360a0 + cfd4397 commit c3e4715
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
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'));
}
}

0 comments on commit c3e4715

Please sign in to comment.