Completed
Pull Request — master (#5032)
by Grégoire
03:41
created

CoreControllerTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
lcom 0
cbo 5
dl 0
loc 126
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B testdashboardActionStandardRequest() 0 62 4
B testdashboardActionAjaxLayout() 0 60 4
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\Controller;
15
16
use PHPUnit\Framework\TestCase;
17
use Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface;
18
use Sonata\AdminBundle\Admin\Pool;
19
use Sonata\AdminBundle\Controller\CoreController;
20
use Sonata\AdminBundle\Templating\MutableTemplateRegistryInterface;
21
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
22
use Symfony\Component\DependencyInjection\ContainerInterface;
23
use Symfony\Component\HttpFoundation\Request;
24
use Symfony\Component\HttpFoundation\RequestStack;
25
use Symfony\Component\HttpFoundation\Response;
26
27
class CoreControllerTest extends TestCase
28
{
29
    public function testdashboardActionStandardRequest(): void
30
    {
31
        $container = $this->createMock(ContainerInterface::class);
32
33
        $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class);
34
        $templateRegistry->getTemplate('ajax')->willReturn('ajax.html');
35
        $templateRegistry->getTemplate('dashboard')->willReturn('dashboard.html');
36
        $templateRegistry->getTemplate('layout')->willReturn('layout.html');
37
38
        $pool = new Pool($container, 'title', 'logo.png');
39
        $pool->setTemplateRegistry($templateRegistry->reveal());
40
41
        $templating = $this->createMock(EngineInterface::class);
42
        $request = new Request();
43
44
        $requestStack = new RequestStack();
45
        $requestStack->push($request);
46
47
        $breadcrumbsBuilder = $this->getMockForAbstractClass(BreadcrumbsBuilderInterface::class);
48
49
        $values = [
50
            'sonata.admin.breadcrumbs_builder' => $breadcrumbsBuilder,
51
            'sonata.admin.pool' => $pool,
52
            'templating' => $templating,
53
            'request_stack' => $requestStack,
54
            'sonata.admin.global_template_registry' => $templateRegistry->reveal(),
55
        ];
56
57
        $container->expects($this->any())->method('get')->will($this->returnCallback(function ($id) use ($values) {
58
            return $values[$id];
59
        }));
60
61
        $container->expects($this->any())
62
            ->method('has')
63
            ->will($this->returnCallback(function ($id) {
64
                if ('templating' == $id) {
65
                    return true;
66
                }
67
68
                return false;
69
            }));
70
71
        $container->expects($this->any())->method('getParameter')->will($this->returnCallback(function ($name) {
72
            if ('sonata.admin.configuration.dashboard_blocks' == $name) {
73
                return [];
74
            }
75
        }));
76
        $container->expects($this->any())->method('has')->will($this->returnCallback(function ($id) {
77
            if ('templating' == $id) {
78
                return true;
79
            }
80
81
            return false;
82
        }));
83
84
        $controller = new CoreController();
85
        $controller->setContainer($container);
86
87
        $response = $controller->dashboardAction($request);
0 ignored issues
show
Unused Code introduced by
The call to CoreController::dashboardAction() has too many arguments starting with $request.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
88
89
        $this->isInstanceOf(Response::class, $response);
90
    }
91
92
    public function testdashboardActionAjaxLayout(): void
93
    {
94
        $container = $this->createMock(ContainerInterface::class);
95
96
        $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class);
97
        $templateRegistry->getTemplate('ajax')->willReturn('ajax.html');
98
        $templateRegistry->getTemplate('dashboard')->willReturn('dashboard.html');
99
        $templateRegistry->getTemplate('layout')->willReturn('layout.html');
100
101
        $pool = new Pool($container, 'title', 'logo.png');
102
        $pool->setTemplateRegistry($templateRegistry->reveal());
103
104
        $templating = $this->createMock(EngineInterface::class);
105
        $request = new Request();
106
        $request->headers->set('X-Requested-With', 'XMLHttpRequest');
107
108
        $requestStack = new RequestStack();
109
        $requestStack->push($request);
110
111
        $values = [
112
            'sonata.admin.pool' => $pool,
113
            'templating' => $templating,
114
            'request_stack' => $requestStack,
115
            'sonata.admin.global_template_registry' => $templateRegistry->reveal(),
116
        ];
117
118
        $container->expects($this->any())->method('get')->will($this->returnCallback(function ($id) use ($values) {
119
            return $values[$id];
120
        }));
121
122
        $container->expects($this->any())
123
            ->method('has')
124
            ->will($this->returnCallback(function ($id) {
125
                if ('templating' == $id) {
126
                    return true;
127
                }
128
129
                return false;
130
            }));
131
132
        $container->expects($this->any())->method('getParameter')->will($this->returnCallback(function ($name) {
133
            if ('sonata.admin.configuration.dashboard_blocks' == $name) {
134
                return [];
135
            }
136
        }));
137
        $container->expects($this->any())->method('has')->will($this->returnCallback(function ($id) {
138
            if ('templating' == $id) {
139
                return true;
140
            }
141
142
            return false;
143
        }));
144
145
        $controller = new CoreController();
146
        $controller->setContainer($container);
147
148
        $response = $controller->dashboardAction($request);
0 ignored issues
show
Unused Code introduced by
The call to CoreController::dashboardAction() has too many arguments starting with $request.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
149
150
        $this->isInstanceOf(Response::class, $response);
151
    }
152
}
153