aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/umpirsky/twig-gettext-extractor/Twig/Gettext/Test/ExtractorTest.php
blob: d467835f392cbe0f3f40a00b2451b07f618df11c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php

/**
 * This file is part of the Twig Gettext utility.
 *
 *  (c) Саша Стаменковић <umpirsky@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Twig\Gettext\Test;

use Twig\Gettext\Extractor;
use Twig\Gettext\Loader\Filesystem;
use Symfony\Component\Translation\Loader\PoFileLoader;

/**
 * @author Саша Стаменковић <umpirsky@gmail.com>
 */
class ExtractorTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var \Twig_Environment
     */
    protected $twig;

    /**
     * @var PoFileLoader
     */
    protected $loader;

    protected function setUp()
    {
        $this->twig = new \Twig_Environment(new Filesystem('/'), array(
            'cache'       => '/tmp/cache/'.uniqid(),
            'auto_reload' => true
        ));
        $this->twig->addExtension(new \Twig_Extensions_Extension_I18n());

        $this->loader = new PoFileLoader();
    }

    /**
     * @dataProvider testExtractDataProvider
     */
    public function testExtract(array $templates, array $parameters, array $messages)
    {
        $extractor = new Extractor($this->twig);

        foreach ($templates as $template) {
            $extractor->addTemplate($template);
        }
        foreach ($parameters as $parameter) {
            $extractor->addGettextParameter($parameter);
        }

        $extractor->extract();

        $catalog = $this->loader->load($this->getPotFile(), null);

        foreach ($messages as $message) {
            $this->assertTrue(
                $catalog->has($message),
                sprintf('Message "%s" not found in catalog.', $message)
            );
        }
    }

    public function testExtractDataProvider()
    {
        return array(
            array(
                array(
                    __DIR__.'/Fixtures/twig/singular.twig',
                    __DIR__.'/Fixtures/twig/plural.twig',
                ),
                $this->getGettextParameters(),
                array(
                    'Hello %name%!',
                    'Hello World!',
                    'Hey %name%, I have one apple.',
                    'Hey %name%, I have %count% apples.',
                ),
            ),
        );
    }

    public function testExtractNoTranslations()
    {
        $extractor = new Extractor($this->twig);

        $extractor->addTemplate(__DIR__.'/Fixtures/twig/empty.twig');
        $extractor->setGettextParameters($this->getGettextParameters());

        $extractor->extract();

        $catalog = $this->loader->load($this->getPotFile(), null);

        $this->assertEmpty($catalog->all('messages'));
    }

    private function getPotFile()
    {
        return __DIR__.'/Fixtures/messages.pot';
    }

    private function getGettextParameters()
    {
        return array(
            '--force-po',
            '-o',
            $this->getPotFile(),
        );
    }

    protected function tearDown()
    {
        if (file_exists($this->getPotFile())) {
            unlink($this->getPotFile());
        }
    }
}