Completed
Push — 3.x ( 248ac6...5e36d7 )
by Grégoire
04:37
created

testGetShortObjectDescriptionActionEmptyObjectIdAsJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 1
nc 1
nop 0
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();
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...
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();
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...
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();
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...
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();
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...
125
        $this->admin->getObject(42)->willReturn($object);
126
        $this->admin->getTemplate('short_object_description')->willReturn('template');
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->admin->getTemplat...rt_object_description') (of type null|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...
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...
127
        $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...
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();
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...
145
        $this->admin->getObject(null)->willReturn(false);
146
        $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...
147
        $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...
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();
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...
167
        $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...
168
        $this->admin->getObject(42)->willReturn($object);
169
        $this->admin->getTemplate('short_object_description')->willReturn('template');
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->admin->getTemplat...rt_object_description') (of type null|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...
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...
170
        $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...
171
172
        $action = $this->action;
173
        $response = $action($request);
174
175
        $this->assertSame('{"result":{"id":42,"label":"bar"}}', $response->getContent());
176
    }
177
}
178