Completed
Push — master ( f76b3a...bd6f71 )
by Tobias
04:11
created

FileStorageTest::createTranslationLoader()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[email protected]>
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 Translation\SymfonyStorage\Tests\Unit;
13
14
use PHPUnit\Framework\TestCase;
15
use Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader;
16
use Symfony\Component\Translation\MessageCatalogue;
17
use Symfony\Component\Translation\MessageCatalogueInterface;
18
use Symfony\Component\Translation\Reader\TranslationReader;
19
use Symfony\Component\Translation\Writer\TranslationWriter;
20
use Translation\Common\Model\Message;
21
use Translation\SymfonyStorage\FileStorage;
22
use Translation\SymfonyStorage\Loader\XliffLoader;
23
24
/**
25
 * @author Tobias Nyholm <[email protected]>
26
 */
27
class FileStorageTest extends TestCase
28
{
29
    public function testConstructor()
30
    {
31
        $storage = new FileStorage(new TranslationWriter(), $this->createTranslationLoader(), ['foo']);
32
        $this->assertInstanceOf(FileStorage::class, $storage);
33
    }
34
35
    /**
36
     * @expectedException \LogicException
37
     */
38
    public function testConstructorInvalidLoader()
39
    {
40
        new FileStorage(new TranslationWriter(), new TranslationWriter(), ['foo']);
0 ignored issues
show
Documentation introduced by
new \Symfony\Component\T...ter\TranslationWriter() is of type object<Symfony\Component...iter\TranslationWriter>, but the function expects a object<Symfony\Bundle\Fr...ader\TranslationReader>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
41
    }
42
43
    /**
44
     * @expectedException \LogicException
45
     */
46
    public function testConstructorEmptyArray()
47
    {
48
        new FileStorage(new TranslationWriter(), $this->createTranslationLoader(), []);
49
    }
50
51
    public function testCreateNewCatalogue()
52
    {
53
        $writer = $this->getMockBuilder(TranslationWriter::class)
54
            ->setMethods([$this->getMethodNameToWriteTranslations()])
55
            ->disableOriginalConstructor()
56
            ->getMock();
57
        $writer->expects($this->once())
58
            ->method($this->getMethodNameToWriteTranslations())
59
            ->with(
60
                $this->isInstanceOf(MessageCatalogueInterface::class),
61
                'xlf',
62
                ['path' => 'foo', 'xliff_version' => '2.0']
63
            );
64
65
        $storage = new FileStorage($writer, $this->createTranslationLoader(), ['foo']);
0 ignored issues
show
Documentation introduced by
$writer is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...iter\TranslationWriter>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
66
        $storage->create(new Message('key', 'domain', 'en', 'Message'));
67
68
        $writer = $this->getMockBuilder(TranslationWriter::class)
69
            ->setMethods([$this->getMethodNameToWriteTranslations()])
70
            ->disableOriginalConstructor()
71
            ->getMock();
72
        $writer->expects($this->once())
73
            ->method($this->getMethodNameToWriteTranslations())
74
            ->with(
75
                $this->isInstanceOf(MessageCatalogueInterface::class),
76
                'format',
77
                ['path' => 'bar', 'default_output_format' => 'format', 'xliff_version' => '2.0']
78
            );
79
80
        $storage = new FileStorage($writer, $this->createTranslationLoader(), ['bar'], ['default_output_format' => 'format']);
0 ignored issues
show
Documentation introduced by
$writer is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...iter\TranslationWriter>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
81
        $storage->create(new Message('key', 'domain', 'en', 'Message'));
82
    }
83
84 View Code Duplication
    public function testCreateExistingCatalogue()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
    {
86
        $writer = $this->getMockBuilder(TranslationWriter::class)
87
            ->setMethods([$this->getMethodNameToWriteTranslations()])
88
            ->disableOriginalConstructor()
89
            ->getMock();
90
        $writer->expects($this->once())
91
            ->method($this->getMethodNameToWriteTranslations())
92
            ->with(
93
                $this->isInstanceOf(MessageCatalogueInterface::class),
94
                'xlf',
95
                ['path' => $this->getFixturePath(), 'xliff_version' => '2.0']
96
            );
97
98
        $loader = $this->createTranslationLoader();
99
        $loader->addLoader('xlf', new XliffLoader());
100
        $storage = new FileStorage($writer, $loader, ['foo', $this->getFixturePath()]);
0 ignored issues
show
Documentation introduced by
$writer is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...iter\TranslationWriter>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
101
102
        $storage->create(new Message('key', 'messages', 'en', 'Translation'));
103
    }
104
105
    public function testGet()
106
    {
107
        $writer = $this->getMockBuilder(TranslationWriter::class)
108
            ->disableOriginalConstructor()
109
            ->getMock();
110
111
        $loader = $this->createTranslationLoader();
112
        $loader->addLoader('xlf', new XliffLoader());
113
        $storage = new FileStorage($writer, $loader, [$this->getFixturePath()]);
0 ignored issues
show
Documentation introduced by
$writer is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...iter\TranslationWriter>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
114
115
        $this->assertEquals('Bazbar', $storage->get('en', 'messages', 'test_1')->getTranslation());
116
117
        // Missing locale
118
        $this->assertEquals('test_1', $storage->get('sv', 'messages', 'test_1')->getTranslation());
119
120
        // Missing domain
121
        $this->assertEquals('test_1', $storage->get('en', 'xx', 'test_1')->getTranslation());
122
123
        // Missing key
124
        $this->assertEquals('miss', $storage->get('en', 'messages', 'miss')->getTranslation());
125
    }
126
127
    public function testUpdate()
128
    {
129
        $writer = $this->getMockBuilder(TranslationWriter::class)
130
            ->setMethods([$this->getMethodNameToWriteTranslations()])
131
            ->disableOriginalConstructor()
132
            ->getMock();
133
        $writer->expects($this->exactly(2))
134
            ->method($this->getMethodNameToWriteTranslations())
135
            ->with(
136
                $this->isInstanceOf(MessageCatalogueInterface::class),
137
                'xlf',
138
                ['path' => $this->getFixturePath(), 'xliff_version' => '2.0']
139
            );
140
141
        $loader = $this->createTranslationLoader();
142
        $loader->addLoader('xlf', new XliffLoader());
143
        $storage = new FileStorage($writer, $loader, [$this->getFixturePath()]);
0 ignored issues
show
Documentation introduced by
$writer is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...iter\TranslationWriter>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
144
145
        $storage->update(new Message('key', 'messages', 'en', 'Translation'));
146
        $storage->update(new Message('test_1', 'messages', 'en', 'Translation'));
147
    }
148
149 View Code Duplication
    public function testDelete()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
150
    {
151
        $writer = $this->getMockBuilder(TranslationWriter::class)
152
            ->setMethods([$this->getMethodNameToWriteTranslations()])
153
            ->disableOriginalConstructor()
154
            ->getMock();
155
156
        $writer->expects($this->once())
157
            ->method($this->getMethodNameToWriteTranslations())
158
            ->with(
159
                $this->callback(function (MessageCatalogueInterface $catalogue) {
160
                    return !$catalogue->defines('test_0', 'messages');
161
                }),
162
                'xlf',
163
                ['path' => $this->getFixturePath(), 'xliff_version' => '2.0']
164
            );
165
166
        $loader = $this->createTranslationLoader();
167
        $loader->addLoader('xlf', new XliffLoader());
168
        $storage = new FileStorage($writer, $loader, [$this->getFixturePath()]);
0 ignored issues
show
Documentation introduced by
$writer is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...iter\TranslationWriter>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
169
170
        $storage->delete('en', 'messages', 'test_0');
171
    }
172
173
    public function testImport()
174
    {
175
        $writer = $this->getMockBuilder(TranslationWriter::class)
176
            ->setMethods([$this->getMethodNameToWriteTranslations()])
177
            ->disableOriginalConstructor()
178
            ->getMock();
179
180
        $writer->expects($this->once())
181
            ->method($this->getMethodNameToWriteTranslations())
182
            ->with(
183
                $this->callback(function (MessageCatalogueInterface $catalogue) {
184
                    return $catalogue->defines('test_4711', 'messages');
185
                }),
186
                'xlf',
187
                ['path' => $this->getFixturePath(), 'xliff_version' => '2.0']
188
            );
189
190
        $loader = $this->createTranslationLoader();
191
        $loader->addLoader('xlf', new XliffLoader());
192
        $storage = new FileStorage($writer, $loader, [$this->getFixturePath()]);
0 ignored issues
show
Documentation introduced by
$writer is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...iter\TranslationWriter>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
193
        $catalogue = new MessageCatalogue('en', ['messages' => ['test_4711' => 'foobar']]);
194
195
        $storage->import($catalogue);
196
    }
197
198
    public function testExport()
199
    {
200
        $writer = $this->getMockBuilder(TranslationWriter::class)
201
            ->disableOriginalConstructor()
202
            ->getMock();
203
204
        $loader = $this->createTranslationLoader();
205
        $loader->addLoader('xlf', new XliffLoader());
206
        $storage = new FileStorage($writer, $loader, [$this->getFixturePath()]);
0 ignored issues
show
Documentation introduced by
$writer is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...iter\TranslationWriter>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
207
208
        $catalogue = new MessageCatalogue('en');
209
        $storage->export($catalogue);
210
        $this->assertTrue($catalogue->defines('test_0', 'messages'));
211
        $this->assertTrue($catalogue->defines('test_1', 'messages'));
212
213
        // Wrong locale
214
        $catalogue = new MessageCatalogue('sv');
215
        $storage->export($catalogue);
216
        $this->assertFalse($catalogue->defines('test_0', 'messages'));
217
    }
218
219
    /**
220
     * @return string
221
     */
222
    private function getFixturePath()
223
    {
224
        return realpath(__DIR__.'/../Fixtures/single-file');
225
    }
226
227
    /**
228
     * @return TranslationLoader|TranslationReader
229
     */
230
    private function createTranslationLoader()
231
    {
232
        if (class_exists(TranslationReader::class)) {
233
            return new TranslationReader();
234
        }
235
236
        return new TranslationLoader();
237
    }
238
239
    private function getMethodNameToWriteTranslations()
240
    {
241
        if (method_exists(TranslationWriter::class, 'write')) {
242
            return 'write';
243
        }
244
245
        return 'writeTranslations';
246
    }
247
}
248