Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancement: Provide SlugifyServiceProvider for use with league/conta…
…iner
- Loading branch information
1 parent
641bd30
commit 4984175
Showing
3 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Cocur\Slugify\Bridge\League; | ||
|
||
use Cocur\Slugify\RuleProvider\DefaultRuleProvider; | ||
use Cocur\Slugify\RuleProvider\RuleProviderInterface; | ||
use Cocur\Slugify\Slugify; | ||
use Cocur\Slugify\SlugifyInterface; | ||
use League\Container\ServiceProvider\AbstractServiceProvider; | ||
|
||
class SlugifyServiceProvider extends AbstractServiceProvider | ||
{ | ||
protected $provides = [ | ||
SlugifyInterface::class, | ||
]; | ||
|
||
public function register() | ||
{ | ||
$this->container->share(SlugifyInterface::class, function () { | ||
$options = []; | ||
if ($this->container->has('config.slugify.options')) { | ||
$options = $this->container->get('config.slugify.options'); | ||
} | ||
|
||
$provider = null; | ||
if ($this->container->has(RuleProviderInterface::class)) { | ||
/* @var RuleProviderInterface $provider */ | ||
$provider = $this->container->get(RuleProviderInterface::class); | ||
} | ||
|
||
return new Slugify( | ||
$options, | ||
$provider | ||
); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
namespace Cocur\Slugify\Tests\Bridge\League; | ||
|
||
use Cocur\Slugify\Bridge\League\SlugifyServiceProvider; | ||
use Cocur\Slugify\RuleProvider\DefaultRuleProvider; | ||
use Cocur\Slugify\RuleProvider\RuleProviderInterface; | ||
use Cocur\Slugify\SlugifyInterface; | ||
use League\Container\Container; | ||
use Mockery as m; | ||
|
||
class SlugifyServiceProviderTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testProvidesSlugify() | ||
{ | ||
$container = new Container(); | ||
|
||
$container->addServiceProvider(new SlugifyServiceProvider()); | ||
|
||
$slugify = $container->get(SlugifyInterface::class); | ||
|
||
$this->assertInstanceOf(SlugifyInterface::class, $slugify); | ||
$this->assertAttributeInstanceOf(DefaultRuleProvider::class, 'provider', $slugify); | ||
} | ||
|
||
public function testProvidesSlugifyAsSharedService() | ||
{ | ||
$container = new Container(); | ||
|
||
$container->addServiceProvider(new SlugifyServiceProvider()); | ||
|
||
$slugify = $container->get(SlugifyInterface::class); | ||
|
||
$this->assertSame($slugify, $container->get(SlugifyInterface::class)); | ||
} | ||
|
||
public function testProvidesSlugifyUsingSharedConfigurationOptions() | ||
{ | ||
$container = new Container(); | ||
|
||
$options = [ | ||
'lowercase' => false, | ||
]; | ||
|
||
$container->share('config.slugify.options', $options); | ||
$container->addServiceProvider(new SlugifyServiceProvider()); | ||
|
||
/* @var SlugifyInterface $slugify */ | ||
$slugify = $container->get(SlugifyInterface::class); | ||
|
||
$slug = 'Foo-Bar-Baz'; | ||
|
||
$this->assertSame($slug, $slugify->slugify($slug)); | ||
} | ||
|
||
public function testProvidesSlugifyUsingSharedProvider() | ||
{ | ||
$container = new Container(); | ||
|
||
$ruleProvider = $this->getRuleProviderMock(); | ||
|
||
$container->share(RuleProviderInterface::class, $ruleProvider); | ||
$container->addServiceProvider(new SlugifyServiceProvider()); | ||
|
||
$slugify = $container->get(SlugifyInterface::class); | ||
|
||
$this->assertAttributeSame($ruleProvider, 'provider', $slugify); | ||
} | ||
|
||
/** | ||
* @return m\Mock|RuleProviderInterface | ||
*/ | ||
private function getRuleProviderMock() | ||
{ | ||
$ruleProvider = m::mock(RuleProviderInterface::class); | ||
|
||
$ruleProvider | ||
->shouldReceive('getRules') | ||
->withAnyArgs() | ||
->andReturn([]) | ||
; | ||
|
||
return $ruleProvider; | ||
} | ||
} |