Completed
Push — master ( 848fca...e5a7d1 )
by Grégoire
11s
created

AdminVoterTest::testDeprecatedRequestSetter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
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\Menu\Matcher\Voter;
15
16
use Knp\Menu\ItemInterface;
17
use Sonata\AdminBundle\Admin\AbstractAdmin;
18
use Sonata\AdminBundle\Menu\Matcher\Voter\AdminVoter;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\HttpFoundation\RequestStack;
21
22
class AdminVoterTest extends AbstractVoterTest
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function provideData()
28
    {
29
        return [
30
            'no data' => [null, null, null, null],
31
            'no route and granted' => [$this->getAdmin('_sonata_admin'), '_sonata_admin', null, null],
32
            'no granted' => [$this->getAdmin('_sonata_admin', true, false), '_sonata_admin', null, null],
33
            'no code' => [$this->getAdmin('_sonata_admin_code', true, true), '_sonata_admin', null, null],
34
            'no code request' => [$this->getAdmin('_sonata_admin', true, true), '_sonata_admin_unexpected', null, null],
35
            'no route' => [$this->getAdmin('_sonata_admin', false, true), '_sonata_admin', null, null],
36
            'has admin' => [$this->getAdmin('_sonata_admin', true, true), '_sonata_admin', null, true],
37
            'has child admin' => [$this->getChildAdmin('_sonata_admin', '_sonata_child_admin', true, true), '_sonata_admin|_sonata_child_admin', null, true],
38
            'has bad child admin' => [$this->getChildAdmin('_sonata_admin', '_sonata_child_admin', true, true), '_sonata_admin|_sonata_child_admin_unexpected', null, null],
39
            'direct link' => ['admin_post', null, 'admin_post', true],
40
            'no direct link' => ['admin_post', null, 'admin_blog', null],
41
        ];
42
    }
43
44
    /**
45
     * @group legacy
46
     */
47
    public function testDeprecatedRequestSetter(): void
48
    {
49
        $request = new Request();
50
51
        $requestStack = new RequestStack();
52
        $requestStack->push($request);
53
54
        $voter = new AdminVoter();
55
        $voter->setRequest($request);
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Menu\...dminVoter::setRequest() has been deprecated with message: since version 3.x. Pass a RequestStack to the constructor 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...
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    protected function createVoter($dataVoter, $route)
62
    {
63
        $request = new Request();
64
        $request->request->set('_sonata_admin', $dataVoter);
65
        $request->request->set('_route', $route);
66
67
        $requestStack = new RequestStack();
68
        $requestStack->push($request);
69
70
        $voter = new AdminVoter($requestStack);
71
72
        return $voter;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    protected function createItem($data)
79
    {
80
        $item = $this->getMockForAbstractClass(ItemInterface::class);
81
        $item->expects($this->any())
82
             ->method('getExtra')
83
             ->with($this->logicalOr(
84
                $this->equalTo('admin'),
85
                $this->equalTo('route')
86
             ))
87
             ->will($this->returnValue($data))
88
        ;
89
90
        return $item;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    private function getAdmin($code, $list = false, $granted = false)
97
    {
98
        $admin = $this->createMock(AbstractAdmin::class);
99
        $admin
100
            ->expects($this->any())
101
            ->method('hasRoute')
102
            ->with('list')
103
            ->will($this->returnValue($list))
104
        ;
105
        $admin
106
            ->expects($this->any())
107
            ->method('hasAccess')
108
            ->with('list')
109
            ->will($this->returnValue($granted))
110
        ;
111
        $admin
112
            ->expects($this->any())
113
            ->method('getCode')
114
            ->will($this->returnValue($code))
115
        ;
116
        $admin
117
            ->expects($this->any())
118
            ->method('getChildren')
119
            ->will($this->returnValue([]))
120
        ;
121
122
        return $admin;
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    private function getChildAdmin($parentCode, $childCode, $list = false, $granted = false)
129
    {
130
        $parentAdmin = $this->createMock(AbstractAdmin::class);
131
        $parentAdmin
132
            ->expects($this->any())
133
            ->method('hasRoute')
134
            ->with('list')
135
            ->will($this->returnValue($list))
136
        ;
137
        $parentAdmin
138
            ->expects($this->any())
139
            ->method('hasAccess')
140
            ->with('list')
141
            ->will($this->returnValue($granted))
142
        ;
143
        $parentAdmin
144
            ->expects($this->any())
145
            ->method('getCode')
146
            ->will($this->returnValue($parentCode))
147
        ;
148
149
        $childAdmin = $this->createMock(AbstractAdmin::class);
150
        $childAdmin
151
            ->expects($this->any())
152
            ->method('getBaseCodeRoute')
153
            ->will($this->returnValue($parentCode.'|'.$childCode))
154
        ;
155
156
        $parentAdmin
157
            ->expects($this->any())
158
            ->method('getChildren')
159
            ->will($this->returnValue([$childAdmin]))
160
        ;
161
162
        return $parentAdmin;
163
    }
164
}
165