1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sonata Project package. |
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\AdminBundle\Tests\Twig; |
13
|
|
|
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
use Sonata\AdminBundle\Admin\AdminInterface; |
16
|
|
|
use Sonata\AdminBundle\Admin\Pool; |
17
|
|
|
use Sonata\AdminBundle\Twig\GlobalVariables; |
18
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
19
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Ahmet Akbana <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class GlobalVariablesTest extends TestCase |
25
|
|
|
{ |
26
|
|
|
private $code; |
27
|
|
|
private $action; |
28
|
|
|
private $admin; |
29
|
|
|
private $pool; |
30
|
|
|
|
31
|
|
|
public function setUp() |
32
|
|
|
{ |
33
|
|
|
$this->code = 'sonata.page.admin.page|sonata.page.admin.snapshot'; |
34
|
|
|
$this->action = 'list'; |
35
|
|
|
$this->admin = $this->getMockForAbstractClass(AdminInterface::class); |
36
|
|
|
$this->pool = $this->getMockBuilder(Pool::class)->disableOriginalConstructor()->getMock(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testUrl() |
40
|
|
|
{ |
41
|
|
|
$this->admin->expects($this->once()) |
42
|
|
|
->method('generateUrl') |
43
|
|
|
->with('sonata.page.admin.page|sonata.page.admin.snapshot.list', ['foo'], UrlGeneratorInterface::ABSOLUTE_PATH) |
44
|
|
|
->willReturn(true); |
45
|
|
|
|
46
|
|
|
$this->pool->expects($this->once()) |
47
|
|
|
->method('getAdminByAdminCode') |
48
|
|
|
->with('sonata.page.admin.page') |
49
|
|
|
->willReturn($this->admin); |
50
|
|
|
|
51
|
|
|
$globalVariables = new GlobalVariables($this->pool); |
52
|
|
|
|
53
|
|
|
$globalVariables->url($this->code, $this->action, ['foo']); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testObjectUrl() |
57
|
|
|
{ |
58
|
|
|
$this->admin->expects($this->once()) |
59
|
|
|
->method('generateObjectUrl') |
60
|
|
|
->with('sonata.page.admin.page|sonata.page.admin.snapshot.list', 'foo', ['bar'], UrlGeneratorInterface::ABSOLUTE_PATH) |
61
|
|
|
->willReturn(true); |
62
|
|
|
|
63
|
|
|
$this->pool->expects($this->once()) |
64
|
|
|
->method('getAdminByAdminCode') |
65
|
|
|
->with('sonata.page.admin.page') |
66
|
|
|
->willReturn($this->admin); |
67
|
|
|
|
68
|
|
|
$globalVariables = new GlobalVariables($this->pool); |
69
|
|
|
|
70
|
|
|
$globalVariables->objectUrl($this->code, $this->action, 'foo', ['bar']); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @group legacy |
75
|
|
|
* NEXT_MAJOR: remove this method |
76
|
|
|
*/ |
77
|
|
|
public function testWithContainer() |
78
|
|
|
{ |
79
|
|
|
$this->admin->expects($this->once()) |
80
|
|
|
->method('generateUrl') |
81
|
|
|
->with('sonata.page.admin.page|sonata.page.admin.snapshot.list', ['foo'], UrlGeneratorInterface::ABSOLUTE_PATH) |
82
|
|
|
->willReturn(true); |
83
|
|
|
|
84
|
|
|
$this->pool->expects($this->once()) |
85
|
|
|
->method('getAdminByAdminCode') |
86
|
|
|
->with('sonata.page.admin.page') |
87
|
|
|
->willReturn($this->admin); |
88
|
|
|
|
89
|
|
|
$container = $this->getMockForAbstractClass(ContainerInterface::class); |
90
|
|
|
$container->expects($this->once()) |
91
|
|
|
->method('get') |
92
|
|
|
->with('sonata.admin.pool') |
93
|
|
|
->willReturn($this->pool); |
94
|
|
|
|
95
|
|
|
$globalVariables = new GlobalVariables($container); |
96
|
|
|
|
97
|
|
|
$globalVariables->url($this->code, $this->action, ['foo']); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* NEXT_MAJOR: remove this method. |
102
|
|
|
*/ |
103
|
|
|
public function testInvalidArgumentException() |
104
|
|
|
{ |
105
|
|
|
$this->expectException( |
106
|
|
|
\InvalidArgumentException::class, |
107
|
|
|
'$adminPool should be an instance of Sonata\AdminBundle\Admin\Pool' |
108
|
|
|
); |
109
|
|
|
|
110
|
|
|
new GlobalVariables('foo'); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @dataProvider getMosaicBackgroundProvider |
115
|
|
|
*/ |
116
|
|
|
public function testGetMosaicBackground($expected, $image) |
117
|
|
|
{ |
118
|
|
|
$globalVariables = new GlobalVariables($this->pool, $image); |
119
|
|
|
$this->assertEquals($expected, $globalVariables->getMosaicBackground()); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function getMosaicBackgroundProvider() |
123
|
|
|
{ |
124
|
|
|
return [ |
125
|
|
|
'background is null' => [null, null], |
126
|
|
|
'background is set' => ['image.png', 'image.png'], |
127
|
|
|
]; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: