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