CustomTransliteratorTest.php 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. * @package Gedmo.Tests
  12. * @link http://www.gediminasm.org
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. class CustomTransliteratorTest extends BaseTestCaseORM
  16. {
  17. const ARTICLE = 'Sluggable\\Fixture\\Article';
  18. public function testStandardTransliteratorFailsOnChineseCharacters()
  19. {
  20. $evm = new EventManager;
  21. $evm->addEventSubscriber(new SluggableListener);
  22. $this->getMockSqliteEntityManager($evm);
  23. $this->populate();
  24. $repo = $this->em->getRepository(self::ARTICLE);
  25. $chinese = $repo->findOneByCode('zh');
  26. $this->assertEquals('zh', $chinese->getSlug());
  27. }
  28. public function testCanUseCustomTransliterator()
  29. {
  30. $evm = new EventManager;
  31. $evm->addEventSubscriber(new MySluggableListener);
  32. $this->getMockSqliteEntityManager($evm);
  33. $this->populate();
  34. $repo = $this->em->getRepository(self::ARTICLE);
  35. $chinese = $repo->findOneByCode('zh');
  36. $this->assertEquals('bei-jing', $chinese->getSlug());
  37. }
  38. private function populate()
  39. {
  40. $chinese = new Article;
  41. $chinese->setTitle('北京');
  42. $chinese->setCode('zh');
  43. $this->em->persist($chinese);
  44. $this->em->flush();
  45. $this->em->clear();
  46. }
  47. protected function getUsedEntityFixtures()
  48. {
  49. return array(
  50. self::ARTICLE
  51. );
  52. }
  53. }
  54. class MySluggableListener extends SluggableListener
  55. {
  56. public function __construct(){
  57. $this->setTransliterator(array('\Gedmo\Sluggable\Transliterator', 'transliterate'));
  58. }
  59. }
  60. class Transliterator
  61. {
  62. public static function transliterate($text, $separator, $object)
  63. {
  64. return 'Bei Jing';
  65. }
  66. }