|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Lug package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Lug\Bundle\TranslationBundle\Tests\DependencyInjection\Compiler; |
|
13
|
|
|
|
|
14
|
|
|
use Lug\Bundle\TranslationBundle\DependencyInjection\Compiler\ConfigureFactoryPass; |
|
15
|
|
|
use Lug\Component\Resource\Factory\Factory; |
|
16
|
|
|
use Lug\Component\Translation\Factory\TranslatableFactory; |
|
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
18
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
19
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @author GeLo <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class ConfigureFactoryPassTest extends \PHPUnit_Framework_TestCase |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var ConfigureFactoryPass |
|
28
|
|
|
*/ |
|
29
|
|
|
private $compiler; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* {@inheritdoc} |
|
33
|
|
|
*/ |
|
34
|
|
|
protected function setUp() |
|
35
|
|
|
{ |
|
36
|
|
|
$this->compiler = new ConfigureFactoryPass(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function testProcessWithTranslatable() |
|
40
|
|
|
{ |
|
41
|
|
|
$container = $this->createContainerBuilderMock(); |
|
42
|
|
|
$container |
|
43
|
|
|
->expects($this->once()) |
|
44
|
|
|
->method('findTaggedServiceIds') |
|
45
|
|
|
->with($this->identicalTo('lug.factory')) |
|
46
|
|
|
->will($this->returnValue([ |
|
47
|
|
|
$factoryService = 'lug.factory.resource_name' => [['resource' => $resourceName = 'resource_name']], |
|
48
|
|
|
])); |
|
49
|
|
|
|
|
50
|
|
|
$container |
|
51
|
|
|
->expects($this->exactly(3)) |
|
52
|
|
|
->method('getDefinition') |
|
53
|
|
|
->will($this->returnValueMap([ |
|
54
|
|
|
[$factoryService, $factory = $this->createDefinitionMock()], |
|
55
|
|
|
[$resourceService = 'lug.resource.resource_name', $resource = $this->createDefinitionMock()], |
|
56
|
|
|
[$translationService = 'lug.resource.translation_name', $translation = $this->createDefinitionMock()], |
|
57
|
|
|
])); |
|
58
|
|
|
|
|
59
|
|
|
$factory |
|
60
|
|
|
->expects($this->once()) |
|
61
|
|
|
->method('getClass') |
|
62
|
|
|
->will($this->returnValue(TranslatableFactory::class)); |
|
63
|
|
|
|
|
64
|
|
|
$resource |
|
65
|
|
|
->expects($this->once()) |
|
66
|
|
|
->method('getMethodCalls') |
|
67
|
|
|
->will($this->returnValue([['addRelation', ['translation', $translationService]]])); |
|
68
|
|
|
|
|
69
|
|
|
$translation |
|
70
|
|
|
->expects($this->once()) |
|
71
|
|
|
->method('getArgument') |
|
72
|
|
|
->with($this->identicalTo(0)) |
|
73
|
|
|
->will($this->returnValue($translationName = 'translation_name')); |
|
74
|
|
|
|
|
75
|
|
|
$factory |
|
76
|
|
|
->expects($this->at(1)) |
|
77
|
|
|
->method('addArgument') |
|
78
|
|
|
->with($this->callback(function ($argument) { |
|
79
|
|
|
return $argument instanceof Reference |
|
80
|
|
|
&& (string) $argument === 'lug.translation.context.locale'; |
|
81
|
|
|
})) |
|
82
|
|
|
->will($this->returnSelf()); |
|
83
|
|
|
|
|
84
|
|
|
$factory |
|
85
|
|
|
->expects($this->at(2)) |
|
86
|
|
|
->method('addArgument') |
|
87
|
|
|
->with($this->callback(function ($argument) use ($translationName) { |
|
88
|
|
|
return $argument instanceof Reference |
|
89
|
|
|
&& (string) $argument === 'lug.factory.'.$translationName; |
|
90
|
|
|
})) |
|
91
|
|
|
->will($this->returnSelf()); |
|
92
|
|
|
|
|
93
|
|
|
$this->compiler->process($container); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function testProcessWithoutTranslatable() |
|
97
|
|
|
{ |
|
98
|
|
|
$container = $this->createContainerBuilderMock(); |
|
99
|
|
|
$container |
|
100
|
|
|
->expects($this->once()) |
|
101
|
|
|
->method('findTaggedServiceIds') |
|
102
|
|
|
->with($this->identicalTo('lug.factory')) |
|
103
|
|
|
->will($this->returnValue([$service = 'service' => []])); |
|
104
|
|
|
|
|
105
|
|
|
$container |
|
106
|
|
|
->expects($this->once()) |
|
107
|
|
|
->method('getDefinition') |
|
108
|
|
|
->with($this->identicalTo($service)) |
|
109
|
|
|
->will($this->returnValue($factory = $this->createDefinitionMock())); |
|
110
|
|
|
|
|
111
|
|
|
$factory |
|
112
|
|
|
->expects($this->once()) |
|
113
|
|
|
->method('getClass') |
|
114
|
|
|
->will($this->returnValue(Factory::class)); |
|
115
|
|
|
|
|
116
|
|
|
$factory |
|
117
|
|
|
->expects($this->never()) |
|
118
|
|
|
->method('addArgument'); |
|
119
|
|
|
|
|
120
|
|
|
$this->compiler->process($container); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ContainerBuilder |
|
125
|
|
|
*/ |
|
126
|
|
|
private function createContainerBuilderMock() |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->createMock(ContainerBuilder::class); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|Definition |
|
133
|
|
|
*/ |
|
134
|
|
|
private function createDefinitionMock() |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->createMock(Definition::class); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|