1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sonata project. |
5
|
|
|
* |
6
|
|
|
* (c) Thomas Rabaix <[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 Sonata\DoctrineORMAdminBundle\Tests\DependencyInjection\Compiler; |
13
|
|
|
|
14
|
|
|
use Sonata\DoctrineORMAdminBundle\DependencyInjection\Compiler\AddAuditEntityCompilerPass; |
15
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
16
|
|
|
|
17
|
|
|
class AddAuditEntityCompilerPassTest extends \PHPUnit_Framework_TestCase |
18
|
|
|
{ |
19
|
|
|
public function processDataProvider() |
20
|
|
|
{ |
21
|
|
|
return array( |
22
|
|
|
array(true, array( |
23
|
|
|
'admin1' => array('audit' => null, 'audited' => true), |
24
|
|
|
'admin2' => array('audit' => true, 'audited' => true), |
25
|
|
|
'admin3' => array('audit' => false, 'audited' => false), |
26
|
|
|
)), |
27
|
|
|
array(false, array( |
28
|
|
|
'admin1' => array('audit' => null, 'audited' => false), |
29
|
|
|
'admin2' => array('audit' => true, 'audited' => true), |
30
|
|
|
'admin3' => array('audit' => false, 'audited' => false), |
31
|
|
|
)), |
32
|
|
|
); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @dataProvider processDataProvider |
37
|
|
|
*/ |
38
|
|
|
public function testProcess($force, array $services) |
39
|
|
|
{ |
40
|
|
|
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder'); |
41
|
|
|
|
42
|
|
|
$container |
43
|
|
|
->expects($this->any()) |
44
|
|
|
->method('hasDefinition') |
45
|
|
|
->will($this->returnCallback(function ($id) { |
46
|
|
|
if ('simplethings_entityaudit.config' === $id) { |
47
|
|
|
return true; |
48
|
|
|
} |
49
|
|
|
})) |
50
|
|
|
; |
51
|
|
|
|
52
|
|
|
$container |
53
|
|
|
->expects($this->any()) |
54
|
|
|
->method('getParameter') |
55
|
|
|
->will($this->returnCallback(function ($id) use ($force) { |
56
|
|
|
if ('sonata_doctrine_orm_admin.audit.force' === $id) { |
57
|
|
|
return $force; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if ('simplethings.entityaudit.audited_entities' === $id) { |
61
|
|
|
return array(); |
62
|
|
|
} |
63
|
|
|
})) |
64
|
|
|
; |
65
|
|
|
|
66
|
|
|
$container |
67
|
|
|
->expects($this->any()) |
68
|
|
|
->method('findTaggedServiceIds') |
69
|
|
|
->will($this->returnCallback(function ($id) use ($services) { |
70
|
|
|
if ('sonata.admin' === $id) { |
71
|
|
|
$tags = array(); |
72
|
|
|
|
73
|
|
|
foreach ($services as $id => $service) { |
74
|
|
|
$attributes = array('manager_type' => 'orm'); |
75
|
|
|
|
76
|
|
|
if (null !== $audit = $service['audit']) { |
77
|
|
|
$attributes['audit'] = $audit; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$tags[$id] = array(0 => $attributes); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $tags; |
84
|
|
|
} |
85
|
|
|
})) |
86
|
|
|
; |
87
|
|
|
|
88
|
|
|
$container |
89
|
|
|
->expects($this->any()) |
90
|
|
|
->method('getDefinition') |
91
|
|
|
->will($this->returnCallback(function ($id) { |
92
|
|
|
return new Definition(null, array(null, $id)); |
93
|
|
|
})) |
94
|
|
|
; |
95
|
|
|
|
96
|
|
|
$expectedAuditedEntities = array(); |
97
|
|
|
|
98
|
|
|
foreach ($services as $id => $service) { |
99
|
|
|
if ($service['audited']) { |
100
|
|
|
$expectedAuditedEntities[] = $id; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$container |
105
|
|
|
->expects($this->once()) |
106
|
|
|
->method('setParameter') |
107
|
|
|
->with('simplethings.entityaudit.audited_entities', $expectedAuditedEntities) |
108
|
|
|
; |
109
|
|
|
|
110
|
|
|
$compilerPass = new AddAuditEntityCompilerPass(); |
111
|
|
|
$compilerPass->process($container); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|