|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Sonata Project package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Sonata\AdminBundle\Tests\Twig; |
|
15
|
|
|
|
|
16
|
|
|
use PHPUnit\Framework\TestCase; |
|
17
|
|
|
use Sonata\AdminBundle\Admin\AdminInterface; |
|
18
|
|
|
use Sonata\AdminBundle\Admin\Pool; |
|
19
|
|
|
use Sonata\AdminBundle\Twig\GlobalVariables; |
|
20
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
21
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @author Ahmet Akbana <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class GlobalVariablesTest extends TestCase |
|
27
|
|
|
{ |
|
28
|
|
|
private $code; |
|
29
|
|
|
private $action; |
|
30
|
|
|
private $admin; |
|
31
|
|
|
private $pool; |
|
32
|
|
|
|
|
33
|
|
|
public function setUp(): void |
|
34
|
|
|
{ |
|
35
|
|
|
$this->code = 'sonata.page.admin.page|sonata.page.admin.snapshot'; |
|
36
|
|
|
$this->action = 'list'; |
|
37
|
|
|
$this->admin = $this->getMockForAbstractClass(AdminInterface::class); |
|
38
|
|
|
$this->pool = $this->getMockBuilder(Pool::class)->disableOriginalConstructor()->getMock(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function testUrl(): void |
|
42
|
|
|
{ |
|
43
|
|
|
$this->admin->expects($this->once()) |
|
44
|
|
|
->method('generateUrl') |
|
45
|
|
|
->with('sonata.page.admin.page|sonata.page.admin.snapshot.list', ['foo'], UrlGeneratorInterface::ABSOLUTE_PATH) |
|
46
|
|
|
->willReturn(true); |
|
47
|
|
|
|
|
48
|
|
|
$this->pool->expects($this->once()) |
|
49
|
|
|
->method('getAdminByAdminCode') |
|
50
|
|
|
->with('sonata.page.admin.page') |
|
51
|
|
|
->willReturn($this->admin); |
|
52
|
|
|
|
|
53
|
|
|
$globalVariables = new GlobalVariables($this->pool); |
|
54
|
|
|
|
|
55
|
|
|
$globalVariables->url($this->code, $this->action, ['foo']); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function testObjectUrl(): void |
|
59
|
|
|
{ |
|
60
|
|
|
$this->admin->expects($this->once()) |
|
61
|
|
|
->method('generateObjectUrl') |
|
62
|
|
|
->with('sonata.page.admin.page|sonata.page.admin.snapshot.list', 'foo', ['bar'], UrlGeneratorInterface::ABSOLUTE_PATH) |
|
63
|
|
|
->willReturn(true); |
|
64
|
|
|
|
|
65
|
|
|
$this->pool->expects($this->once()) |
|
66
|
|
|
->method('getAdminByAdminCode') |
|
67
|
|
|
->with('sonata.page.admin.page') |
|
68
|
|
|
->willReturn($this->admin); |
|
69
|
|
|
|
|
70
|
|
|
$globalVariables = new GlobalVariables($this->pool); |
|
71
|
|
|
|
|
72
|
|
|
$globalVariables->objectUrl($this->code, $this->action, 'foo', ['bar']); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @group legacy |
|
77
|
|
|
* NEXT_MAJOR: remove this method |
|
78
|
|
|
*/ |
|
79
|
|
|
public function testWithContainer(): void |
|
80
|
|
|
{ |
|
81
|
|
|
$this->admin->expects($this->once()) |
|
82
|
|
|
->method('generateUrl') |
|
83
|
|
|
->with('sonata.page.admin.page|sonata.page.admin.snapshot.list', ['foo'], UrlGeneratorInterface::ABSOLUTE_PATH) |
|
84
|
|
|
->willReturn(true); |
|
85
|
|
|
|
|
86
|
|
|
$this->pool->expects($this->once()) |
|
87
|
|
|
->method('getAdminByAdminCode') |
|
88
|
|
|
->with('sonata.page.admin.page') |
|
89
|
|
|
->willReturn($this->admin); |
|
90
|
|
|
|
|
91
|
|
|
$container = $this->getMockForAbstractClass(ContainerInterface::class); |
|
92
|
|
|
$container->expects($this->once()) |
|
93
|
|
|
->method('get') |
|
94
|
|
|
->with('sonata.admin.pool') |
|
95
|
|
|
->willReturn($this->pool); |
|
96
|
|
|
|
|
97
|
|
|
$globalVariables = new GlobalVariables($container); |
|
98
|
|
|
|
|
99
|
|
|
$globalVariables->url($this->code, $this->action, ['foo']); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* NEXT_MAJOR: remove this method. |
|
104
|
|
|
*/ |
|
105
|
|
|
public function testInvalidArgumentException(): void |
|
106
|
|
|
{ |
|
107
|
|
|
$this->expectException( |
|
108
|
|
|
\InvalidArgumentException::class, |
|
109
|
|
|
'$adminPool should be an instance of Sonata\AdminBundle\Admin\Pool' |
|
110
|
|
|
); |
|
111
|
|
|
|
|
112
|
|
|
new GlobalVariables('foo'); |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function testGetMosaicBackground(): void |
|
116
|
|
|
{ |
|
117
|
|
|
$this->assertSame( |
|
118
|
|
|
'image.png', |
|
119
|
|
|
(new GlobalVariables($this->pool, 'image.png'))->getMosaicBackground() |
|
120
|
|
|
); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function testGetMosaicBackgroundNull(): void |
|
124
|
|
|
{ |
|
125
|
|
|
$this->assertNull((new GlobalVariables($this->pool))->getMosaicBackground()); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
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: