SlugifyServiceTest.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace Cocur\Slugify\Tests\Bridge\ZF2;
  3. use Cocur\Slugify\Bridge\ZF2\Module;
  4. use Cocur\Slugify\Bridge\ZF2\SlugifyService;
  5. use Zend\ServiceManager\ServiceManager;
  6. use Mockery\Adapter\Phpunit\MockeryTestCase;
  7. /**
  8. * Class SlugifyServiceTest
  9. * @package cocur/slugify
  10. * @subpackage bridge
  11. * @license http://www.opensource.org/licenses/MIT The MIT License
  12. */
  13. class SlugifyServiceTest extends MockeryTestCase
  14. {
  15. /**
  16. * @var SlugifyService
  17. */
  18. private $slugifyService;
  19. protected function setUp(): void
  20. {
  21. $this->slugifyService = new SlugifyService();
  22. }
  23. /**
  24. * @covers \Cocur\Slugify\Bridge\ZF2\SlugifyService::__invoke()
  25. */
  26. public function testInvokeWithoutCustomConfig()
  27. {
  28. $sm = $this->createServiceManagerMock();
  29. $slugify = call_user_func($this->slugifyService, $sm);
  30. $this->assertInstanceOf('Cocur\Slugify\Slugify', $slugify);
  31. // Make sure reg exp is default one
  32. $actual = 'Hello My Friend.zip';
  33. $expected = 'hello-my-friend-zip';
  34. $this->assertSame($expected, $slugify->slugify($actual));
  35. }
  36. /**
  37. * @covers \Cocur\Slugify\Bridge\ZF2\SlugifyService::__invoke()
  38. */
  39. public function testInvokeWithCustomConfig()
  40. {
  41. $sm = $this->createServiceManagerMock([
  42. Module::CONFIG_KEY => [
  43. 'options' => ['regexp' => '/([^a-z0-9.]|-)+/']
  44. ]
  45. ]);
  46. $slugify = call_user_func($this->slugifyService, $sm);
  47. $this->assertInstanceOf('Cocur\Slugify\Slugify', $slugify);
  48. // Make sure reg exp is the one provided and dots are kept
  49. $actual = 'Hello My Friend.zip';
  50. $expected = 'hello-my-friend.zip';
  51. $this->assertSame($expected, $slugify->slugify($actual));
  52. }
  53. protected function createServiceManagerMock(array $config = [])
  54. {
  55. $sm = new ServiceManager();
  56. $sm->setService('Config', $config);
  57. return $sm;
  58. }
  59. }