1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Travel With Me |
5
|
|
|
* |
6
|
|
|
* @copyright (c) Antoine Froger <[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 Af\Bundle\DontTranslateBundle\Tests\Translation; |
13
|
|
|
|
14
|
|
|
use Af\Bundle\DontTranslateBundle\Translation\Translator; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class TranslatorTest |
18
|
|
|
* |
19
|
|
|
* @package Af\Bundle\DontTranslateBundle\Tests\Translation |
20
|
|
|
* |
21
|
|
|
* @author Antoine Froger <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class TranslatorTest extends \PHPUnit_Framework_TestCase |
24
|
|
|
{ |
25
|
|
|
public function testTranslate() |
26
|
|
|
{ |
27
|
|
|
// debug = false, messages are translated |
28
|
|
|
$translator = $this->getTranslator(false); |
29
|
|
|
|
30
|
|
|
$this->assertEquals('foo translation', $translator->trans('foo')); |
31
|
|
|
$this->assertEquals('bar translation', $translator->trans('bar')); |
32
|
|
|
$this->assertEquals('baz translation', $translator->trans('baz')); |
33
|
|
|
$this->assertEquals('foo 2', $translator->transChoice('foo', 2)); |
34
|
|
|
$this->assertEquals('bar single', $translator->transChoice('bar', 0)); |
35
|
|
|
$this->assertEquals('bar plural', $translator->transChoice('bar', 1)); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testDontTranslate() |
39
|
|
|
{ |
40
|
|
|
// debug = true, messages aren't translated |
41
|
|
|
$translator = $this->getTranslator(true); |
42
|
|
|
|
43
|
|
|
$this->assertEquals('foo', $translator->trans('foo')); |
44
|
|
|
$this->assertEquals('bar', $translator->trans('bar')); |
45
|
|
|
$this->assertEquals('baz', $translator->trans('baz')); |
46
|
|
|
$this->assertEquals('foo', $translator->transChoice('foo', 2)); |
47
|
|
|
$this->assertEquals('bar', $translator->transChoice('bar', 0)); |
48
|
|
|
$this->assertEquals('bar', $translator->transChoice('bar', 1)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private function getTranslator($debug) |
52
|
|
|
{ |
53
|
|
|
$debug = (bool)$debug; |
54
|
|
|
|
55
|
|
|
$translator = $this->getMock('Symfony\Component\Translation\TranslatorInterface'); |
56
|
|
|
$translator |
57
|
|
|
->expects($debug ? $this->never() : $this->exactly(3)) |
58
|
|
|
->method('trans') |
59
|
|
|
->will($this->returnCallback(function ($key) { |
60
|
|
|
$values = array( |
61
|
|
|
'foo' => 'foo translation', |
62
|
|
|
'bar' => 'bar translation', |
63
|
|
|
'baz' => 'baz translation', |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
return isset($values[$key]) ? $values[$key] : null; |
67
|
|
|
})) |
68
|
|
|
; |
69
|
|
|
$translator |
70
|
|
|
->expects($debug ? $this->never() : $this->exactly(3)) |
71
|
|
|
->method('transChoice') |
72
|
|
|
->will($this->returnCallback(function ($key, $count) { |
73
|
|
|
$values = array( |
74
|
|
|
'foo' => 'foo 0|foo 1|foo 2', |
75
|
|
|
'bar' => 'bar single|bar plural', |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
if (!isset($values[$key])) { |
79
|
|
|
return null; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$results = explode('|', $values[$key]); |
83
|
|
|
|
84
|
|
|
return isset($results[$count]) ? $results[$count] : null; |
85
|
|
|
})) |
86
|
|
|
; |
87
|
|
|
|
88
|
|
|
$translator = new Translator($translator); |
89
|
|
|
$translator->setDebug($debug); |
90
|
|
|
|
91
|
|
|
return $translator; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|