Completed
Pull Request — master (#47)
by Eric
32:49
created

ResourceTest   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 277
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 23
c 3
b 0
f 0
lcom 1
cbo 2
dl 0
loc 277
rs 10

23 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 21 1
A testInheritance() 0 4 1
A testDefaultState() 0 19 1
B testInitialState() 0 40 1
A testDriver() 0 6 1
A testDriverManager() 0 6 1
A testDriverMappingPath() 0 6 1
A testDriverMappingFormat() 0 6 1
A testModel() 0 6 1
A testFactory() 0 6 1
A testRepository() 0 6 1
A testForm() 0 6 1
A testChoiceForm() 0 6 1
A testDomainManager() 0 6 1
A testController() 0 6 1
A testIdPropertyPath() 0 6 1
A testLabelPropertyPath() 0 6 1
A createFactoryClassMock() 0 4 1
A createRepositoryClassMock() 0 4 1
A createFormClassMock() 0 4 1
A createControllerClassMock() 0 4 1
A createDomainManagerClassMock() 0 4 1
A createResourceMock() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the Lug package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lug\Component\Resource\Tests\Model;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Lug\Component\Resource\Domain\DomainManagerInterface;
16
use Lug\Component\Resource\Factory\FactoryInterface;
17
use Lug\Component\Resource\Model\Resource;
18
use Lug\Component\Resource\Model\ResourceInterface;
19
use Lug\Component\Resource\Repository\RepositoryInterface;
20
use Symfony\Component\Form\FormInterface;
21
22
/**
23
 * @author GeLo <[email protected]>
24
 */
25
class ResourceTest extends \PHPUnit_Framework_TestCase
26
{
27
    /**
28
     * @var Resource
29
     */
30
    private $resource;
31
32
    /**
33
     * @var string
34
     */
35
    private $driver;
36
37
    /**
38
     * @var string
39
     */
40
    private $driverManager;
41
42
    /**
43
     * @var string
44
     */
45
    private $driverMappingPath;
46
47
    /**
48
     * @var string
49
     */
50
    private $driverMappingFormat;
51
52
    /**
53
     * @var string
54
     */
55
    private $name;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
56
57
    /**
58
     * @var string[]
59
     */
60
    private $interfaces;
61
62
    /**
63
     * @var string
64
     */
65
    private $model;
66
67
    /**
68
     * @var string
69
     */
70
    private $repository;
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    protected function setUp()
76
    {
77
        $this->name = 'name';
78
        $this->driver = ResourceInterface::DRIVER_DOCTRINE_MONGODB;
79
        $this->driverManager = 'my.manager';
80
        $this->driverMappingPath = __DIR__;
81
        $this->driverMappingFormat = ResourceInterface::DRIVER_MAPPING_FORMAT_XML;
82
        $this->interfaces = [\ArrayAccess::class, \Countable::class];
0 ignored issues
show
Documentation Bug introduced by
It seems like array(\ArrayAccess::class, \Countable::class) of type array<integer,?> is incompatible with the declared type array<integer,string> of property $interfaces.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
83
        $this->model = \ArrayIterator::class;
84
85
        $this->resource = new Resource(
86
            $this->name,
87
            $this->driver,
88
            $this->driverManager,
89
            $this->driverMappingPath,
90
            $this->driverMappingFormat,
91
            $this->interfaces,
92
            $this->model,
93
            $this->repository
94
        );
95
    }
96
97
    public function testInheritance()
98
    {
99
        $this->assertInstanceOf(ResourceInterface::class, $this->resource);
100
    }
101
102
    public function testDefaultState()
103
    {
104
        $this->assertSame($this->name, $this->resource->getName());
105
        $this->assertSame($this->driver, $this->resource->getDriver());
106
        $this->assertSame($this->driverManager, $this->resource->getDriverManager());
107
        $this->assertSame($this->driverMappingPath, $this->resource->getDriverMappingPath());
108
        $this->assertSame($this->driverMappingFormat, $this->resource->getDriverMappingFormat());
109
        $this->assertSame($this->interfaces, $this->resource->getInterfaces());
110
        $this->assertSame($this->model, $this->resource->getModel());
111
        $this->assertSame($this->repository, $this->resource->getRepository());
112
        $this->assertNull($this->resource->getFactory());
113
        $this->assertNull($this->resource->getForm());
114
        $this->assertNull($this->resource->getChoiceForm());
115
        $this->assertNull($this->resource->getDomainManager());
116
        $this->assertNull($this->resource->getController());
117
        $this->assertNull($this->resource->getIdPropertyPath());
118
        $this->assertNull($this->resource->getLabelPropertyPath());
119
        $this->assertNull($this->resource->getTranslation());
120
    }
121
122
    public function testInitialState()
123
    {
124
        $translation = $this->createResourceMock();
125
126
        $this->resource = new Resource(
127
            $this->name,
128
            $this->driver,
129
            $this->driverManager,
130
            $this->driverMappingPath,
131
            $this->driverMappingFormat,
132
            $this->interfaces,
133
            $this->model,
134
            $this->repository,
135
            $factory = $this->createFactoryClassMock(),
136
            $form = $this->createFormClassMock(),
137
            $choiceForm = $this->createFormClassMock(),
138
            $domainManager = $this->createDomainManagerClassMock(),
139
            $controller = $this->createControllerClassMock(),
140
            $idPropertyPath = 'id',
141
            $labelPropertyPath = 'label',
142
            $translation
143
        );
144
145
        $this->assertSame($this->name, $this->resource->getName());
146
        $this->assertSame($this->driver, $this->resource->getDriver());
147
        $this->assertSame($this->driverManager, $this->resource->getDriverManager());
148
        $this->assertSame($this->driverMappingPath, $this->resource->getDriverMappingPath());
149
        $this->assertSame($this->driverMappingFormat, $this->resource->getDriverMappingFormat());
150
        $this->assertSame($this->interfaces, $this->resource->getInterfaces());
151
        $this->assertSame($this->model, $this->resource->getModel());
152
        $this->assertSame($this->repository, $this->resource->getRepository());
153
        $this->assertSame($factory, $this->resource->getFactory());
154
        $this->assertSame($form, $this->resource->getForm());
155
        $this->assertSame($choiceForm, $this->resource->getChoiceForm());
156
        $this->assertSame($domainManager, $this->resource->getDomainManager());
157
        $this->assertSame($controller, $this->resource->getController());
158
        $this->assertSame($idPropertyPath, $this->resource->getIdPropertyPath());
159
        $this->assertSame($labelPropertyPath, $this->resource->getLabelPropertyPath());
160
        $this->assertSame($translation, $this->resource->getTranslation());
161
    }
162
163
    public function testDriver()
164
    {
165
        $this->resource->setDriver($driver = ResourceInterface::DRIVER_DOCTRINE_ORM);
166
167
        $this->assertSame($driver, $this->resource->getDriver());
168
    }
169
170
    public function testDriverManager()
171
    {
172
        $this->resource->setDriverManager($driverManager = 'default');
173
174
        $this->assertSame($driverManager, $this->resource->getDriverManager());
175
    }
176
177
    public function testDriverMappingPath()
178
    {
179
        $this->resource->setDriverMappingPath($driverMappingPath = __DIR__.'/default');
180
181
        $this->assertSame($driverMappingPath, $this->resource->getDriverMappingPath());
182
    }
183
184
    public function testDriverMappingFormat()
185
    {
186
        $this->resource->setDriverMappingFormat($driverMappingFormat = ResourceInterface::DRIVER_MAPPING_FORMAT_YAML);
187
188
        $this->assertSame($driverMappingFormat, $this->resource->getDriverMappingFormat());
189
    }
190
191
    public function testModel()
192
    {
193
        $this->resource->setModel($model = ArrayCollection::class);
194
195
        $this->assertSame($model, $this->resource->getModel());
196
    }
197
198
    public function testFactory()
199
    {
200
        $this->resource->setFactory($factory = $this->createFactoryClassMock());
201
202
        $this->assertSame($factory, $this->resource->getFactory());
203
    }
204
205
    public function testRepository()
206
    {
207
        $this->resource->setRepository($repository = $this->createRepositoryClassMock());
208
209
        $this->assertSame($repository, $this->resource->getRepository());
210
    }
211
212
    public function testForm()
213
    {
214
        $this->resource->setForm($form = $this->createFormClassMock());
215
216
        $this->assertSame($form, $this->resource->getForm());
217
    }
218
219
    public function testChoiceForm()
220
    {
221
        $this->resource->setChoiceForm($choiceForm = $this->createFormClassMock());
222
223
        $this->assertSame($choiceForm, $this->resource->getChoiceForm());
224
    }
225
226
    public function testDomainManager()
227
    {
228
        $this->resource->setDomainManager($domainManager = $this->createDomainManagerClassMock());
229
230
        $this->assertSame($domainManager, $this->resource->getDomainManager());
231
    }
232
233
    public function testController()
234
    {
235
        $this->resource->setController($controller = $this->createControllerClassMock());
236
237
        $this->assertSame($controller, $this->resource->getController());
238
    }
239
240
    public function testIdPropertyPath()
241
    {
242
        $this->resource->setIdPropertyPath($idPropertyPath = 'id');
243
244
        $this->assertSame($idPropertyPath, $this->resource->getIdPropertyPath());
245
    }
246
247
    public function testLabelPropertyPath()
248
    {
249
        $this->resource->setLabelPropertyPath($labelPropertyPath = 'label');
250
251
        $this->assertSame($labelPropertyPath, $this->resource->getLabelPropertyPath());
252
    }
253
254
    /**
255
     * @return string
256
     */
257
    private function createFactoryClassMock()
258
    {
259
        return $this->getMockClass(FactoryInterface::class);
260
    }
261
262
    /**
263
     * @return string
264
     */
265
    private function createRepositoryClassMock()
266
    {
267
        return $this->getMockClass(RepositoryInterface::class);
268
    }
269
270
    /**
271
     * @return string
272
     */
273
    private function createFormClassMock()
274
    {
275
        return $this->getMockClass(FormInterface::class);
276
    }
277
278
    /**
279
     * @return string
280
     */
281
    private function createControllerClassMock()
282
    {
283
        return $this->getMockClass(\stdClass::class);
284
    }
285
286
    /**
287
     * @return \PHPUnit_Framework_MockObject_MockObject|DomainManagerInterface
288
     */
289
    private function createDomainManagerClassMock()
290
    {
291
        return $this->createMock(DomainManagerInterface::class);
292
    }
293
294
    /**
295
     * @return \PHPUnit_Framework_MockObject_MockObject|ResourceInterface
296
     */
297
    private function createResourceMock()
298
    {
299
        return $this->createMock(ResourceInterface::class);
300
    }
301
}
302