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\Action; |
15
|
|
|
|
16
|
|
|
use PHPUnit\Framework\TestCase; |
17
|
|
|
use Prophecy\Argument; |
18
|
|
|
use Sonata\AdminBundle\Action\GetShortObjectDescriptionAction; |
19
|
|
|
use Sonata\AdminBundle\Admin\AbstractAdmin; |
20
|
|
|
use Sonata\AdminBundle\Admin\Pool; |
21
|
|
|
use Sonata\AdminBundle\Templating\TemplateRegistry; |
22
|
|
|
use Symfony\Component\DependencyInjection\Container; |
23
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
24
|
|
|
use Symfony\Component\HttpFoundation\Request; |
25
|
|
|
use Symfony\Component\HttpFoundation\Response; |
26
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
27
|
|
|
use Twig\Environment; |
28
|
|
|
use Twig\Loader\ArrayLoader; |
29
|
|
|
|
30
|
|
|
final class GetShortObjectDescriptionActionTest extends TestCase |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var Pool |
34
|
|
|
*/ |
35
|
|
|
private $pool; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Environment |
39
|
|
|
*/ |
40
|
|
|
private $twig; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var GetShortObjectDescriptionAction |
44
|
|
|
*/ |
45
|
|
|
private $action; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var AbstractAdmin |
49
|
|
|
*/ |
50
|
|
|
private $admin; |
51
|
|
|
|
52
|
|
|
protected function setUp(): void |
53
|
|
|
{ |
54
|
|
|
$this->twig = new Environment(new ArrayLoader(['short_object_description' => 'renderedTemplate'])); |
55
|
|
|
$this->pool = $this->prophesize(Pool::class); |
56
|
|
|
$this->admin = $this->prophesize(AbstractAdmin::class); |
57
|
|
|
$this->pool->getInstance(Argument::any())->willReturn($this->admin->reveal()); |
58
|
|
|
$this->admin->setRequest(Argument::type(Request::class))->shouldBeCalled(); |
59
|
|
|
$this->action = new GetShortObjectDescriptionAction( |
60
|
|
|
$this->twig, |
61
|
|
|
$this->pool->reveal() |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testGetShortObjectDescriptionActionInvalidAdmin(): void |
66
|
|
|
{ |
67
|
|
|
$code = 'sonata.post.admin'; |
68
|
|
|
|
69
|
|
|
$request = new Request([ |
70
|
|
|
'code' => $code, |
71
|
|
|
'objectId' => 42, |
72
|
|
|
'uniqid' => 'asdasd123', |
73
|
|
|
]); |
74
|
|
|
|
75
|
|
|
$this->pool->getInstance($code)->willThrow(\InvalidArgumentException::class); |
|
|
|
|
76
|
|
|
$this->admin->setRequest(Argument::type(Request::class))->shouldNotBeCalled(); |
|
|
|
|
77
|
|
|
|
78
|
|
|
$this->expectException(NotFoundHttpException::class); |
79
|
|
|
|
80
|
|
|
($this->action)($request); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testGetShortObjectDescriptionActionObjectDoesNotExist(): void |
84
|
|
|
{ |
85
|
|
|
$this->expectException(\RuntimeException::class); |
86
|
|
|
$this->expectExceptionMessage('Invalid format'); |
87
|
|
|
|
88
|
|
|
$request = new Request([ |
89
|
|
|
'code' => 'sonata.post.admin', |
90
|
|
|
'objectId' => 42, |
91
|
|
|
'uniqid' => 'asdasd123', |
92
|
|
|
]); |
93
|
|
|
|
94
|
|
|
$this->admin->setUniqid('asdasd123')->shouldBeCalled(); |
|
|
|
|
95
|
|
|
$this->admin->getObject(42)->willReturn(null); |
96
|
|
|
|
97
|
|
|
($this->action)($request); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testGetShortObjectDescriptionActionEmptyObjectId(): void |
101
|
|
|
{ |
102
|
|
|
$request = new Request([ |
103
|
|
|
'code' => 'sonata.post.admin', |
104
|
|
|
'objectId' => '', |
105
|
|
|
'uniqid' => 'asdasd123', |
106
|
|
|
'_format' => 'html', |
107
|
|
|
]); |
108
|
|
|
|
109
|
|
|
$this->admin->setUniqid('asdasd123')->shouldBeCalled(); |
|
|
|
|
110
|
|
|
$this->admin->getObject(null)->willReturn(null); |
111
|
|
|
|
112
|
|
|
$this->assertInstanceOf(Response::class, ($this->action)($request)); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function testGetShortObjectDescriptionActionObject(): void |
116
|
|
|
{ |
117
|
|
|
$templateRegistry = new TemplateRegistry([ |
118
|
|
|
'short_object_description' => 'short_object_description', |
119
|
|
|
]); |
120
|
|
|
$container = new Container(); |
121
|
|
|
$container->set('sonata.post.admin.template_registry', $templateRegistry); |
122
|
|
|
$this->pool->getContainer()->willReturn($container); |
|
|
|
|
123
|
|
|
|
124
|
|
|
$request = new Request([ |
125
|
|
|
'code' => 'sonata.post.admin', |
126
|
|
|
'objectId' => 42, |
127
|
|
|
'uniqid' => 'asdasd123', |
128
|
|
|
'_format' => 'html', |
129
|
|
|
]); |
130
|
|
|
$object = new \stdClass(); |
131
|
|
|
|
132
|
|
|
$this->admin->setUniqid('asdasd123')->shouldBeCalled(); |
|
|
|
|
133
|
|
|
$this->admin->getObject(42)->willReturn($object); |
134
|
|
|
$this->admin->toString($object)->willReturn('bar'); |
|
|
|
|
135
|
|
|
$this->admin->getCode()->willReturn('sonata.post.admin'); |
|
|
|
|
136
|
|
|
|
137
|
|
|
$response = ($this->action)($request); |
138
|
|
|
|
139
|
|
|
$this->assertSame('renderedTemplate', $response->getContent()); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function testGetShortObjectDescriptionActionEmptyObjectIdAsJson(): void |
143
|
|
|
{ |
144
|
|
|
$request = new Request([ |
145
|
|
|
'code' => 'sonata.post.admin', |
146
|
|
|
'objectId' => '', |
147
|
|
|
'uniqid' => 'asdasd123', |
148
|
|
|
'_format' => 'json', |
149
|
|
|
]); |
150
|
|
|
|
151
|
|
|
$this->admin->setUniqid('asdasd123')->shouldBeCalled(); |
|
|
|
|
152
|
|
|
$this->admin->getObject(null)->willReturn(null); |
153
|
|
|
$this->admin->id(null)->willReturn(''); |
|
|
|
|
154
|
|
|
$this->admin->toString(null)->willReturn(''); |
|
|
|
|
155
|
|
|
|
156
|
|
|
$response = ($this->action)($request); |
157
|
|
|
|
158
|
|
|
$this->assertInstanceOf(JsonResponse::class, $response); |
159
|
|
|
$this->assertSame('{"result":{"id":"","label":""}}', $response->getContent()); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function testGetShortObjectDescriptionActionObjectAsJson(): void |
163
|
|
|
{ |
164
|
|
|
$request = new Request([ |
165
|
|
|
'code' => 'sonata.post.admin', |
166
|
|
|
'objectId' => 42, |
167
|
|
|
'uniqid' => 'asdasd123', |
168
|
|
|
'_format' => 'json', |
169
|
|
|
]); |
170
|
|
|
$object = new \stdClass(); |
171
|
|
|
|
172
|
|
|
$this->admin->setUniqid('asdasd123')->shouldBeCalled(); |
|
|
|
|
173
|
|
|
$this->admin->id($object)->willReturn(42); |
|
|
|
|
174
|
|
|
$this->admin->getObject(42)->willReturn($object); |
175
|
|
|
$this->admin->toString($object)->willReturn('bar'); |
|
|
|
|
176
|
|
|
|
177
|
|
|
$response = ($this->action)($request); |
178
|
|
|
|
179
|
|
|
$this->assertSame('{"result":{"id":"42","label":"bar"}}', $response->getContent()); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.