Completed
Push — 3.x ( b75183...b1c847 )
by Oskar
04:45
created

Action/GetShortObjectDescriptionActionTest.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 Symfony\Component\HttpFoundation\JsonResponse;
22
use Symfony\Component\HttpFoundation\Request;
23
use Symfony\Component\HttpFoundation\Response;
24
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
25
use Twig\Environment;
26
use Twig\Loader\ArrayLoader;
27
28
final class GetShortObjectDescriptionActionTest extends TestCase
29
{
30
    /**
31
     * @var Pool
32
     */
33
    private $pool;
34
35
    /**
36
     * @var Environment
37
     */
38
    private $twig;
39
40
    /**
41
     * @var GetShortObjectDescriptionAction
42
     */
43
    private $action;
44
45
    /**
46
     * @var AbstractAdmin
47
     */
48
    private $admin;
49
50
    protected function setUp(): void
51
    {
52
        $this->twig = new Environment(new ArrayLoader(['template' => 'renderedTemplate']));
53
        $this->pool = $this->prophesize(Pool::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->prophesize(\Sonat...ndle\Admin\Pool::class) of type object<Prophecy\Prophecy\ObjectProphecy> is incompatible with the declared type object<Sonata\AdminBundle\Admin\Pool> of property $pool.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
54
        $this->admin = $this->prophesize(AbstractAdmin::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->prophesize(\Sonat...n\AbstractAdmin::class) of type object<Prophecy\Prophecy\ObjectProphecy> is incompatible with the declared type object<Sonata\AdminBundle\Admin\AbstractAdmin> of property $admin.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
55
        $this->pool->getInstance(Argument::any())->willReturn($this->admin->reveal());
56
        $this->admin->setRequest(Argument::type(Request::class))->shouldBeCalled();
57
        $this->action = new GetShortObjectDescriptionAction(
58
            $this->twig,
59
            $this->pool->reveal()
60
        );
61
    }
62
63
    public function testGetShortObjectDescriptionActionInvalidAdmin(): void
64
    {
65
        $this->expectException(NotFoundHttpException::class);
66
67
        $request = new Request([
68
            'code' => 'sonata.post.admin',
69
            'objectId' => 42,
70
            'uniqid' => 'asdasd123',
71
        ]);
72
73
        $this->pool->getInstance('sonata.post.admin')->willReturn(null);
74
        $this->admin->setRequest(Argument::type(Request::class))->shouldNotBeCalled();
0 ignored issues
show
\Prophecy\Argument::type...ndation\Request::class) is of type object<Prophecy\Argument\Token\TypeToken>, but the function expects a object<Symfony\Component\HttpFoundation\Request>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
75
76
        $action = $this->action;
77
        $action($request);
78
    }
79
80
    public function testGetShortObjectDescriptionActionObjectDoesNotExist(): void
81
    {
82
        $this->expectException(\RuntimeException::class);
83
        $this->expectExceptionMessage('Invalid format');
84
85
        $request = new Request([
86
            'code' => 'sonata.post.admin',
87
            'objectId' => 42,
88
            'uniqid' => 'asdasd123',
89
        ]);
90
91
        $this->admin->setUniqid('asdasd123')->shouldBeCalled();
92
        $this->admin->getObject(42)->willReturn(false);
93
94
        $action = $this->action;
95
        $action($request);
96
    }
97
98
    public function testGetShortObjectDescriptionActionEmptyObjectId(): void
99
    {
100
        $request = new Request([
101
            'code' => 'sonata.post.admin',
102
            'objectId' => '',
103
            'uniqid' => 'asdasd123',
104
            '_format' => 'html',
105
        ]);
106
107
        $this->admin->setUniqid('asdasd123')->shouldBeCalled();
108
        $this->admin->getObject(null)->willReturn(false);
109
110
        $action = $this->action;
111
        $response = $action($request);
112
113
        $this->assertInstanceOf(Response::class, $response);
114
    }
115
116
    public function testGetShortObjectDescriptionActionObject(): void
117
    {
118
        $request = new Request([
119
            'code' => 'sonata.post.admin',
120
            'objectId' => 42,
121
            'uniqid' => 'asdasd123',
122
            '_format' => 'html',
123
        ]);
124
        $object = new \stdClass();
125
126
        $this->admin->setUniqid('asdasd123')->shouldBeCalled();
127
        $this->admin->getObject(42)->willReturn($object);
128
        $this->admin->getTemplate('short_object_description')->willReturn('template');
129
        $this->admin->toString($object)->willReturn('bar');
130
131
        $action = $this->action;
132
        $response = $action($request);
133
134
        $this->assertSame('renderedTemplate', $response->getContent());
135
    }
136
137
    public function testGetShortObjectDescriptionActionEmptyObjectIdAsJson(): void
138
    {
139
        $request = new Request([
140
            'code' => 'sonata.post.admin',
141
            'objectId' => '',
142
            'uniqid' => 'asdasd123',
143
            '_format' => 'json',
144
        ]);
145
146
        $this->admin->setUniqid('asdasd123')->shouldBeCalled();
147
        $this->admin->getObject(null)->willReturn(false);
148
        $this->admin->id(false)->willReturn('');
149
        $this->admin->toString(false)->willReturn('');
150
151
        $action = $this->action;
152
        $response = $action($request);
153
154
        $this->assertInstanceOf(JsonResponse::class, $response);
155
        $this->assertSame('{"result":{"id":"","label":""}}', $response->getContent());
156
    }
157
158
    public function testGetShortObjectDescriptionActionObjectAsJson(): void
159
    {
160
        $request = new Request([
161
            'code' => 'sonata.post.admin',
162
            'objectId' => 42,
163
            'uniqid' => 'asdasd123',
164
            '_format' => 'json',
165
        ]);
166
        $object = new \stdClass();
167
168
        $this->admin->setUniqid('asdasd123')->shouldBeCalled();
169
        $this->admin->id($object)->willReturn(42);
170
        $this->admin->getObject(42)->willReturn($object);
171
        $this->admin->getTemplate('short_object_description')->willReturn('template');
172
        $this->admin->toString($object)->willReturn('bar');
173
174
        $action = $this->action;
175
        $response = $action($request);
176
177
        $this->assertSame('{"result":{"id":42,"label":"bar"}}', $response->getContent());
178
    }
179
}
180