1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ElevenLabs\ApiServiceBundle\Tests\DependencyInjection\Compiler; |
4
|
|
|
|
5
|
|
|
use ElevenLabs\ApiServiceBundle\DependencyInjection\Compiler\PaginatorCompilerPass; |
6
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
9
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class PaginatorCompilerPassTest. |
13
|
|
|
*/ |
14
|
|
|
class PaginatorCompilerPassTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var ContainerBuilder|MockObject |
18
|
|
|
*/ |
19
|
|
|
private $containerBuilder; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Definition|MockObject |
23
|
|
|
*/ |
24
|
|
|
private $pagination; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Definition|MockObject |
28
|
|
|
*/ |
29
|
|
|
private $provider; |
30
|
|
|
|
31
|
|
|
public function setUp() |
32
|
|
|
{ |
33
|
|
|
$this->pagination = $this->createMock(Definition::class); |
34
|
|
|
$this->provider = $this->createMock(Definition::class); |
35
|
|
|
$this->containerBuilder = $this->createMock(ContainerBuilder::class); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @dataProvider dataProviderTestShouldNotAddPagination |
40
|
|
|
* |
41
|
|
|
* @param array $config |
42
|
|
|
* |
43
|
|
|
* @throws \Exception |
44
|
|
|
*/ |
45
|
|
|
public function testShouldNotAddPagination(array $config) |
46
|
|
|
{ |
47
|
|
|
$this->containerBuilder |
48
|
|
|
->expects($this->once()) |
49
|
|
|
->method('getExtensionConfig') |
50
|
|
|
->with('api_service') |
51
|
|
|
->willReturn([$config]) |
52
|
|
|
; |
53
|
|
|
$this->containerBuilder |
54
|
|
|
->expects($this->once()) |
55
|
|
|
->method('findTaggedServiceIds') |
56
|
|
|
->with('api_service.pagination_provider') |
57
|
|
|
->willReturn([]) |
58
|
|
|
; |
59
|
|
|
$this->containerBuilder |
60
|
|
|
->expects($this->once()) |
61
|
|
|
->method('getDefinition') |
62
|
|
|
->with('api_service.pagination_provider.chain') |
63
|
|
|
->willReturn($this->pagination) |
64
|
|
|
; |
65
|
|
|
|
66
|
|
|
$compiler = new PaginatorCompilerPass(); |
67
|
|
|
$compiler->process($this->containerBuilder); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function dataProviderTestShouldNotAddPagination(): array |
71
|
|
|
{ |
72
|
|
|
return [ |
73
|
|
|
[ |
74
|
|
|
[], |
75
|
|
|
], |
76
|
|
|
[ |
77
|
|
|
['pagination' => []], |
78
|
|
|
], |
79
|
|
|
[ |
80
|
|
|
[ |
81
|
|
|
'pagination' => [ |
82
|
|
|
'hal' => [ |
83
|
|
|
'page' => '_links.self.href.page', |
84
|
|
|
'perPage' => 'itemsPerPage', |
85
|
|
|
'totalPages' => '_links.last.href.page', |
86
|
|
|
'totalItems' => 'totalItems', |
87
|
|
|
], |
88
|
|
|
], |
89
|
|
|
], |
90
|
|
|
], |
91
|
|
|
]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testShouldAddPagination() |
95
|
|
|
{ |
96
|
|
|
$this->containerBuilder |
97
|
|
|
->expects($this->once()) |
98
|
|
|
->method('getExtensionConfig') |
99
|
|
|
->with('api_service') |
100
|
|
|
->willReturn([ |
101
|
|
|
[ |
102
|
|
|
'pagination' => [ |
103
|
|
|
'hal' => [ |
104
|
|
|
'page' => '_links.self.href.page', |
105
|
|
|
'perPage' => 'itemsPerPage', |
106
|
|
|
'totalPages' => '_links.last.href.page', |
107
|
|
|
'totalItems' => 'totalItems', |
108
|
|
|
], |
109
|
|
|
], |
110
|
|
|
] |
111
|
|
|
]) |
112
|
|
|
; |
113
|
|
|
$this->containerBuilder |
114
|
|
|
->expects($this->once()) |
115
|
|
|
->method('findTaggedServiceIds') |
116
|
|
|
->with('api_service.pagination_provider') |
117
|
|
|
->willReturn([ |
118
|
|
|
'foo' => [['provider' => 'hal']] |
119
|
|
|
]) |
120
|
|
|
; |
121
|
|
|
$this->containerBuilder |
122
|
|
|
->expects($this->exactly(2)) |
123
|
|
|
->method('getDefinition') |
124
|
|
|
->willReturnCallback(function ($id) { |
125
|
|
|
$this->assertTrue(\in_array($id, ['api_service.pagination_provider.chain', 'foo'])); |
126
|
|
|
|
127
|
|
|
if ('api_service.pagination_provider.chain' === $id) { |
128
|
|
|
$this->pagination->expects($this->once())->method('replaceArgument')->willReturnCallback(function ($key, $argument) { |
129
|
|
|
$this->assertSame(0, $key); |
130
|
|
|
$this->assertIsArray($argument); |
131
|
|
|
$this->assertNotEmpty($argument); |
132
|
|
|
}); |
133
|
|
|
} else if ('foo' === $id) { |
134
|
|
|
$this->pagination->expects($this->exactly(2))->method('replaceArgument')->willReturnCallback(function ($key, $argument) { |
135
|
|
|
$this->assertSame(0, $key); |
136
|
|
|
$this->assertIsArray($argument); |
137
|
|
|
$this->assertNotEmpty($argument); |
138
|
|
|
}); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $this->pagination; |
142
|
|
|
}) |
143
|
|
|
; |
144
|
|
|
|
145
|
|
|
$compiler = new PaginatorCompilerPass(); |
146
|
|
|
$compiler->process($this->containerBuilder); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|