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\Unit\Builder; |
||
13 | |||
14 | use PHPUnit\Framework\TestCase; |
||
15 | use Sonata\AdminBundle\Admin\FieldDescriptionCollection; |
||
16 | use Sonata\AdminBundle\Admin\FieldDescriptionInterface; |
||
17 | use Sonata\AdminBundle\Guesser\TypeGuesserInterface; |
||
18 | use Sonata\DoctrinePHPCRAdminBundle\Admin\Admin; |
||
19 | use Sonata\DoctrinePHPCRAdminBundle\Admin\FieldDescription; |
||
20 | use Sonata\DoctrinePHPCRAdminBundle\Builder\ListBuilder; |
||
21 | use Sonata\DoctrinePHPCRAdminBundle\Model\ModelManager; |
||
22 | use Symfony\Component\Form\Guess\Guess; |
||
23 | use Symfony\Component\Form\Guess\TypeGuess; |
||
24 | |||
25 | class ListBuilderTest extends TestCase |
||
26 | { |
||
27 | /** |
||
28 | * @var ListBuilder |
||
29 | */ |
||
30 | private $lb; |
||
31 | |||
32 | /** |
||
33 | * @var Admin |
||
34 | */ |
||
35 | private $admin; |
||
36 | |||
37 | /** |
||
38 | * @var ModelManager |
||
39 | */ |
||
40 | private $modelManager; |
||
41 | |||
42 | /** |
||
43 | * @var FieldDescriptionInterface |
||
44 | */ |
||
45 | private $fieldDescription; |
||
46 | |||
47 | /** |
||
48 | * @var FieldDescriptionCollection |
||
49 | */ |
||
50 | private $fieldDescriptionCollection; |
||
51 | |||
52 | /** |
||
53 | * @var TypeGuesserInterface |
||
54 | */ |
||
55 | private $guesser; |
||
56 | |||
57 | public function setUp() |
||
58 | { |
||
59 | $this->guesser = $this->createMock('\Sonata\AdminBundle\Guesser\TypeGuesserInterface', [], []); |
||
60 | $this->templates = []; |
||
0 ignored issues
–
show
|
|||
61 | } |
||
62 | |||
63 | public function testGetBaseList() |
||
64 | { |
||
65 | $lb = new ListBuilder($this->guesser, $this->templates); |
||
66 | $this->assertInstanceOf('Sonata\AdminBundle\Admin\FieldDescriptionCollection', $lb->getBaseList()); |
||
67 | } |
||
68 | |||
69 | public function testAddField() |
||
70 | { |
||
71 | $this->setupAddField(); |
||
72 | $this->lb->addField($this->fieldDescriptionCollection, 'string', $this->fieldDescription, $this->admin); |
||
73 | } |
||
74 | |||
75 | public function testAddFieldNullType() |
||
76 | { |
||
77 | $typeguess = $this->createMock('Symfony\Component\Form\Guess\TypeGuess', [], [], '', false); |
||
78 | $this->guesser->expects($this->once()) |
||
79 | ->method('guessType') |
||
80 | ->with($this->anything()) |
||
81 | ->will($this->returnValue($typeguess)); |
||
82 | $this->setupAddField(); |
||
83 | $this->lb->addField($this->fieldDescriptionCollection, null, $this->fieldDescription, $this->admin); |
||
84 | } |
||
85 | |||
86 | public function testAddListActionField() |
||
87 | { |
||
88 | $this->setUpListActionTests(); |
||
89 | |||
90 | $fieldDescription = new FieldDescription(); |
||
91 | $fieldDescription->setName('foo'); |
||
92 | $list = $this->listBuilder->getBaseList(); |
||
0 ignored issues
–
show
The property
listBuilder does not exist. Did you maybe forget to declare it?
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code: class MyClass { }
$x = new MyClass();
$x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: class MyClass {
public $foo;
}
$x = new MyClass();
$x->foo = true;
Loading history...
|
|||
93 | $this->listBuilder |
||
94 | ->addField($list, 'actions', $fieldDescription, $this->admin); |
||
95 | |||
96 | $this->assertSame( |
||
97 | 'SonataAdminBundle:CRUD:list__action.html.twig', |
||
98 | $list->get('foo')->getTemplate(), |
||
99 | 'Custom list action field has a default list action template assigned' |
||
100 | ); |
||
101 | } |
||
102 | |||
103 | public function testCorrectFixedActionsFieldType() |
||
104 | { |
||
105 | $this->setUpListActionTests(); |
||
106 | |||
107 | $this->guesser->expects($this->once())->method('guessType') |
||
108 | ->willReturn(new TypeGuess(null, [], Guess::LOW_CONFIDENCE)); |
||
109 | |||
110 | $fieldDescription = new FieldDescription(); |
||
111 | $fieldDescription->setName('_action'); |
||
112 | $list = $this->listBuilder->getBaseList(); |
||
113 | $this->listBuilder->addField($list, null, $fieldDescription, $this->admin); |
||
114 | |||
115 | $this->assertSame( |
||
116 | 'actions', |
||
117 | $list->get('_action')->getType(), |
||
118 | 'Standard list _action field has "actions" type' |
||
119 | ); |
||
120 | } |
||
121 | |||
122 | //public function testAddField() |
||
123 | //{ |
||
124 | // $fieldDescriptionCollection = $this->createMock('\Sonata\AdminBundle\Admin\FieldDescriptionCollection', [], []); |
||
125 | // $fieldDescription = $this->createMock('\Sonata\AdminBundle\Admin\FieldDescriptionInterface', [], []); |
||
126 | // $admin = $this->createMock('\Sonata\AdminBundle\Admin\AdminInterface', [], []); |
||
127 | // $lb = new ListBuilder($this->guesser, $this->templates); |
||
128 | |||
129 | // $lb->addField($fieldDescriptionCollection, 'sometype', $fieldDescription, $admin); |
||
130 | |||
131 | //} |
||
132 | |||
133 | protected function setUpListActionTests() |
||
134 | { |
||
135 | $this->metaData = $this->createMock('\Doctrine\ODM\PHPCR\Mapping\ClassMetadata'); |
||
0 ignored issues
–
show
The property
metaData does not exist. Did you maybe forget to declare it?
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code: class MyClass { }
$x = new MyClass();
$x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: class MyClass {
public $foo;
}
$x = new MyClass();
$x->foo = true;
Loading history...
|
|||
136 | $this->modelManager = $this->createMock('Sonata\DoctrinePHPCRAdminBundle\Model\ModelManager'); |
||
137 | $this->modelManager->expects($this->any()) |
||
138 | ->method('getMetadata') |
||
139 | ->will($this->returnValue($this->metaData)); |
||
140 | $this->modelManager->expects($this->any()) |
||
141 | ->method('hasMetadata') |
||
142 | ->with($this->anything()) |
||
143 | ->will($this->returnValue(true)); |
||
144 | |||
145 | $this->admin = $this->createMock('\Sonata\AdminBundle\Admin\Admin', [], [], '', false); |
||
146 | $this->admin->expects($this->atLeastOnce())->method('getModelManager') |
||
147 | ->willReturn($this->modelManager); |
||
148 | |||
149 | $this->listBuilder = new ListBuilder($this->guesser); |
||
150 | } |
||
151 | |||
152 | private function setupAddField() |
||
153 | { |
||
154 | $this->lb = new ListBuilder($this->guesser, $this->templates); |
||
155 | $this->metaData = $this->createMock('\Doctrine\ODM\PHPCR\Mapping\ClassMetadata', [], [], '', false); |
||
156 | $this->modelManager = $this->createMock('\Sonata\DoctrinePHPCRAdminBundle\Model\ModelManager'); |
||
157 | $this->modelManager->expects($this->any()) |
||
158 | ->method('getMetadata') |
||
159 | ->will($this->returnValue($this->metaData)); |
||
160 | $this->modelManager->expects($this->any()) |
||
161 | ->method('hasMetadata') |
||
162 | ->with($this->anything()) |
||
163 | ->will($this->returnValue(true)); |
||
164 | |||
165 | $this->fieldDescription = $this->createMock('\Sonata\AdminBundle\Admin\FieldDescriptionInterface'); |
||
166 | $this->fieldDescription->expects($this->any()) |
||
167 | ->method('getType') |
||
168 | ->will($this->returnValue('string')); |
||
169 | $this->fieldDescription->expects($this->once()) |
||
170 | ->method('setType') |
||
171 | ->with($this->anything()); |
||
172 | |||
173 | //AdminInterface doesn't implement methods called in addField, |
||
174 | //so we mock Admin |
||
175 | $this->admin = $this->createMock('\Sonata\AdminBundle\Admin\AbstractAdmin', [], [], '', false); |
||
176 | $this->admin->expects($this->any()) |
||
177 | ->method('getModelManager') |
||
178 | ->will($this->returnValue($this->modelManager)); |
||
179 | $this->admin->expects($this->once()) |
||
180 | ->method('addListFieldDescription') |
||
181 | ->with($this->anything(), $this->fieldDescription); |
||
182 | |||
183 | $this->fieldDescriptionCollection = $this->createMock('\Sonata\AdminBundle\Admin\FieldDescriptionCollection'); |
||
184 | $this->fieldDescriptionCollection->expects($this->once()) |
||
185 | ->method('add') |
||
186 | ->with($this->fieldDescription); |
||
187 | } |
||
188 | } |
||
189 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: