TextTest.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. require_once __DIR__.'/../../../../lib/Twig/Extensions/Extension/Text.php';
  11. class Twig_Tests_Extension_TextTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /** @var TwigEnvironment */
  14. private $env;
  15. public function setUp()
  16. {
  17. $this->env = $this->getMockBuilder('Twig_Environment')->disableOriginalConstructor()->getMock();
  18. $this->env
  19. ->expects($this->any())
  20. ->method('getCharset')
  21. ->will($this->returnValue('utf-8'))
  22. ;
  23. }
  24. /**
  25. * @dataProvider getTruncateTestData
  26. */
  27. public function testTruncate($input, $length, $preserve, $separator, $expectedOutput)
  28. {
  29. $output = twig_truncate_filter($this->env, $input, $length, $preserve, $separator);
  30. $this->assertEquals($expectedOutput, $output);
  31. }
  32. public function getTruncateTestData()
  33. {
  34. return array(
  35. array('This is a very long sentence.', 2, false, '...', 'Th...'),
  36. array('This is a very long sentence.', 6, false, '...', 'This i...'),
  37. array('This is a very long sentence.', 2, true, '...', 'This...'),
  38. array('This is a very long sentence.', 2, true, '[...]', 'This[...]'),
  39. array('This is a very long sentence.', 23, false, '...', 'This is a very long sen...'),
  40. array('This is a very long sentence.', 23, true, '...', 'This is a very long sentence.'),
  41. );
  42. }
  43. }