Completed
Push — 3.x ( 11c3ea...aeee6f )
by Grégoire
03:52
created

AdminVoterTest::getChildAdmin()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

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