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\DoctrineMongoDBAdminBundle\Tests\Builder; |
15
|
|
|
|
16
|
|
|
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; |
17
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
18
|
|
|
use PHPUnit\Framework\TestCase; |
19
|
|
|
use Sonata\AdminBundle\Admin\AdminInterface; |
20
|
|
|
use Sonata\AdminBundle\Admin\FieldDescriptionCollection; |
21
|
|
|
use Sonata\AdminBundle\Datagrid\Datagrid; |
22
|
|
|
use Sonata\AdminBundle\Datagrid\DatagridInterface; |
23
|
|
|
use Sonata\AdminBundle\Datagrid\Pager; |
24
|
|
|
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface; |
25
|
|
|
use Sonata\AdminBundle\Filter\FilterFactoryInterface; |
26
|
|
|
use Sonata\AdminBundle\Guesser\TypeGuesserInterface; |
27
|
|
|
use Sonata\AdminBundle\Translator\FormLabelTranslatorStrategy; |
28
|
|
|
use Sonata\DoctrineMongoDBAdminBundle\Admin\FieldDescription; |
29
|
|
|
use Sonata\DoctrineMongoDBAdminBundle\Builder\DatagridBuilder; |
30
|
|
|
use Sonata\DoctrineMongoDBAdminBundle\Filter\ModelFilter; |
31
|
|
|
use Sonata\DoctrineMongoDBAdminBundle\Model\ModelManager; |
32
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
33
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
34
|
|
|
use Symfony\Component\Form\Guess\Guess; |
35
|
|
|
use Symfony\Component\Form\Guess\TypeGuess; |
36
|
|
|
|
37
|
|
|
final class DatagridBuilderTest extends TestCase |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* @var DatagridBuilder |
41
|
|
|
*/ |
42
|
|
|
private $datagridBuilder; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var MockObject&TypeGuesserInterface |
46
|
|
|
*/ |
47
|
|
|
private $typeGuesser; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var MockObject&FormFactoryInterface |
51
|
|
|
*/ |
52
|
|
|
private $formFactory; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var MockObject&FilterFactoryInterface |
56
|
|
|
*/ |
57
|
|
|
private $filterFactory; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var MockObject&AdminInterface |
61
|
|
|
*/ |
62
|
|
|
private $admin; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var MockObject&ModelManager |
66
|
|
|
*/ |
67
|
|
|
private $modelManager; |
68
|
|
|
|
69
|
|
|
protected function setUp(): void |
70
|
|
|
{ |
71
|
|
|
$this->formFactory = $this->createStub(FormFactoryInterface::class); |
72
|
|
|
$this->filterFactory = $this->createStub(FilterFactoryInterface::class); |
73
|
|
|
$this->typeGuesser = $this->createStub(TypeGuesserInterface::class); |
74
|
|
|
|
75
|
|
|
$this->datagridBuilder = new DatagridBuilder( |
76
|
|
|
$this->formFactory, |
77
|
|
|
$this->filterFactory, |
78
|
|
|
$this->typeGuesser |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
$this->admin = $this->createMock(AdminInterface::class); |
82
|
|
|
$this->modelManager = $this->createMock(ModelManager::class); |
83
|
|
|
|
84
|
|
|
$this->admin |
85
|
|
|
->method('getClass') |
86
|
|
|
->willReturn('FakeClass'); |
87
|
|
|
$this->admin |
88
|
|
|
->method('getModelManager') |
89
|
|
|
->willReturn($this->modelManager); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testGetBaseDatagrid(): void |
93
|
|
|
{ |
94
|
|
|
$proxyQuery = $this->createStub(ProxyQueryInterface::class); |
95
|
|
|
$fieldDescription = $this->createStub(FieldDescriptionCollection::class); |
96
|
|
|
$formBuilder = $this->createStub(FormBuilderInterface::class); |
97
|
|
|
|
98
|
|
|
$this->admin->method('createQuery')->willReturn($proxyQuery); |
99
|
|
|
$this->admin->method('getList')->willReturn($fieldDescription); |
100
|
|
|
|
101
|
|
|
$this->modelManager->method('getIdentifierFieldNames')->willReturn(['id']); |
102
|
|
|
|
103
|
|
|
$this->formFactory->method('createNamedBuilder')->willReturn($formBuilder); |
104
|
|
|
|
105
|
|
|
$this->assertInstanceOf( |
106
|
|
|
Datagrid::class, |
107
|
|
|
$datagrid = $this->datagridBuilder->getBaseDatagrid($this->admin) |
108
|
|
|
); |
109
|
|
|
$this->assertInstanceOf(Pager::class, $datagrid->getPager()); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function testFixFieldDescription(): void |
113
|
|
|
{ |
114
|
|
|
$classMetadata = $this->createMock(ClassMetadata::class); |
115
|
|
|
|
116
|
|
|
$fieldDescription = new FieldDescription(); |
117
|
|
|
$fieldDescription->setName('test'); |
118
|
|
|
$fieldDescription->setMappingType(ClassMetadata::ONE); |
119
|
|
|
|
120
|
|
|
$this->admin |
121
|
|
|
->expects($this->once()) |
122
|
|
|
->method('attachAdminClass'); |
123
|
|
|
|
124
|
|
|
$this->modelManager->method('hasMetadata')->willReturn(true); |
125
|
|
|
|
126
|
|
|
$this->modelManager |
127
|
|
|
->expects($this->once()) |
128
|
|
|
->method('getParentMetadataForProperty') |
129
|
|
|
->willReturn([$classMetadata, 'someField', $parentAssociationMapping = []]); |
130
|
|
|
|
131
|
|
|
$this->datagridBuilder->fixFieldDescription($this->admin, $fieldDescription); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function testAddFilterNoType(): void |
135
|
|
|
{ |
136
|
|
|
$this->admin |
137
|
|
|
->expects($this->once()) |
138
|
|
|
->method('addFilterFieldDescription'); |
139
|
|
|
|
140
|
|
|
$datagrid = $this->createMock(DatagridInterface::class); |
141
|
|
|
$guessType = new TypeGuess(ModelFilter::class, [ |
142
|
|
|
'name' => 'value', |
143
|
|
|
], Guess::VERY_HIGH_CONFIDENCE); |
144
|
|
|
|
145
|
|
|
$fieldDescription = new FieldDescription(); |
146
|
|
|
$fieldDescription->setName('test'); |
147
|
|
|
|
148
|
|
|
$this->typeGuesser->method('guessType')->willReturn($guessType); |
149
|
|
|
|
150
|
|
|
$this->modelManager |
151
|
|
|
->expects($this->once()) |
152
|
|
|
->method('hasMetadata')->willReturn(false); |
153
|
|
|
|
154
|
|
|
$this->admin->method('getCode')->willReturn('someFakeCode'); |
155
|
|
|
|
156
|
|
|
$this->filterFactory->method('create')->willReturn(new ModelFilter()); |
157
|
|
|
|
158
|
|
|
$this->admin->method('getLabelTranslatorStrategy')->willReturn(new FormLabelTranslatorStrategy()); |
159
|
|
|
|
160
|
|
|
$datagrid |
161
|
|
|
->expects($this->once()) |
162
|
|
|
->method('addFilter') |
163
|
|
|
->with($this->isInstanceOf(ModelFilter::class)); |
164
|
|
|
|
165
|
|
|
$this->datagridBuilder->addFilter( |
166
|
|
|
$datagrid, |
167
|
|
|
null, |
168
|
|
|
$fieldDescription, |
169
|
|
|
$this->admin |
170
|
|
|
); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|