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

testGetShortObjectDescriptionActionInvalidAdmin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
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 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);
0 ignored issues
show
Bug introduced by
The method willReturn() 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...
74
        $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...
Documentation introduced by
\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();
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...
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();
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...
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();
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...
127
        $this->admin->getObject(42)->willReturn($object);
128
        $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...
Bug introduced by
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...
129
        $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...
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
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...
147
        $this->admin->getObject(null)->willReturn(false);
148
        $this->admin->id(false)->willReturn('');
0 ignored issues
show
Documentation introduced by
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...
Bug introduced by
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
Bug introduced by
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
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...
169
        $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...
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...
Bug introduced by
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
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...
173
174
        $action = $this->action;
175
        $response = $action($request);
176
177
        $this->assertSame('{"result":{"id":42,"label":"bar"}}', $response->getContent());
178
    }
179
}
180