Completed
Push — 3.x ( e95e95...638cd1 )
by Oskar
05:54
created

Action/GetShortObjectDescriptionActionTest.php (9 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);
54
        $this->admin = $this->prophesize(AbstractAdmin::class);
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();
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();
0 ignored issues
show
The method shouldBeCalled cannot be called on $this->admin->setUniqid('asdasd123') (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
147
        $this->admin->getObject(null)->willReturn(false);
148
        $this->admin->id(false)->willReturn('');
0 ignored issues
show
false is of type boolean, but the function expects a object.

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...
The method willReturn cannot be called on $this->admin->id(false) (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
149
        $this->admin->toString(false)->willReturn('');
0 ignored issues
show
The method willReturn cannot be called on $this->admin->toString(false) (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
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();
0 ignored issues
show
The method shouldBeCalled cannot be called on $this->admin->setUniqid('asdasd123') (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
169
        $this->admin->id($object)->willReturn(42);
0 ignored issues
show
The method willReturn cannot be called on $this->admin->id($object) (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
170
        $this->admin->getObject(42)->willReturn($object);
171
        $this->admin->getTemplate('short_object_description')->willReturn('template');
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...actAdmin::getTemplate() has been deprecated with message: since 3.34, will be dropped in 4.0. Use TemplateRegistry services instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
The method willReturn cannot be called on $this->admin->getTemplat...rt_object_description') (of type string|null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
172
        $this->admin->toString($object)->willReturn('bar');
0 ignored issues
show
The method willReturn cannot be called on $this->admin->toString($object) (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
173
174
        $action = $this->action;
175
        $response = $action($request);
176
177
        $this->assertSame('{"result":{"id":42,"label":"bar"}}', $response->getContent());
178
    }
179
}
180