Completed
Push — master ( 2eb0ca...af375c )
by Marko
14s
created

GetShortObjectDescriptionActionTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 0
loc 109
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 12 1
A testGetShortObjectDescriptionActionInvalidAdmin() 0 16 1
A testGetShortObjectDescriptionActionObjectDoesNotExist() 0 17 1
A testGetShortObjectDescriptionActionEmptyObjectId() 0 17 1
A testGetShortObjectDescriptionActionObject() 0 20 1
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\Request;
22
use Symfony\Component\HttpFoundation\Response;
23
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
24
use Twig\Environment;
25
use Twig\Loader\ArrayLoader;
26
27
final class GetShortObjectDescriptionActionTest extends TestCase
28
{
29
    /**
30
     * @var Pool
31
     */
32
    private $pool;
33
34
    /**
35
     * @var Environment
36
     */
37
    private $twig;
38
39
    /**
40
     * @var GetShortObjectDescriptionAction
41
     */
42
    private $action;
43
44
    /**
45
     * @var AbstractAdmin
46
     */
47
    private $admin;
48
49
    protected function setUp(): void
50
    {
51
        $this->twig = new Environment(new ArrayLoader(['template' => 'renderedTemplate']));
52
        $this->pool = $this->prophesize(Pool::class);
53
        $this->admin = $this->prophesize(AbstractAdmin::class);
54
        $this->pool->getInstance(Argument::any())->willReturn($this->admin->reveal());
55
        $this->admin->setRequest(Argument::type(Request::class))->shouldBeCalled();
56
        $this->action = new GetShortObjectDescriptionAction(
57
            $this->twig,
58
            $this->pool->reveal()
59
        );
60
    }
61
62
    public function testGetShortObjectDescriptionActionInvalidAdmin(): void
63
    {
64
        $this->expectException(NotFoundHttpException::class);
65
66
        $request = new Request([
67
            'code' => 'sonata.post.admin',
68
            'objectId' => 42,
69
            'uniqid' => 'asdasd123',
70
        ]);
71
72
        $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...
73
        $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...
74
75
        $action = $this->action;
76
        $action($request);
77
    }
78
79
    public function testGetShortObjectDescriptionActionObjectDoesNotExist(): void
80
    {
81
        $this->expectException(\RuntimeException::class);
82
        $this->expectExceptionMessage('Invalid format');
83
84
        $request = new Request([
85
            'code' => 'sonata.post.admin',
86
            'objectId' => 42,
87
            'uniqid' => 'asdasd123',
88
        ]);
89
90
        $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...
91
        $this->admin->getObject(42)->willReturn(false);
92
93
        $action = $this->action;
94
        $action($request);
95
    }
96
97
    public function testGetShortObjectDescriptionActionEmptyObjectId(): void
98
    {
99
        $request = new Request([
100
            'code' => 'sonata.post.admin',
101
            'objectId' => '',
102
            'uniqid' => 'asdasd123',
103
            '_format' => 'html',
104
        ]);
105
106
        $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...
107
        $this->admin->getObject(null)->willReturn(false);
108
109
        $action = $this->action;
110
        $response = $action($request);
111
112
        $this->assertInstanceOf(Response::class, $response);
113
    }
114
115
    public function testGetShortObjectDescriptionActionObject(): void
116
    {
117
        $request = new Request([
118
            'code' => 'sonata.post.admin',
119
            'objectId' => 42,
120
            'uniqid' => 'asdasd123',
121
            '_format' => 'html',
122
        ]);
123
        $object = new \stdClass();
124
125
        $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...
126
        $this->admin->getObject(42)->willReturn($object);
127
        $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...
128
        $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...
129
130
        $action = $this->action;
131
        $response = $action($request);
132
133
        $this->assertSame('renderedTemplate', $response->getContent());
134
    }
135
}
136