Completed
Push — 3.x ( 799626...732596 )
by Grégoire
04:11
created

CoreControllerTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 8
lcom 0
cbo 5
dl 0
loc 126
rs 10
c 3
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
/*
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\Controller;
13
14
use PHPUnit\Framework\TestCase;
15
use Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface;
16
use Sonata\AdminBundle\Admin\Pool;
17
use Sonata\AdminBundle\Controller\CoreController;
18
use Sonata\AdminBundle\Templating\MutableTemplateRegistryInterface;
19
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
20
use Symfony\Component\DependencyInjection\ContainerInterface;
21
use Symfony\Component\HttpFoundation\Request;
22
use Symfony\Component\HttpFoundation\RequestStack;
23
use Symfony\Component\HttpFoundation\Response;
24
25
class CoreControllerTest extends TestCase
26
{
27
    public function testdashboardActionStandardRequest()
28
    {
29
        $container = $this->createMock(ContainerInterface::class);
30
31
        $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class);
32
        $templateRegistry->getTemplate('ajax')->willReturn('ajax.html');
33
        $templateRegistry->getTemplate('dashboard')->willReturn('dashboard.html');
34
        $templateRegistry->getTemplate('layout')->willReturn('layout.html');
35
36
        $pool = new Pool($container, 'title', 'logo.png');
37
        $pool->setTemplateRegistry($templateRegistry->reveal());
38
39
        $templating = $this->createMock(EngineInterface::class);
40
        $request = new Request();
41
42
        $requestStack = new RequestStack();
43
        $requestStack->push($request);
44
45
        $breadcrumbsBuilder = $this->getMockForAbstractClass(BreadcrumbsBuilderInterface::class);
46
47
        $values = [
48
            'sonata.admin.breadcrumbs_builder' => $breadcrumbsBuilder,
49
            'sonata.admin.pool' => $pool,
50
            'templating' => $templating,
51
            'request_stack' => $requestStack,
52
            'sonata.admin.global_template_registry' => $templateRegistry->reveal(),
53
        ];
54
55
        $container->expects($this->any())->method('get')->will($this->returnCallback(function ($id) use ($values) {
56
            return $values[$id];
57
        }));
58
59
        $container->expects($this->any())
60
            ->method('has')
61
            ->will($this->returnCallback(function ($id) {
62
                if ('templating' == $id) {
63
                    return true;
64
                }
65
66
                return false;
67
            }));
68
69
        $container->expects($this->any())->method('getParameter')->will($this->returnCallback(function ($name) {
70
            if ('sonata.admin.configuration.dashboard_blocks' == $name) {
71
                return [];
72
            }
73
        }));
74
        $container->expects($this->any())->method('has')->will($this->returnCallback(function ($id) {
75
            if ('templating' == $id) {
76
                return true;
77
            }
78
79
            return false;
80
        }));
81
82
        $controller = new CoreController();
83
        $controller->setContainer($container);
84
85
        $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...
86
87
        $this->isInstanceOf(Response::class, $response);
88
    }
89
90
    public function testdashboardActionAjaxLayout()
91
    {
92
        $container = $this->createMock(ContainerInterface::class);
93
94
        $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class);
95
        $templateRegistry->getTemplate('ajax')->willReturn('ajax.html');
96
        $templateRegistry->getTemplate('dashboard')->willReturn('dashboard.html');
97
        $templateRegistry->getTemplate('layout')->willReturn('layout.html');
98
99
        $pool = new Pool($container, 'title', 'logo.png');
100
        $pool->setTemplateRegistry($templateRegistry->reveal());
101
102
        $templating = $this->createMock(EngineInterface::class);
103
        $request = new Request();
104
        $request->headers->set('X-Requested-With', 'XMLHttpRequest');
105
106
        $requestStack = new RequestStack();
107
        $requestStack->push($request);
108
109
        $values = [
110
            'sonata.admin.pool' => $pool,
111
            'templating' => $templating,
112
            'request_stack' => $requestStack,
113
            'sonata.admin.global_template_registry' => $templateRegistry->reveal(),
114
        ];
115
116
        $container->expects($this->any())->method('get')->will($this->returnCallback(function ($id) use ($values) {
117
            return $values[$id];
118
        }));
119
120
        $container->expects($this->any())
121
            ->method('has')
122
            ->will($this->returnCallback(function ($id) {
123
                if ('templating' == $id) {
124
                    return true;
125
                }
126
127
                return false;
128
            }));
129
130
        $container->expects($this->any())->method('getParameter')->will($this->returnCallback(function ($name) {
131
            if ('sonata.admin.configuration.dashboard_blocks' == $name) {
132
                return [];
133
            }
134
        }));
135
        $container->expects($this->any())->method('has')->will($this->returnCallback(function ($id) {
136
            if ('templating' == $id) {
137
                return true;
138
            }
139
140
            return false;
141
        }));
142
143
        $controller = new CoreController();
144
        $controller->setContainer($container);
145
146
        $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...
147
148
        $this->isInstanceOf(Response::class, $response);
149
    }
150
}
151