Completed
Push — 1.x ( 9bedc1...8f7fe8 )
by Jordi Sala
02:13
created

ListBuilderTest::setUpListActionTests()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 22
rs 9.2
c 1
b 0
f 0
cc 1
eloc 17
nc 1
nop 0
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\DoctrinePHPCRAdminBundle\Tests\Builder;
13
14
use Sonata\DoctrinePHPCRAdminBundle\Builder\ListBuilder;
15
use Sonata\AdminBundle\Admin\AdminInterface;
16
use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
17
use Sonata\DoctrinePHPCRAdminBundle\Admin\FieldDescription;
18
use Sonata\DoctrinePHPCRAdminBundle\Model\ModelManager;
19
use Symfony\Component\Form\Guess\Guess;
20
use Symfony\Component\Form\Guess\TypeGuess;
21
22
class ListBuilderTest extends \PHPUnit_Framework_TestCase
23
{
24
    /**
25
     * @var ListBuilder
26
     */
27
    protected $listBuilder;
28
29
    /**
30
     * @var AdminInterface
31
     */
32
    protected $admin;
33
34
    /**
35
     * @var ModelManager
36
     */
37
    protected $modelManager;
38
39
    public function setUp()
40
    {
41
        $this->guesser = $this->getMock('\Sonata\AdminBundle\Guesser\TypeGuesserInterface', array(), array());
42
        $this->templates = array();
43
    }
44
45
    public function testGetBaseList()
46
    {
47
        $lb = new ListBuilder($this->guesser, $this->templates);
48
        $this->assertInstanceOf('Sonata\AdminBundle\Admin\FieldDescriptionCollection', $lb->getBaseList());
49
    }
50
51
    public function testAddField()
52
    {
53
        $this->setupAddField();
54
        $this->lb->addField($this->fieldDescriptionCollection, 'string', $this->fieldDescription, $this->admin);
55
    }
56
57
    public function testAddFieldNullType()
58
    {
59
        $typeguess = $this->getMock('Symfony\Component\Form\Guess\TypeGuess', array(), array(), '', false);
60
        $this->guesser->expects($this->once())
61
            ->method('guessType')
62
            ->with($this->anything())
63
            ->will($this->returnValue($typeguess));
64
        $this->setupAddField();
65
        $this->lb->addField($this->fieldDescriptionCollection, null, $this->fieldDescription, $this->admin);
66
    }
67
68
    private function setupAddField()
69
    {
70
        $this->lb = new ListBuilder($this->guesser, $this->templates);
71
        $this->metaData = $this->getMock('\Doctrine\ODM\PHPCR\Mapping\ClassMetadata', array(), array(), '', false);
72
        $this->modelManager = $this->getMockBuilder('\Sonata\DoctrinePHPCRAdminBundle\Model\ModelManager')->disableOriginalConstructor()->getMock();
73
        $this->modelManager->expects($this->any())
74
            ->method('getMetadata')
75
            ->will($this->returnValue($this->metaData));
76
        $this->modelManager->expects($this->any())
77
            ->method('hasMetadata')
78
            ->with($this->anything())
79
            ->will($this->returnValue(true));
80
81
        $this->fieldDescription = $this->getMock('\Sonata\AdminBundle\Admin\FieldDescriptionInterface');
82
        $this->fieldDescription->expects($this->any())
83
            ->method('getModelManager')
84
            ->will($this->returnValue($this->modelManager));
85
        $this->fieldDescription->expects($this->any())
86
            ->method('getType')
87
            ->will($this->returnValue('string'));
88
        $this->fieldDescription->expects($this->once())
89
            ->method('setType')
90
            ->with($this->anything());
91
92
        //AdminInterface doesn't implement methods called in addField,
93
        //so we mock Admin
94
        $this->admin = $this->getMock('\Sonata\AdminBundle\Admin\Admin', array(), array(), '', false);
95
        $this->admin->expects($this->any())
96
            ->method('getModelManager')
97
            ->will($this->returnValue($this->modelManager));
98
        $this->admin->expects($this->once())
99
            ->method('addListFieldDescription')
100
            ->with($this->anything(), $this->fieldDescription);
101
102
        $this->fieldDescriptionCollection = $this->getMock('\Sonata\AdminBundle\Admin\FieldDescriptionCollection', array(), array());
103
        $this->fieldDescriptionCollection->expects($this->once())
104
            ->method('add')
105
            ->with($this->fieldDescription);
106
    }
107
108
    //public function testAddField()
109
    //{
110
    //    $fieldDescriptionCollection = $this->getMock('\Sonata\AdminBundle\Admin\FieldDescriptionCollection', array(), array());
111
    //    $fieldDescription = $this->getMock('\Sonata\AdminBundle\Admin\FieldDescriptionInterface', array(), array());
112
    //    $admin = $this->getMock('\Sonata\AdminBundle\Admin\AdminInterface', array(), array());
113
    //    $lb = new ListBuilder($this->guesser, $this->templates);
114
115
    //    $lb->addField($fieldDescriptionCollection, 'sometype', $fieldDescription, $admin);
116
117
    //}
118
    
119
    protected function setUpListActionTests()
120
    {
121
        $this->metaData = $this->getMock(
122
            '\Doctrine\ODM\PHPCR\Mapping\ClassMetadata', array(), array(), '', false
123
        );
124
        $this->modelManager = $this
125
            ->getMockBuilder('\Sonata\DoctrinePHPCRAdminBundle\Model\ModelManager')
126
            ->disableOriginalConstructor()->getMock();
127
        $this->modelManager->expects($this->any())
128
            ->method('getMetadata')
129
            ->will($this->returnValue($this->metaData));
130
        $this->modelManager->expects($this->any())
131
            ->method('hasMetadata')
132
            ->with($this->anything())
133
            ->will($this->returnValue(true));
134
135
        $this->admin = $this->getMock('\Sonata\AdminBundle\Admin\Admin', array(), array(), '', false);
136
        $this->admin->expects($this->atLeastOnce())->method('getModelManager')
137
            ->willReturn($this->modelManager);
138
139
        $this->listBuilder = new ListBuilder($this->guesser);
140
    }
141
142
    public function testAddListActionField()
143
    {
144
        $this->setUpListActionTests();
145
146
        $fieldDescription = new FieldDescription();
147
        $fieldDescription->setName('foo');
148
        $list = $this->listBuilder->getBaseList();
149
        $this->listBuilder
150
            ->addField($list, 'actions', $fieldDescription, $this->admin);
151
152
        $this->assertSame(
153
            'SonataAdminBundle:CRUD:list__action.html.twig',
154
            $list->get('foo')->getTemplate(),
155
            'Custom list action field has a default list action template assigned'
156
        );
157
    }
158
159
    public function testCorrectFixedActionsFieldType()
160
    {
161
        $this->setUpListActionTests();
162
163
        $this->guesser->expects($this->once())->method('guessType')
164
            ->willReturn(new TypeGuess(null, array(), Guess::LOW_CONFIDENCE));
165
166
        $fieldDescription = new FieldDescription();
167
        $fieldDescription->setName('_action');
168
        $list = $this->listBuilder->getBaseList();
169
        $this->listBuilder->addField($list, null, $fieldDescription, $this->admin);
170
171
        $this->assertSame(
172
            'actions',
173
            $list->get('_action')->getType(),
174
            'Standard list _action field has "actions" type'
175
        );
176
    }
177
}
178