|
11 | 11 |
|
12 | 12 | namespace Translation\SymfonyStorage\Tests\Unit; |
13 | 13 |
|
| 14 | +use PHPUnit\Framework\TestCase; |
14 | 15 | use Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader; |
| 16 | +use Symfony\Component\Translation\MessageCatalogue; |
15 | 17 | use Symfony\Component\Translation\MessageCatalogueInterface; |
16 | 18 | use Symfony\Component\Translation\Writer\TranslationWriter; |
17 | 19 | use Translation\Common\Model\Message; |
|
21 | 23 | /** |
22 | 24 | * @author Tobias Nyholm <tobias.nyholm@gmail.com> |
23 | 25 | */ |
24 | | -class FileStorageTest extends \PHPUnit_Framework_TestCase |
| 26 | +class FileStorageTest extends TestCase |
25 | 27 | { |
26 | 28 | public function testConstructor() |
27 | 29 | { |
28 | | - new FileStorage(new TranslationWriter(), new TranslationLoader(), ['foo']); |
| 30 | + $storage = new FileStorage(new TranslationWriter(), new TranslationLoader(), ['foo']); |
| 31 | + $this->assertInstanceOf(FileStorage::class, $storage); |
29 | 32 | } |
30 | 33 |
|
31 | 34 | /** |
@@ -88,13 +91,135 @@ public function testCreateExistingCatalogue() |
88 | 91 | ->with( |
89 | 92 | $this->isInstanceOf(MessageCatalogueInterface::class), |
90 | 93 | 'xlf', |
91 | | - ['path' => __DIR__] |
| 94 | + ['path' => $this->getFixturePath()] |
92 | 95 | ); |
93 | 96 |
|
94 | 97 | $loader = new TranslationLoader(); |
95 | 98 | $loader->addLoader('xlf', new XliffLoader()); |
96 | | - $storage = new FileStorage($writer, $loader, ['foo', __DIR__]); |
| 99 | + $storage = new FileStorage($writer, $loader, ['foo', $this->getFixturePath()]); |
97 | 100 |
|
98 | 101 | $storage->create(new Message('key', 'messages', 'en', 'Translation')); |
99 | 102 | } |
| 103 | + |
| 104 | + public function testGet() |
| 105 | + { |
| 106 | + $writer = $this->getMockBuilder(TranslationWriter::class) |
| 107 | + ->disableOriginalConstructor() |
| 108 | + ->getMock(); |
| 109 | + |
| 110 | + $loader = new TranslationLoader(); |
| 111 | + $loader->addLoader('xlf', new XliffLoader()); |
| 112 | + $storage = new FileStorage($writer, $loader, [$this->getFixturePath()]); |
| 113 | + |
| 114 | + $this->assertEquals('Bazbar', $storage->get('en', 'messages', 'test_1')->getTranslation()); |
| 115 | + |
| 116 | + // Missing locale |
| 117 | + $this->assertEquals('test_1', $storage->get('sv', 'messages', 'test_1')->getTranslation()); |
| 118 | + |
| 119 | + // Missing domain |
| 120 | + $this->assertEquals('test_1', $storage->get('en', 'xx', 'test_1')->getTranslation()); |
| 121 | + |
| 122 | + // Missing key |
| 123 | + $this->assertEquals('miss', $storage->get('en', 'messages', 'miss')->getTranslation()); |
| 124 | + } |
| 125 | + |
| 126 | + public function testUpdate() |
| 127 | + { |
| 128 | + $writer = $this->getMockBuilder(TranslationWriter::class) |
| 129 | + ->setMethods(['writeTranslations']) |
| 130 | + ->disableOriginalConstructor() |
| 131 | + ->getMock(); |
| 132 | + $writer->expects($this->exactly(2)) |
| 133 | + ->method('writeTranslations') |
| 134 | + ->with( |
| 135 | + $this->isInstanceOf(MessageCatalogueInterface::class), |
| 136 | + 'xlf', |
| 137 | + ['path' => $this->getFixturePath()] |
| 138 | + ); |
| 139 | + |
| 140 | + $loader = new TranslationLoader(); |
| 141 | + $loader->addLoader('xlf', new XliffLoader()); |
| 142 | + $storage = new FileStorage($writer, $loader, [$this->getFixturePath()]); |
| 143 | + |
| 144 | + $storage->update(new Message('key', 'messages', 'en', 'Translation')); |
| 145 | + $storage->update(new Message('test_1', 'messages', 'en', 'Translation')); |
| 146 | + } |
| 147 | + |
| 148 | + public function testDelete() |
| 149 | + { |
| 150 | + $writer = $this->getMockBuilder(TranslationWriter::class) |
| 151 | + ->setMethods(['writeTranslations']) |
| 152 | + ->disableOriginalConstructor() |
| 153 | + ->getMock(); |
| 154 | + |
| 155 | + $writer->expects($this->once()) |
| 156 | + ->method('writeTranslations') |
| 157 | + ->with( |
| 158 | + $this->callback(function (MessageCatalogueInterface $catalogue) { |
| 159 | + return !$catalogue->defines('test_0', 'messages'); |
| 160 | + }), |
| 161 | + 'xlf', |
| 162 | + ['path' => $this->getFixturePath()] |
| 163 | + ); |
| 164 | + |
| 165 | + $loader = new TranslationLoader(); |
| 166 | + $loader->addLoader('xlf', new XliffLoader()); |
| 167 | + $storage = new FileStorage($writer, $loader, [$this->getFixturePath()]); |
| 168 | + |
| 169 | + $storage->delete('en', 'messages', 'test_0'); |
| 170 | + } |
| 171 | + |
| 172 | + public function testImport() |
| 173 | + { |
| 174 | + $writer = $this->getMockBuilder(TranslationWriter::class) |
| 175 | + ->setMethods(['writeTranslations']) |
| 176 | + ->disableOriginalConstructor() |
| 177 | + ->getMock(); |
| 178 | + |
| 179 | + $writer->expects($this->once()) |
| 180 | + ->method('writeTranslations') |
| 181 | + ->with( |
| 182 | + $this->callback(function (MessageCatalogueInterface $catalogue) { |
| 183 | + return $catalogue->defines('test_4711', 'messages'); |
| 184 | + }), |
| 185 | + 'xlf', |
| 186 | + ['path' => $this->getFixturePath()] |
| 187 | + ); |
| 188 | + |
| 189 | + $loader = new TranslationLoader(); |
| 190 | + $loader->addLoader('xlf', new XliffLoader()); |
| 191 | + $storage = new FileStorage($writer, $loader, [$this->getFixturePath()]); |
| 192 | + $catalogue = new MessageCatalogue('en', ['messages' => ['test_4711' => 'foobar']]); |
| 193 | + |
| 194 | + $storage->import($catalogue); |
| 195 | + } |
| 196 | + |
| 197 | + public function testExport() |
| 198 | + { |
| 199 | + $writer = $this->getMockBuilder(TranslationWriter::class) |
| 200 | + ->disableOriginalConstructor() |
| 201 | + ->getMock(); |
| 202 | + |
| 203 | + $loader = new TranslationLoader(); |
| 204 | + $loader->addLoader('xlf', new XliffLoader()); |
| 205 | + $storage = new FileStorage($writer, $loader, [$this->getFixturePath()]); |
| 206 | + |
| 207 | + $catalogue = new MessageCatalogue('en'); |
| 208 | + $storage->export($catalogue); |
| 209 | + $this->assertTrue($catalogue->defines('test_0', 'messages')); |
| 210 | + $this->assertTrue($catalogue->defines('test_1', 'messages')); |
| 211 | + |
| 212 | + // Wrong locale |
| 213 | + $catalogue = new MessageCatalogue('sv'); |
| 214 | + $storage->export($catalogue); |
| 215 | + $this->assertFalse($catalogue->defines('test_0', 'messages')); |
| 216 | + } |
| 217 | + |
| 218 | + /** |
| 219 | + * @return string |
| 220 | + */ |
| 221 | + private function getFixturePath() |
| 222 | + { |
| 223 | + return realpath(__DIR__.'/../Fixtures/single-file'); |
| 224 | + } |
100 | 225 | } |
0 commit comments