testGetShortObjectDescriptionActionObject()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 9.504
c 0
b 0
f 0
cc 1
nc 1
nop 0
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);
0 ignored issues
show
Bug introduced by
The method willThrow() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
76
        $this->admin->setRequest(Argument::type(Request::class))->shouldNotBeCalled();
0 ignored issues
show
Bug introduced by
The method shouldNotBeCalled cannot be called on $this->admin->setRequest...dation\Request::class)) (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...
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();
0 ignored issues
show
Bug introduced by
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...
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();
0 ignored issues
show
Bug introduced by
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...
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);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Symfony\Component...ion\ContainerInterface>.

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.

Loading history...
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();
0 ignored issues
show
Bug introduced by
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...
133
        $this->admin->getObject(42)->willReturn($object);
134
        $this->admin->toString($object)->willReturn('bar');
0 ignored issues
show
Bug introduced by
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...
135
        $this->admin->getCode()->willReturn('sonata.post.admin');
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->admin->getCode() (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...
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();
0 ignored issues
show
Bug introduced by
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...
152
        $this->admin->getObject(null)->willReturn(null);
153
        $this->admin->id(null)->willReturn('');
0 ignored issues
show
Documentation introduced by
null is of type null, 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...
Bug introduced by
The method willReturn cannot be called on $this->admin->id(null) (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...
154
        $this->admin->toString(null)->willReturn('');
0 ignored issues
show
Documentation introduced by
null is of type null, 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...
Bug introduced by
The method willReturn cannot be called on $this->admin->toString(null) (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...
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();
0 ignored issues
show
Bug introduced by
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...
173
        $this->admin->id($object)->willReturn(42);
0 ignored issues
show
Bug introduced by
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...
174
        $this->admin->getObject(42)->willReturn($object);
175
        $this->admin->toString($object)->willReturn('bar');
0 ignored issues
show
Bug introduced by
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...
176
177
        $response = ($this->action)($request);
178
179
        $this->assertSame('{"result":{"id":"42","label":"bar"}}', $response->getContent());
180
    }
181
}
182