Completed
Push — 3.x ( db377c...cd74a2 )
by Andrey F.
12:41
created

DashboardActionTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 5
dl 0
loc 93
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B testdashboardActionStandardRequest() 0 44 1
B testDashboardActionAjaxLayout() 0 45 1
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 Sonata\AdminBundle\Action\DashboardAction;
16
use Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface;
17
use Sonata\AdminBundle\Admin\Pool;
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 DashboardActionTest 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
        $dashboardAction = new DashboardAction(
48
            [],
49
            $breadcrumbsBuilder,
50
            $templateRegistry->reveal(),
51
            $pool
52
        );
53
        $values = [
54
            'templating' => $templating,
55
            'request_stack' => $requestStack,
56
        ];
57
        $dashboardAction->setContainer($container);
58
59
        $container->expects($this->any())->method('get')->will($this->returnCallback(function ($id) use ($values) {
60
            return $values[$id];
61
        }));
62
63
        $container->expects($this->any())
64
            ->method('has')
65
            ->will($this->returnCallback(function ($id) {
66
                return 'templating' === $id;
67
            }));
68
69
        $this->isInstanceOf(Response::class, $dashboardAction($request));
70
    }
71
72
    public function testDashboardActionAjaxLayout()
73
    {
74
        $container = $this->createMock(ContainerInterface::class);
75
76
        $templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class);
77
        $templateRegistry->getTemplate('ajax')->willReturn('ajax.html');
78
        $templateRegistry->getTemplate('dashboard')->willReturn('dashboard.html');
79
        $templateRegistry->getTemplate('layout')->willReturn('layout.html');
80
        $breadcrumbsBuilder = $this->getMockForAbstractClass(BreadcrumbsBuilderInterface::class);
81
82
        $pool = new Pool($container, 'title', 'logo.png');
83
        $pool->setTemplateRegistry($templateRegistry->reveal());
84
85
        $templating = $this->createMock(EngineInterface::class);
86
        $request = new Request();
87
        $request->headers->set('X-Requested-With', 'XMLHttpRequest');
88
89
        $requestStack = new RequestStack();
90
        $requestStack->push($request);
91
92
        $dashboardAction = new DashboardAction(
93
            [],
94
            $breadcrumbsBuilder,
95
            $templateRegistry->reveal(),
96
            $pool
97
        );
98
        $dashboardAction->setContainer($container);
99
        $values = [
100
            'templating' => $templating,
101
            'request_stack' => $requestStack,
102
        ];
103
        $dashboardAction->setContainer($container);
104
105
        $container->expects($this->any())->method('get')->will($this->returnCallback(function ($id) use ($values) {
106
            return $values[$id];
107
        }));
108
109
        $container->expects($this->any())
110
            ->method('has')
111
            ->will($this->returnCallback(function ($id) {
112
                return 'templating' === $id;
113
            }));
114
115
        $this->isInstanceOf(Response::class, $dashboardAction($request));
116
    }
117
}
118