SlugifyServiceProviderTest.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace Cocur\Slugify\Tests\Bridge\League;
  3. use Cocur\Slugify\Bridge\League\SlugifyServiceProvider;
  4. use Cocur\Slugify\RuleProvider\DefaultRuleProvider;
  5. use Cocur\Slugify\RuleProvider\RuleProviderInterface;
  6. use Cocur\Slugify\SlugifyInterface;
  7. use League\Container\Container;
  8. use Mockery as m;
  9. use Mockery\Adapter\Phpunit\MockeryTestCase;
  10. class SlugifyServiceProviderTest extends MockeryTestCase
  11. {
  12. public function testProvidesSlugify()
  13. {
  14. $container = new Container();
  15. $container->addServiceProvider(new SlugifyServiceProvider());
  16. $slugify = $container->get(SlugifyInterface::class);
  17. $this->assertInstanceOf(SlugifyInterface::class, $slugify);
  18. $this->assertInstanceOf(DefaultRuleProvider::class, $this->getProperty($slugify, 'provider'));
  19. }
  20. public function testProvidesSlugifyAsSharedService()
  21. {
  22. $container = new Container();
  23. $container->addServiceProvider(new SlugifyServiceProvider());
  24. $slugify = $container->get(SlugifyInterface::class);
  25. $this->assertSame($slugify, $container->get(SlugifyInterface::class));
  26. }
  27. public function testProvidesSlugifyUsingSharedConfigurationOptions()
  28. {
  29. $container = new Container();
  30. $options = [
  31. 'lowercase' => false,
  32. ];
  33. $container->share('config.slugify.options', $options);
  34. $container->addServiceProvider(new SlugifyServiceProvider());
  35. /* @var SlugifyInterface $slugify */
  36. $slugify = $container->get(SlugifyInterface::class);
  37. $slug = 'Foo-Bar-Baz';
  38. $this->assertSame($slug, $slugify->slugify($slug));
  39. }
  40. public function testProvidesSlugifyUsingSharedProvider()
  41. {
  42. $container = new Container();
  43. $ruleProvider = $this->getRuleProviderMock();
  44. $container->share(RuleProviderInterface::class, $ruleProvider);
  45. $container->addServiceProvider(new SlugifyServiceProvider());
  46. $slugify = $container->get(SlugifyInterface::class);
  47. $this->assertSame($ruleProvider, $this->getProperty($slugify, 'provider'));
  48. }
  49. /**
  50. * @return m\Mock|RuleProviderInterface
  51. */
  52. private function getRuleProviderMock()
  53. {
  54. $ruleProvider = m::mock(RuleProviderInterface::class);
  55. $ruleProvider
  56. ->shouldReceive('getRules')
  57. ->withAnyArgs()
  58. ->andReturn([])
  59. ;
  60. return $ruleProvider;
  61. }
  62. private function getProperty(SlugifyInterface $slugify, string $name)
  63. {
  64. $reflection = new \ReflectionClass($slugify);
  65. $prop = $reflection->getProperty($name);
  66. $prop->setAccessible(true);
  67. return $prop->getValue($slugify);
  68. }
  69. }