CustomTransliteratorTest.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace Gedmo\Sluggable;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Doctrine\Common\Util\Debug,
  6. Sluggable\Fixture\Article;
  7. /**
  8. * These are tests for sluggable behavior
  9. *
  10. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  11. * @link http://www.gediminasm.org
  12. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  13. */
  14. class CustomTransliteratorTest extends BaseTestCaseORM
  15. {
  16. const ARTICLE = 'Sluggable\\Fixture\\Article';
  17. public function testStandardTransliteratorFailsOnChineseCharacters()
  18. {
  19. $evm = new EventManager;
  20. $evm->addEventSubscriber(new SluggableListener);
  21. $this->getMockSqliteEntityManager($evm);
  22. $this->populate();
  23. $repo = $this->em->getRepository(self::ARTICLE);
  24. $chinese = $repo->findOneByCode('zh');
  25. $this->assertEquals('zh', $chinese->getSlug());
  26. }
  27. public function testCanUseCustomTransliterator()
  28. {
  29. $evm = new EventManager;
  30. $evm->addEventSubscriber(new MySluggableListener);
  31. $this->getMockSqliteEntityManager($evm);
  32. $this->populate();
  33. $repo = $this->em->getRepository(self::ARTICLE);
  34. $chinese = $repo->findOneByCode('zh');
  35. $this->assertEquals('bei-jing', $chinese->getSlug());
  36. }
  37. private function populate()
  38. {
  39. $chinese = new Article;
  40. $chinese->setTitle('北京');
  41. $chinese->setCode('zh');
  42. $this->em->persist($chinese);
  43. $this->em->flush();
  44. $this->em->clear();
  45. }
  46. protected function getUsedEntityFixtures()
  47. {
  48. return array(
  49. self::ARTICLE
  50. );
  51. }
  52. }
  53. class MySluggableListener extends SluggableListener
  54. {
  55. public function __construct(){
  56. $this->setTransliterator(array('\Gedmo\Sluggable\Transliterator', 'transliterate'));
  57. }
  58. }
  59. class Transliterator
  60. {
  61. public static function transliterate($text, $separator, $object)
  62. {
  63. return 'Bei Jing';
  64. }
  65. }