Completed
Push — 3.x ( bf82cf...39bdd5 )
by Grégoire
04:22
created

AdminVoterTest::getChildAdmin()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

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