Completed
Pull Request — 1.x (#432)
by
unknown
14:08
created

ListBuilderTest::testAddListActionField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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