Completed
Push — master ( e25986...ffddd1 )
by Craig
07:06
created

TwigFileExtractorTest::getFileSourceFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zikula\Bundle\CoreBundle\Tests\Translation;
13
14
use JMS\TranslationBundle\Translation\FileSourceFactory;
15
use Symfony\Bridge\Twig\Form\TwigRendererEngine;
16
use Symfony\Bridge\Twig\Form\TwigRenderer;
17
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
18
use Symfony\Component\Finder\SplFileInfo;
19
use Symfony\Component\Routing\RequestContext;
20
use Symfony\Component\Routing\RouteCollection;
21
use Symfony\Component\Routing\Generator\UrlGenerator;
22
use Symfony\Bridge\Twig\Extension\RoutingExtension;
23
use JMS\TranslationBundle\Twig\RemovingNodeVisitor;
24
use JMS\TranslationBundle\Twig\DefaultApplyingNodeVisitor;
25
use JMS\TranslationBundle\Exception\RuntimeException;
26
use Symfony\Component\Translation\MessageSelector;
27
use Symfony\Component\Translation\IdentityTranslator;
28
use Symfony\Bridge\Twig\Extension\TranslationExtension as SymfonyTranslationExtension;
29
use JMS\TranslationBundle\Model\Message;
30
use JMS\TranslationBundle\Model\MessageCatalogue;
31
use JMS\TranslationBundle\Twig\TranslationExtension;
32
use Symfony\Bridge\Twig\Extension\FormExtension;
33
use Zikula\Bundle\CoreBundle\Translation\ZikulaTwigFileExtractor;
34
use Zikula\Bundle\CoreBundle\Twig\Extension\CoreExtension;
35
use Zikula\Bundle\CoreBundle\Twig\Extension\GettextExtension;
36
37
class TwigFileExtractorTest extends KernelTestCase
38
{
39
    public function testExtractSimpleTemplate()
40
    {
41
        $expected = new MessageCatalogue();
42
        $fileSourceFactory = $this->getFileSourceFactory();
43
        $fixtureSplInfo = new \SplFileInfo('/' . __DIR__ . '/Fixture/simple_template.html.twig'); // extra slash in path is necessary :(
44
45
        $message = new Message('text1', 'zikula');
46
        $message->addSource($fileSourceFactory->create($fixtureSplInfo, 1));
47
        $expected->add($message);
48
49
        $message = new Message('text2 %s', 'zikula');
50
        $message->addSource($fileSourceFactory->create($fixtureSplInfo, 3));
51
        $expected->add($message);
52
53
        $message = new Message('text3|text3s', 'zikula');
54
        $message->addSource($fileSourceFactory->create($fixtureSplInfo, 5));
55
        $expected->add($message);
56
57
        $message = new Message('text4 %s|text4s %s', 'zikula');
58
        $message->addSource($fileSourceFactory->create($fixtureSplInfo, 7));
59
        $expected->add($message);
60
61
        $message = new Message('text5', 'my_domain');
62
        $message->addSource($fileSourceFactory->create($fixtureSplInfo, 9));
63
        $expected->add($message);
64
65
        $message = new Message('text6 %s', 'my_domain');
66
        $message->addSource($fileSourceFactory->create($fixtureSplInfo, 11));
67
        $expected->add($message);
68
69
        $message = new Message('text7|text7s', 'my_domain');
70
        $message->addSource($fileSourceFactory->create($fixtureSplInfo, 13));
71
        $expected->add($message);
72
73
        $message = new Message('text8 %s|text8s %s', 'my_domain');
74
        $message->addSource($fileSourceFactory->create($fixtureSplInfo, 15));
75
        $expected->add($message);
76
77
        $message = new Message('bar', 'zikula');
78
        $message->addSource($fileSourceFactory->create($fixtureSplInfo, 17));
79
        $expected->add($message);
80
81
        $message = new Message('foo', 'zikula');
82
        $message->addSource($fileSourceFactory->create($fixtureSplInfo, 19));
83
        $expected->add($message);
84
85
        $message = new Message('foo is foo', 'zikula');
86
        $message->addSource($fileSourceFactory->create($fixtureSplInfo, 19));
87
        $expected->add($message);
88
89
        $this->assertEquals($expected, $this->extract('simple_template.html.twig'));
90
    }
91
92
    public function testExtractDeleteTemplate()
93
    {
94
        $expected = new MessageCatalogue();
95
        $fileSourceFactory = $this->getFileSourceFactory();
96
        $fixtureSplInfo = new \SplFileInfo('/' . __DIR__ . '/Fixture/delete.html.twig');  // extra slash in path is necessary :(
97
98
        $message = new Message('Delete block position', 'zikula');
99
        $message->addSource($fileSourceFactory->create($fixtureSplInfo, 9));
100
        $expected->add($message);
101
102
        $message = new Message("Delete block position", 'zikula');
103
        $message->addSource($fileSourceFactory->create($fixtureSplInfo, 10));
104
        $expected->add($message);
105
106
        $message = new Message('Do you really want to delete position \'%name%\'?', 'zikula');
107
        $message->addSource($fileSourceFactory->create($fixtureSplInfo, 13));
108
        $expected->add($message);
109
110
        $this->assertEquals($expected, $this->extract('delete.html.twig'));
111
    }
112
113
    private function extract($file, ZikulaTwigFileExtractor $extractor = null)
114
    {
115
        if (!is_file($file = __DIR__.'/Fixture/'.$file)) {
116
            throw new RuntimeException(sprintf('The file "%s" does not exist.', $file));
117
        }
118
        $kernel = $this
119
            ->getMockBuilder('\Zikula\Bundle\CoreBundle\HttpKernel\ZikulaKernel')
120
            ->disableOriginalConstructor()
121
            ->getMock();
122
        $kernel
123
            ->method('getBundle')
124
            ->will($this->returnCallback(function ($bundleName) {
125
                $bundle = $this
126
                    ->getMockBuilder('Zikula\Core\AbstractBundle')
127
                    ->disableOriginalConstructor()
128
                    ->getMock();
129
                $bundle
130
                    ->method('getTranslationDomain')
131
                    ->willReturn(strtolower($bundleName));
132
133
                return $bundle;
134
            }));
135
136
        $env = new \Twig_Environment();
137
        $env->addExtension(new SymfonyTranslationExtension($translator = new IdentityTranslator(new MessageSelector())));
138
        $env->addExtension(new TranslationExtension($translator, true));
139
        $env->addExtension(new RoutingExtension(new UrlGenerator(new RouteCollection(), new RequestContext())));
140
        $env->addExtension(new FormExtension(new TwigRenderer(new TwigRendererEngine())));
141
        $env->addExtension(new GettextExtension(new \Zikula\Common\Translator\IdentityTranslator(new MessageSelector()), $kernel));
142
        self::bootKernel();
143
        $env->addExtension(new CoreExtension(self::$kernel->getContainer()));
144
145
        foreach ($env->getNodeVisitors() as $visitor) {
146
            if ($visitor instanceof DefaultApplyingNodeVisitor) {
1 ignored issue
show
Bug introduced by
The class JMS\TranslationBundle\Tw...aultApplyingNodeVisitor does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
147
                $visitor->setEnabled(false);
148
            }
149
            if ($visitor instanceof RemovingNodeVisitor) {
1 ignored issue
show
Bug introduced by
The class JMS\TranslationBundle\Twig\RemovingNodeVisitor does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
150
                $visitor->setEnabled(false);
151
            }
152
        }
153
154
        if (null === $extractor) {
155
            $extractor = new ZikulaTwigFileExtractor($env, $kernel);
156
        }
157
158
        $ast = $env->parse($env->tokenize(file_get_contents($file), $file));
159
160
        $catalogue = new MessageCatalogue();
161
        $extractor->visitTwigFile(new SplFileInfo($file, 'Fixture/', 'Fixture/' . basename($file)), $catalogue, $ast);
162
163
        return $catalogue;
164
    }
165
166
    protected function getFileSourceFactory()
167
    {
168
        return new FileSourceFactory('/');
169
    }
170
}
171