Skip to content

Commit 204f589

Browse files
author
Martynas Sudintas
committed
Merge pull request #39 from martiis/patch-tests-1
Patch tests 1
2 parents d4676d3 + 3fc10b3 commit 204f589

File tree

6 files changed

+140
-26
lines changed

6 files changed

+140
-26
lines changed

Command/ImportCommand.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
119119
*/
120120
private function validateBundleNamespace($bundleName)
121121
{
122-
try {
123-
new \ReflectionClass($bundleName);
124-
} catch (\ReflectionException $e) {
122+
if (!class_exists($bundleName)) {
125123
throw new InvalidArgumentException(
126-
"Invalid bundle namespace '{$bundleName}'. Error: {$e->getMessage()}"
124+
"Invalid bundle namespace '{$bundleName}'"
127125
);
128126
}
129127
}

DependencyInjection/ONGRTranslationsExtension.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ private function validateBundles($container, $bundles)
135135
foreach ($bundles as $bundle) {
136136
if (!class_exists($bundle)) {
137137
throw new InvalidConfigurationException(
138-
"Invalid bundle namespace {$bundle}. Error: {$e->getMessage()}"
138+
"Invalid bundle namespace '{$bundle}'."
139139
);
140140
}
141141
}

Document/Translation.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,11 @@ public function setKey($key)
155155
*/
156156
public function getId()
157157
{
158-
return sha1($this->getDomain() . $this->getKey());
158+
if (!parent::getId()) {
159+
$this->setId(sha1($this->getDomain() . $this->getKey()));
160+
}
161+
162+
return parent::getId();
159163
}
160164

161165
/**

Tests/Functional/Command/ImportCommandTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,21 @@ public function testConfigOnlyOptionImport()
134134
$this->assertGreaterThan(0, $this->getTranslationsCount(['lt'], ['messages']));
135135
}
136136

137+
/**
138+
* Tests if exception is thrown when unknown bundle is provided.
139+
*
140+
* @expectedException \InvalidArgumentException
141+
*/
142+
public function testIncorrectBundleImportException()
143+
{
144+
$this->commandTester->execute(
145+
[
146+
'command' => $this->command->getName(),
147+
'bundle' => 'Acme\AcmeTestBundle',
148+
]
149+
);
150+
}
151+
137152
/**
138153
* Returns translations count.
139154
*

Tests/Functional/Controller/ApiControllerTest.php

+73-20
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,26 @@
1212
namespace ONGR\TranslationsBundle\Tests\Functional\Controller;
1313

1414
use ONGR\ElasticsearchBundle\Test\AbstractElasticsearchTestCase;
15+
use ONGR\TranslationsBundle\Document\Message;
16+
use org\bovigo\vfs\vfsStream;
17+
use Symfony\Component\Yaml\Yaml;
1518

1619
/**
1720
* Tests rest controller actions.
1821
*/
1922
class ApiControllerTest extends AbstractElasticsearchTestCase
2023
{
24+
const STREAM = 'translations_ctrl_api_test';
25+
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
protected function setUp()
30+
{
31+
parent::setUp();
32+
vfsStream::setup(self::STREAM);
33+
}
34+
2135
/**
2236
* {@inheritdoc}
2337
*/
@@ -27,8 +41,11 @@ protected function getDataArray()
2741
'default' => [
2842
'translation' => [
2943
[
30-
'_id' => sha1('foo.key'),
44+
'_id' => sha1('foofoo.key'),
3145
'key' => 'foo.key',
46+
'domain' => 'foo',
47+
'path' => vfsStream::url(self::STREAM),
48+
'format' => 'yml',
3249
'tags' => [
3350
[
3451
'name' => 'foo_tag',
@@ -37,29 +54,32 @@ protected function getDataArray()
3754
'name' => 'tuna_tag',
3855
],
3956
],
40-
'messages' =>
57+
'messages' => [
4158
[
42-
[
43-
'locale' => 'en',
44-
'message' => 'foo',
45-
],
59+
'locale' => 'en',
60+
'message' => 'foo',
61+
'status' => Message::FRESH,
4662
],
63+
],
4764
],
4865
[
49-
'_id' => sha1('baz.key'),
66+
'_id' => sha1('bazbaz.key'),
5067
'key' => 'baz.key',
68+
'domain' => 'baz',
69+
'path' => vfsStream::url(self::STREAM),
70+
'format' => 'yml',
5171
'tags' => [
5272
[
5373
'name' => 'baz_tag',
5474
],
5575
],
56-
'messages' =>
76+
'messages' => [
5777
[
58-
[
59-
'locale' => 'en',
60-
'message' => 'baz',
61-
],
78+
'locale' => 'en',
79+
'message' => 'baz',
80+
'status' => Message::DIRTY,
6281
],
82+
],
6383
],
6484
],
6585
],
@@ -136,7 +156,7 @@ public function testActionStatusCode($method, $url, $statusCode, $content = '')
136156
public function testEditTagAction()
137157
{
138158
$client = self::createClient();
139-
$id = sha1('foo.key');
159+
$id = sha1('foofoo.key');
140160

141161
$requestContent = json_encode(
142162
[
@@ -176,7 +196,7 @@ public function testEditTagAction()
176196
public function testEditMessageAction()
177197
{
178198
$client = self::createClient();
179-
$id = sha1('foo.key');
199+
$id = sha1('foofoo.key');
180200

181201
$requestContent = json_encode(
182202
[
@@ -216,7 +236,7 @@ public function testEditMessageAction()
216236
public function testGetTagsAction()
217237
{
218238
$client = self::createClient();
219-
$id = sha1('foo.key');
239+
$id = sha1('foofoo.key');
220240

221241
$requestContent = json_encode(
222242
[
@@ -240,13 +260,13 @@ public function testGetTagsAction()
240260
[
241261
[
242262
'tags' => [
243-
['name' => 'foo_tag'],
244-
['name' => 'tuna_tag'],
263+
['name' => 'baz_tag'],
245264
],
246265
],
247266
[
248267
'tags' => [
249-
['name' => 'baz_tag'],
268+
['name' => 'foo_tag'],
269+
['name' => 'tuna_tag'],
250270
],
251271
],
252272
],
@@ -260,7 +280,7 @@ public function testGetTagsAction()
260280
public function testRemoveAction()
261281
{
262282
$client = self::createClient();
263-
$id = sha1('foo.key');
283+
$id = sha1('foofoo.key');
264284

265285
$requestContent = json_encode(
266286
[
@@ -299,7 +319,7 @@ public function testRemoveAction()
299319
public function testAddAction()
300320
{
301321
$client = self::createClient();
302-
$id = sha1('foo.key');
322+
$id = sha1('foofoo.key');
303323

304324
$requestContent = json_encode(
305325
[
@@ -336,4 +356,37 @@ public function testAddAction()
336356
$this->assertNotFalse($key, 'tag should exist');
337357
$this->assertEquals('new_foo_tag', $tags[$key], 'Tag should have name as defined in request.');
338358
}
359+
360+
/**
361+
* Tests export action.
362+
*/
363+
public function testExportAction()
364+
{
365+
$client = self::createClient();
366+
$currentDir = getcwd();
367+
$webDir = $currentDir . DIRECTORY_SEPARATOR . 'web';
368+
369+
if (!is_dir($webDir)) {
370+
mkdir($webDir);
371+
}
372+
373+
chdir($webDir);
374+
375+
$client->request('post', '/translate/_api/export');
376+
$path = vfsStream::url(self::STREAM . DIRECTORY_SEPARATOR . 'baz.en.yml');
377+
378+
$this->assertFileExists($path, 'Translation file should exist');
379+
$dumpedData = Yaml::parse(file_get_contents($path));
380+
381+
$this->assertEquals(['baz.key' => 'baz'], $dumpedData, 'Translations should be the same.');
382+
$document = $this
383+
->getManager('default', false)
384+
->getRepository('ONGRTranslationsBundle:Translation')
385+
->find(sha1('bazbaz.key'));
386+
387+
$this->assertEquals(Message::FRESH, $document->getMessages()[0]->getStatus(), 'Message should be refreshed');
388+
$this->assertEquals($currentDir, getcwd());
389+
390+
rmdir($webDir);
391+
}
339392
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ONGR package.
5+
*
6+
* (c) NFQ Technologies UAB <info@nfq.com>
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 ONGR\TranslationsBundle\Tests\Unit\DependencyInjection;
13+
14+
use ONGR\TranslationsBundle\DependencyInjection\ONGRTranslationsExtension;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
17+
/**
18+
* Class holds unit tests for ongr translations bundle extension.
19+
*/
20+
class ONGRTranslationsExtensionTest extends \PHPUnit_Framework_TestCase
21+
{
22+
/**
23+
* Tests if exception is thown when unknown bundle is provided.
24+
*
25+
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
26+
* @expectedExceptionMessage Invalid bundle namespace 'Acme/AcmeTestBundle'.
27+
*/
28+
public function testInvalidBundleName()
29+
{
30+
$config = [
31+
'ongr_translations' => [
32+
'repository' => 'es.manager.default.product',
33+
'bundles' => [
34+
'Acme/AcmeTestBundle',
35+
],
36+
],
37+
];
38+
39+
$container = new ContainerBuilder();
40+
$extension = new ONGRTranslationsExtension();
41+
42+
$extension->load($config, $container);
43+
}
44+
}

0 commit comments

Comments
 (0)