These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /* |
||
6 | * This file is part of the Sonata Project package. |
||
7 | * |
||
8 | * (c) Thomas Rabaix <[email protected]> |
||
9 | * |
||
10 | * For the full copyright and license information, please view the LICENSE |
||
11 | * file that was distributed with this source code. |
||
12 | */ |
||
13 | |||
14 | namespace Sonata\DoctrinePHPCRAdminBundle\Tests\Unit\Builder; |
||
15 | |||
16 | use Doctrine\ODM\PHPCR\Mapping\ClassMetadata; |
||
17 | use PHPUnit\Framework\TestCase; |
||
18 | use Sonata\AdminBundle\Admin\AbstractAdmin; |
||
19 | use Sonata\AdminBundle\Admin\FieldDescriptionCollection; |
||
20 | use Sonata\AdminBundle\Admin\FieldDescriptionInterface; |
||
21 | use Sonata\AdminBundle\Guesser\TypeGuesserInterface; |
||
22 | use Sonata\DoctrinePHPCRAdminBundle\Admin\Admin; |
||
23 | use Sonata\DoctrinePHPCRAdminBundle\Admin\FieldDescription; |
||
24 | use Sonata\DoctrinePHPCRAdminBundle\Builder\ListBuilder; |
||
25 | use Sonata\DoctrinePHPCRAdminBundle\Model\ModelManager; |
||
26 | use Symfony\Component\Form\Guess\TypeGuess; |
||
27 | |||
28 | class ListBuilderTest extends TestCase |
||
29 | { |
||
30 | /** |
||
31 | * @var ListBuilder |
||
32 | */ |
||
33 | private $lb; |
||
34 | |||
35 | /** |
||
36 | * @var Admin |
||
37 | */ |
||
38 | private $admin; |
||
39 | |||
40 | /** |
||
41 | * @var ModelManager |
||
42 | */ |
||
43 | private $modelManager; |
||
44 | |||
45 | /** |
||
46 | * @var FieldDescriptionInterface |
||
47 | */ |
||
48 | private $fieldDescription; |
||
49 | |||
50 | /** |
||
51 | * @var FieldDescriptionCollection |
||
52 | */ |
||
53 | private $fieldDescriptionCollection; |
||
54 | |||
55 | /** |
||
56 | * @var TypeGuesserInterface |
||
57 | */ |
||
58 | private $guesser; |
||
59 | |||
60 | public function setUp(): void |
||
61 | { |
||
62 | $this->guesser = $this->createMock(TypeGuesserInterface::class); |
||
63 | $this->templates = []; |
||
0 ignored issues
–
show
|
|||
64 | } |
||
65 | |||
66 | public function testGetBaseList(): void |
||
67 | { |
||
68 | $lb = new ListBuilder($this->guesser, $this->templates); |
||
69 | $this->assertInstanceOf(FieldDescriptionCollection::class, $lb->getBaseList()); |
||
70 | } |
||
71 | |||
72 | public function testAddField(): void |
||
73 | { |
||
74 | $this->setupAddField(); |
||
75 | $this->lb->addField($this->fieldDescriptionCollection, 'string', $this->fieldDescription, $this->admin); |
||
76 | } |
||
77 | |||
78 | public function testAddFieldNullType(): void |
||
79 | { |
||
80 | $typeguess = $this->createMock(TypeGuess::class, [], [], '', false); |
||
81 | $this->guesser->expects($this->once()) |
||
82 | ->method('guessType') |
||
83 | ->with($this->anything()) |
||
84 | ->willReturn($typeguess); |
||
85 | $this->setupAddField(); |
||
86 | $this->lb->addField($this->fieldDescriptionCollection, null, $this->fieldDescription, $this->admin); |
||
87 | } |
||
88 | |||
89 | public function testAddListActionField(): void |
||
90 | { |
||
91 | $this->setUpListActionTests(); |
||
92 | |||
93 | $fieldDescription = new FieldDescription(); |
||
94 | $fieldDescription->setName('foo'); |
||
95 | $list = $this->listBuilder->getBaseList(); |
||
96 | $this->listBuilder |
||
97 | ->addField($list, 'actions', $fieldDescription, $this->admin); |
||
98 | |||
99 | $this->assertSame( |
||
100 | '@SonataAdmin/CRUD/list__action.html.twig', |
||
101 | $list->get('foo')->getTemplate(), |
||
102 | 'Custom list action field has a default list action template assigned' |
||
103 | ); |
||
104 | } |
||
105 | |||
106 | public function testCorrectFixedActionsFieldType(): void |
||
107 | { |
||
108 | $this->setUpListActionTests(); |
||
109 | $this->guesser->expects($this->once())->method('guessType')->willReturn(null); |
||
110 | |||
111 | $fieldDescription = new FieldDescription(); |
||
112 | $fieldDescription->setName('_action'); |
||
113 | $list = $this->listBuilder->getBaseList(); |
||
114 | $this->listBuilder->addField($list, null, $fieldDescription, $this->admin); |
||
115 | |||
116 | $this->assertSame( |
||
117 | 'actions', |
||
118 | $list->get('_action')->getType(), |
||
119 | 'Standard list _action field has "actions" type' |
||
120 | ); |
||
121 | } |
||
122 | |||
123 | //public function testAddField() |
||
124 | //{ |
||
125 | // $fieldDescriptionCollection = $this->createMock('\Sonata\AdminBundle\Admin\FieldDescriptionCollection', [], []); |
||
126 | // $fieldDescription = $this->createMock('\Sonata\AdminBundle\Admin\FieldDescriptionInterface', [], []); |
||
127 | // $admin = $this->createMock('\Sonata\AdminBundle\Admin\AdminInterface', [], []); |
||
128 | // $lb = new ListBuilder($this->guesser, $this->templates); |
||
129 | |||
130 | // $lb->addField($fieldDescriptionCollection, 'sometype', $fieldDescription, $admin); |
||
131 | |||
132 | //} |
||
133 | |||
134 | protected function setUpListActionTests(): void |
||
135 | { |
||
136 | $this->metaData = $this->createMock(ClassMetadata::class); |
||
137 | $this->modelManager = $this->createMock(ModelManager::class); |
||
138 | $this->modelManager->expects($this->any()) |
||
139 | ->method('getMetadata') |
||
140 | ->willReturn($this->metaData); |
||
141 | $this->modelManager->expects($this->any()) |
||
142 | ->method('hasMetadata') |
||
143 | ->with($this->anything()) |
||
144 | ->willReturn(true); |
||
145 | |||
146 | $this->admin = $this->createMock(Admin::class, [], [], '', false); |
||
147 | $this->admin->expects($this->atLeastOnce())->method('getModelManager') |
||
148 | ->willReturn($this->modelManager); |
||
149 | |||
150 | $this->listBuilder = new ListBuilder($this->guesser); |
||
151 | } |
||
152 | |||
153 | private function setupAddField(): void |
||
154 | { |
||
155 | $this->lb = new ListBuilder($this->guesser, $this->templates); |
||
156 | $this->metaData = $this->createMock(ClassMetadata::class, [], [], '', false); |
||
157 | $this->modelManager = $this->createMock(ModelManager::class); |
||
158 | $this->modelManager->expects($this->any()) |
||
159 | ->method('getMetadata') |
||
160 | ->willReturn($this->metaData); |
||
161 | $this->modelManager->expects($this->any()) |
||
162 | ->method('hasMetadata') |
||
163 | ->with($this->anything()) |
||
164 | ->willReturn(true); |
||
165 | |||
166 | $this->fieldDescription = $this->createMock(FieldDescriptionInterface::class); |
||
167 | $this->fieldDescription->expects($this->any()) |
||
168 | ->method('getType') |
||
169 | ->willReturn('string'); |
||
170 | $this->fieldDescription->expects($this->once()) |
||
171 | ->method('setType') |
||
172 | ->with($this->anything()); |
||
173 | |||
174 | //AdminInterface doesn't implement methods called in addField, |
||
175 | //so we mock Admin |
||
176 | $this->admin = $this->createMock(AbstractAdmin::class, [], [], '', false); |
||
177 | $this->admin->expects($this->any()) |
||
178 | ->method('getModelManager') |
||
179 | ->willReturn($this->modelManager); |
||
180 | $this->admin->expects($this->once()) |
||
181 | ->method('addListFieldDescription') |
||
182 | ->with($this->anything(), $this->fieldDescription); |
||
183 | |||
184 | $this->fieldDescriptionCollection = $this->createMock(FieldDescriptionCollection::class); |
||
185 | $this->fieldDescriptionCollection->expects($this->once()) |
||
186 | ->method('add') |
||
187 | ->with($this->fieldDescription); |
||
188 | } |
||
189 | } |
||
190 |
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: