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
|
|
|
|
23
|
|
|
class ListBuilderTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var ListBuilder |
27
|
|
|
*/ |
28
|
|
|
private $lb; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Admin |
32
|
|
|
*/ |
33
|
|
|
private $admin; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var ModelManager |
37
|
|
|
*/ |
38
|
|
|
private $modelManager; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var FieldDescriptionInterface |
42
|
|
|
*/ |
43
|
|
|
private $fieldDescription; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var FieldDescriptionCollection |
47
|
|
|
*/ |
48
|
|
|
private $fieldDescriptionCollection; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var TypeGuesserInterface |
52
|
|
|
*/ |
53
|
|
|
private $guesser; |
54
|
|
|
|
55
|
|
|
public function setUp() |
56
|
|
|
{ |
57
|
|
|
$this->guesser = $this->createMock('\Sonata\AdminBundle\Guesser\TypeGuesserInterface', [], []); |
58
|
|
|
$this->templates = []; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testGetBaseList() |
62
|
|
|
{ |
63
|
|
|
$lb = new ListBuilder($this->guesser, $this->templates); |
64
|
|
|
$this->assertInstanceOf('Sonata\AdminBundle\Admin\FieldDescriptionCollection', $lb->getBaseList()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testAddField() |
68
|
|
|
{ |
69
|
|
|
$this->setupAddField(); |
70
|
|
|
$this->lb->addField($this->fieldDescriptionCollection, 'string', $this->fieldDescription, $this->admin); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testAddFieldNullType() |
74
|
|
|
{ |
75
|
|
|
$typeguess = $this->createMock('Symfony\Component\Form\Guess\TypeGuess', [], [], '', false); |
76
|
|
|
$this->guesser->expects($this->once()) |
|
|
|
|
77
|
|
|
->method('guessType') |
78
|
|
|
->with($this->anything()) |
79
|
|
|
->will($this->returnValue($typeguess)); |
80
|
|
|
$this->setupAddField(); |
81
|
|
|
$this->lb->addField($this->fieldDescriptionCollection, null, $this->fieldDescription, $this->admin); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testAddListActionField() |
85
|
|
|
{ |
86
|
|
|
$this->setUpListActionTests(); |
87
|
|
|
|
88
|
|
|
$fieldDescription = new FieldDescription(); |
89
|
|
|
$fieldDescription->setName('foo'); |
90
|
|
|
$list = $this->listBuilder->getBaseList(); |
91
|
|
|
$this->listBuilder |
92
|
|
|
->addField($list, 'actions', $fieldDescription, $this->admin); |
93
|
|
|
|
94
|
|
|
$this->assertSame( |
95
|
|
|
'@SonataAdmin/CRUD/list__action.html.twig', |
96
|
|
|
$list->get('foo')->getTemplate(), |
97
|
|
|
'Custom list action field has a default list action template assigned' |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function testCorrectFixedActionsFieldType() |
102
|
|
|
{ |
103
|
|
|
$this->setUpListActionTests(); |
104
|
|
|
$this->guesser->expects($this->once())->method('guessType')->willReturn(null); |
|
|
|
|
105
|
|
|
|
106
|
|
|
$fieldDescription = new FieldDescription(); |
107
|
|
|
$fieldDescription->setName('_action'); |
108
|
|
|
$list = $this->listBuilder->getBaseList(); |
109
|
|
|
$this->listBuilder->addField($list, null, $fieldDescription, $this->admin); |
110
|
|
|
|
111
|
|
|
$this->assertSame( |
112
|
|
|
'actions', |
113
|
|
|
$list->get('_action')->getType(), |
114
|
|
|
'Standard list _action field has "actions" type' |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
//public function testAddField() |
119
|
|
|
//{ |
120
|
|
|
// $fieldDescriptionCollection = $this->createMock('\Sonata\AdminBundle\Admin\FieldDescriptionCollection', [], []); |
121
|
|
|
// $fieldDescription = $this->createMock('\Sonata\AdminBundle\Admin\FieldDescriptionInterface', [], []); |
122
|
|
|
// $admin = $this->createMock('\Sonata\AdminBundle\Admin\AdminInterface', [], []); |
123
|
|
|
// $lb = new ListBuilder($this->guesser, $this->templates); |
124
|
|
|
|
125
|
|
|
// $lb->addField($fieldDescriptionCollection, 'sometype', $fieldDescription, $admin); |
126
|
|
|
|
127
|
|
|
//} |
128
|
|
|
|
129
|
|
|
protected function setUpListActionTests() |
130
|
|
|
{ |
131
|
|
|
$this->metaData = $this->createMock('\Doctrine\ODM\PHPCR\Mapping\ClassMetadata'); |
132
|
|
|
$this->modelManager = $this->createMock('Sonata\DoctrinePHPCRAdminBundle\Model\ModelManager'); |
133
|
|
|
$this->modelManager->expects($this->any()) |
134
|
|
|
->method('getMetadata') |
135
|
|
|
->will($this->returnValue($this->metaData)); |
136
|
|
|
$this->modelManager->expects($this->any()) |
137
|
|
|
->method('hasMetadata') |
138
|
|
|
->with($this->anything()) |
139
|
|
|
->will($this->returnValue(true)); |
140
|
|
|
|
141
|
|
|
$this->admin = $this->createMock(Admin::class, [], [], '', false); |
142
|
|
|
$this->admin->expects($this->atLeastOnce())->method('getModelManager') |
143
|
|
|
->willReturn($this->modelManager); |
144
|
|
|
|
145
|
|
|
$this->listBuilder = new ListBuilder($this->guesser); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
private function setupAddField() |
149
|
|
|
{ |
150
|
|
|
$this->lb = new ListBuilder($this->guesser, $this->templates); |
151
|
|
|
$this->metaData = $this->createMock('\Doctrine\ODM\PHPCR\Mapping\ClassMetadata', [], [], '', false); |
152
|
|
|
$this->modelManager = $this->createMock('\Sonata\DoctrinePHPCRAdminBundle\Model\ModelManager'); |
153
|
|
|
$this->modelManager->expects($this->any()) |
154
|
|
|
->method('getMetadata') |
155
|
|
|
->will($this->returnValue($this->metaData)); |
156
|
|
|
$this->modelManager->expects($this->any()) |
157
|
|
|
->method('hasMetadata') |
158
|
|
|
->with($this->anything()) |
159
|
|
|
->will($this->returnValue(true)); |
160
|
|
|
|
161
|
|
|
$this->fieldDescription = $this->createMock('\Sonata\AdminBundle\Admin\FieldDescriptionInterface'); |
162
|
|
|
$this->fieldDescription->expects($this->any()) |
163
|
|
|
->method('getType') |
164
|
|
|
->will($this->returnValue('string')); |
165
|
|
|
$this->fieldDescription->expects($this->once()) |
166
|
|
|
->method('setType') |
167
|
|
|
->with($this->anything()); |
168
|
|
|
|
169
|
|
|
//AdminInterface doesn't implement methods called in addField, |
170
|
|
|
//so we mock Admin |
171
|
|
|
$this->admin = $this->createMock('\Sonata\AdminBundle\Admin\AbstractAdmin', [], [], '', false); |
172
|
|
|
$this->admin->expects($this->any()) |
173
|
|
|
->method('getModelManager') |
174
|
|
|
->will($this->returnValue($this->modelManager)); |
175
|
|
|
$this->admin->expects($this->once()) |
176
|
|
|
->method('addListFieldDescription') |
177
|
|
|
->with($this->anything(), $this->fieldDescription); |
178
|
|
|
|
179
|
|
|
$this->fieldDescriptionCollection = $this->createMock('\Sonata\AdminBundle\Admin\FieldDescriptionCollection'); |
180
|
|
|
$this->fieldDescriptionCollection->expects($this->once()) |
181
|
|
|
->method('add') |
182
|
|
|
->with($this->fieldDescription); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.