Completed
Push — master ( a33308...36ba79 )
by Maximilian
01:58
created

tests/Builder/ListBuilderTest.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\AdminBundle\Admin\FieldDescriptionCollection;
15
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
16
use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
17
use Sonata\DoctrinePHPCRAdminBundle\Admin\Admin;
18
use Sonata\DoctrinePHPCRAdminBundle\Admin\FieldDescription;
19
use Sonata\DoctrinePHPCRAdminBundle\Builder\ListBuilder;
20
use Sonata\DoctrinePHPCRAdminBundle\Model\ModelManager;
21
use Symfony\Component\Form\Guess\Guess;
22
use Symfony\Component\Form\Guess\TypeGuess;
23
24
class ListBuilderTest extends \PHPUnit_Framework_TestCase
25
{
26
    /**
27
     * @var ListBuilder
28
     */
29
    private $lb;
30
31
    /**
32
     * @var Admin
33
     */
34
    private $admin;
35
36
    /**
37
     * @var ModelManager
38
     */
39
    private $modelManager;
40
41
    /**
42
     * @var FieldDescriptionInterface
43
     */
44
    private $fieldDescription;
45
46
    /**
47
     * @var FieldDescriptionCollection
48
     */
49
    private $fieldDescriptionCollection;
50
51
    /**
52
     * @var TypeGuesserInterface
53
     */
54
    private $guesser;
55
56
    public function setUp()
57
    {
58
        $this->guesser = $this->createMock('\Sonata\AdminBundle\Guesser\TypeGuesserInterface', array(), array());
0 ignored issues
show
The call to ListBuilderTest::createMock() has too many arguments starting with array().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
59
        $this->templates = array();
60
    }
61
62
    public function testGetBaseList()
63
    {
64
        $lb = new ListBuilder($this->guesser, $this->templates);
65
        $this->assertInstanceOf('Sonata\AdminBundle\Admin\FieldDescriptionCollection', $lb->getBaseList());
66
    }
67
68
    public function testAddField()
69
    {
70
        $this->setupAddField();
71
        $this->lb->addField($this->fieldDescriptionCollection, 'string', $this->fieldDescription, $this->admin);
72
    }
73
74
    public function testAddFieldNullType()
75
    {
76
        $typeguess = $this->createMock('Symfony\Component\Form\Guess\TypeGuess', array(), array(), '', false);
0 ignored issues
show
The call to ListBuilderTest::createMock() has too many arguments starting with array().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
77
        $this->guesser->expects($this->once())
78
            ->method('guessType')
79
            ->with($this->anything())
80
            ->will($this->returnValue($typeguess));
81
        $this->setupAddField();
82
        $this->lb->addField($this->fieldDescriptionCollection, null, $this->fieldDescription, $this->admin);
83
    }
84
85
    public function testAddListActionField()
86
    {
87
        $this->setUpListActionTests();
88
89
        $fieldDescription = new FieldDescription();
90
        $fieldDescription->setName('foo');
91
        $list = $this->listBuilder->getBaseList();
92
        $this->listBuilder
93
            ->addField($list, 'actions', $fieldDescription, $this->admin);
94
95
        $this->assertSame(
96
            'SonataAdminBundle:CRUD:list__action.html.twig',
97
            $list->get('foo')->getTemplate(),
98
            'Custom list action field has a default list action template assigned'
99
        );
100
    }
101
102
    public function testCorrectFixedActionsFieldType()
103
    {
104
        $this->setUpListActionTests();
105
106
        $this->guesser->expects($this->once())->method('guessType')
107
            ->willReturn(new TypeGuess(null, array(), Guess::LOW_CONFIDENCE));
108
109
        $fieldDescription = new FieldDescription();
110
        $fieldDescription->setName('_action');
111
        $list = $this->listBuilder->getBaseList();
112
        $this->listBuilder->addField($list, null, $fieldDescription, $this->admin);
113
114
        $this->assertSame(
115
            'actions',
116
            $list->get('_action')->getType(),
117
            'Standard list _action field has "actions" type'
118
        );
119
    }
120
121
    //public function testAddField()
122
    //{
123
    //    $fieldDescriptionCollection = $this->getMock('\Sonata\AdminBundle\Admin\FieldDescriptionCollection', array(), array());
124
    //    $fieldDescription = $this->getMock('\Sonata\AdminBundle\Admin\FieldDescriptionInterface', array(), array());
125
    //    $admin = $this->getMock('\Sonata\AdminBundle\Admin\AdminInterface', array(), array());
126
    //    $lb = new ListBuilder($this->guesser, $this->templates);
127
128
    //    $lb->addField($fieldDescriptionCollection, 'sometype', $fieldDescription, $admin);
129
130
    //}
131
132
    protected function setUpListActionTests()
133
    {
134
        $this->metaData = $this->createMock('\Doctrine\ODM\PHPCR\Mapping\ClassMetadata');
135
        $this->modelManager = $this->createMock('Sonata\DoctrinePHPCRAdminBundle\Model\ModelManager');
136
        $this->modelManager->expects($this->any())
137
            ->method('getMetadata')
138
            ->will($this->returnValue($this->metaData));
139
        $this->modelManager->expects($this->any())
140
            ->method('hasMetadata')
141
            ->with($this->anything())
142
            ->will($this->returnValue(true));
143
144
        $this->admin = $this->createMock('\Sonata\AdminBundle\Admin\Admin', array(), array(), '', false);
0 ignored issues
show
The call to ListBuilderTest::createMock() has too many arguments starting with array().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
145
        $this->admin->expects($this->atLeastOnce())->method('getModelManager')
146
            ->willReturn($this->modelManager);
147
148
        $this->listBuilder = new ListBuilder($this->guesser);
149
    }
150
151
    private function setupAddField()
152
    {
153
        $this->lb = new ListBuilder($this->guesser, $this->templates);
154
        $this->metaData = $this->createMock('\Doctrine\ODM\PHPCR\Mapping\ClassMetadata', array(), array(), '', false);
0 ignored issues
show
The call to ListBuilderTest::createMock() has too many arguments starting with array().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
155
        $this->modelManager = $this->createMock('\Sonata\DoctrinePHPCRAdminBundle\Model\ModelManager');
156
        $this->modelManager->expects($this->any())
157
            ->method('getMetadata')
158
            ->will($this->returnValue($this->metaData));
159
        $this->modelManager->expects($this->any())
160
            ->method('hasMetadata')
161
            ->with($this->anything())
162
            ->will($this->returnValue(true));
163
164
        $this->fieldDescription = $this->createMock('\Sonata\AdminBundle\Admin\FieldDescriptionInterface');
165
        $this->fieldDescription->expects($this->any())
166
            ->method('getType')
167
            ->will($this->returnValue('string'));
168
        $this->fieldDescription->expects($this->once())
169
            ->method('setType')
170
            ->with($this->anything());
171
172
        //AdminInterface doesn't implement methods called in addField,
173
        //so we mock Admin
174
        $this->admin = $this->createMock('\Sonata\AdminBundle\Admin\AbstractAdmin', array(), array(), '', false);
0 ignored issues
show
The call to ListBuilderTest::createMock() has too many arguments starting with array().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
175
        $this->admin->expects($this->any())
176
            ->method('getModelManager')
177
            ->will($this->returnValue($this->modelManager));
178
        $this->admin->expects($this->once())
179
            ->method('addListFieldDescription')
180
            ->with($this->anything(), $this->fieldDescription);
181
182
        $this->fieldDescriptionCollection = $this->createMock('\Sonata\AdminBundle\Admin\FieldDescriptionCollection');
183
        $this->fieldDescriptionCollection->expects($this->once())
184
            ->method('add')
185
            ->with($this->fieldDescription);
186
    }
187
}
188