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