Completed
Pull Request — master (#56)
by
unknown
02:10
created

Mapping::testTryGetClassMetadataById()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 17
nc 1
nop 0
1
<?php
2
3
namespace Mapado\RestClientSdk\Tests\Units;
4
5
use atoum;
6
use Mapado\RestClientSdk\Mapping\ClassMetadata;
7
8
/**
9
 * Class Mapping
10
 * @author Julien Deniau <[email protected]>
11
 */
12
class Mapping extends atoum
13
{
14
    /**
15
     * testGetModelName
16
     *
17
     * @access public
18
     * @return void
19
     */
20
    public function testGetModelName()
21
    {
22
        $this
23
            // no key given
24
            ->given($this->newTestedInstance)
25
                ->and($this->testedInstance->setMapping([new ClassMetadata('foo', null, null)]))
26
            ->then($testedInstance = $this->testedInstance)
27
            ->exception(function () use ($testedInstance) {
28
                $testedInstance->getModelName('');
29
            })
30
                ->isInstanceOf('Mapado\RestClientSdk\Exception\MappingException')
31
                ->hasMessage('key is not set')
32
33
            // no mapping found
34
            ->given($this->newTestedInstance)
35
                ->and($this->testedInstance->setMapping([new ClassMetadata('foo', null, null)]))
36
            ->then($testedInstance = $this->testedInstance)
37
            ->exception(function () use ($testedInstance) {
38
                $testedInstance->getModelName('orders');
39
            })
40
                ->isInstanceOf('Mapado\RestClientSdk\Exception\MappingException')
41
                ->hasMessage('orders key is not mapped')
42
43
            // wrong mapping array
44
            ->given($this->newTestedInstance)
45
            ->and($this->testedInstance->setMapping([new ClassMetadata('orders', null, null)]))
46
            ->then($testedInstance = $this->testedInstance)
47
            ->exception(function () use ($testedInstance) {
48
                $testedInstance->getModelName('orders');
49
            })
50
                ->isInstanceOf('Mapado\RestClientSdk\Exception\MappingException')
51
                ->hasMessage('orders key is mapped but no modelName found')
52
53
            // model found
54
            ->given($this->newTestedInstance)
55
                ->and($this->testedInstance->setMapping($this->getMappingArray()))
56
            ->string($this->testedInstance->getModelName('orders'))
57
                ->isEqualTo('Foo\Bar\Model\Order')
58
        ;
59
    }
60
61
    /**
62
     * testTryGetClassMetadataById
63
     *
64
     * @access public
65
     * @return void
66
     */
67
    public function testTryGetClassMetadataById()
68
    {
69
        $this
70
            // no mapping found
71
            ->given($this->newTestedInstance)
72
                ->and(
73
                    $this->testedInstance->setMapping(
74
                        [
75
                            $classMetadata = new ClassMetadata(
76
                                'bars',
77
                                'Foo\Entity\Bar',
78
                                'Foo\Repository\BarRepository'
79
                            )
80
                        ]
81
                    )
82
                )
83
            ->when($result = $this->testedInstance->tryGetClassMetadataById('unknown'))
84
            ->then
85
                ->variable($result)
86
                    ->isNull()
87
88
            // model found
89
            ->when($result = $this->testedInstance->tryGetClassMetadataById('/bars/1234'))
90
            ->then
91
                ->variable($result)
92
                    ->isIdenticalTo($classMetadata)
93
        ;
94
    }
95
96
    /**
97
     * testGetMappingKeys
98
     *
99
     * @access public
100
     * @return void
101
     */
102
    public function testGetMappingKeys()
103
    {
104
        $this
105
            ->given($this->newTestedInstance)
106
            ->then
107
                ->array($this->testedInstance->getMappingKeys())
108
                    ->isEmpty()
109
110
            ->given($this->newTestedInstance)
111
                ->and($this->testedInstance->setMapping($this->getMappingArray()))
112
            ->then
113
                ->array($this->testedInstance->getMappingKeys())
114
                    ->isEqualTo(['orders', 'order_items', 'clients'])
115
        ;
116
    }
117
118
    /**
119
     * testGetKeyFromId
120
     *
121
     * @access public
122
     * @return void
123
     */
124
    public function testGetKeyFromId()
125
    {
126
        $this
127
            // no mappings
128
            ->given($this->newTestedInstance)
129
            ->then($testedInstance = $this->testedInstance)
130
                ->exception(function () use ($testedInstance) {
131
                    $testedInstance->getKeyFromId('/orders/8');
132
                })
133
                ->isInstanceOf('Mapado\RestClientSdk\Exception\MappingException')
134
                ->hasMessage('orders key is not mapped')
135
136
            // good instances
137
            ->given($this->newTestedInstance)
138
                ->and($this->testedInstance->setMapping($this->getMappingArray()))
139
            ->then
140
                ->string($this->testedInstance->getKeyFromId('/orders/8'))
141
                    ->isEqualTo('orders')
142
143
            // a really complicated id
144
            ->given($this->newTestedInstance)
145
                ->and($this->testedInstance->setMapping($this->getMappingArray()))
146
            ->then
147
                ->string($this->testedInstance->getKeyFromId('/sales/customers/3/orders/8'))
148
                    ->isEqualTo('orders')
149
        ;
150
    }
151
152
    /**
153
     * testPrefix
154
     *
155
     * @access public
156
     * @return void
157
     */
158
    public function testPrefix()
159
    {
160
        $this
161
            // id prefix
162
            ->given($this->newTestedInstance('/v1'))
163
                ->and($this->testedInstance->setMapping($this->getMappingArray()))
164
            ->then
165
                ->string($this->testedInstance->getKeyFromId('/v1/orders/8'))
166
                    ->isEqualTo('orders')
167
        ;
168
    }
169
170
    /**
171
     * testGetKeyFromModel
172
     *
173
     * @access public
174
     * @return void
175
     */
176 View Code Duplication
    public function testGetKeyFromModel()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
177
    {
178
        $this
179
            ->given($this->newTestedInstance)
180
                ->and($this->testedInstance->setMapping($this->getMappingArray()))
181
            ->then
182
                ->string($this->testedInstance->getKeyFromModel('Foo\Bar\Model\OrderItem'))
183
                    ->isEqualTo('order_items')
184
185
            ->then($testedInstance = $this->testedInstance)
186
            ->exception(function () use ($testedInstance) {
187
                $testedInstance->getKeyFromModel('\Not\Viable\Classname');
188
            })
189
                ->isInstanceOf('Mapado\RestClientSdk\Exception\MappingException')
190
                ->hasMessage('Model name \Not\Viable\Classname not found in mapping')
191
        ;
192
    }
193
194
    /**
195
     * testGetClassMetadata
196
     *
197
     * @access public
198
     * @return void
199
     */
200 View Code Duplication
    public function testGetClassMetadata()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
201
    {
202
        $this
203
            ->given($testedInstance = $this->newTestedInstance)
204
                ->and($this->testedInstance->setMapping($this->getMappingArray()))
205
            ->then
206
                ->object($this->testedInstance->getClassMetadata('Foo\Bar\Model\Order'))
207
                    ->isInstanceOf('Mapado\RestClientSdk\Mapping\ClassMetadata')
208
                ->object($this->testedInstance->getClassMetadata('Foo\Bar\Model\Client'))
209
                    ->isInstanceOf('Mapado\RestClientSdk\Mapping\ClassMetadata')
210
211
            ->then
212
            ->exception(function () use ($testedInstance) {
213
                $testedInstance->getClassMetadata('Foo\Bar');
214
            })
215
                    ->isInstanceOf('Mapado\RestClientSdk\Exception\MappingException')
216
                    ->hasMessage('Foo\Bar model is not mapped')
217
        ;
218
    }
219
220
    /**
221
     * testHasClassMetadata
222
     *
223
     * @access public
224
     * @return void
225
     */
226 View Code Duplication
    public function testHasClassMetadata()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
227
    {
228
        $this
229
            ->given($testedInstance = $this->newTestedInstance)
230
                ->and($this->testedInstance->setMapping($this->getMappingArray()))
231
            ->then
232
                ->boolean($this->testedInstance->hasClassMetadata('Foo\Bar\Model\Order'))
233
                    ->isTrue()
234
                ->boolean($this->testedInstance->hasClassMetadata('Foo\Bar\Model\Client'))
235
                    ->isTrue()
236
237
            ->then
238
                ->boolean($testedInstance->hasClassMetadata('Foo\Bar'))
239
                    ->isFalse()
240
        ;
241
    }
242
243
    public function testMappingConfiguration()
244
    {
245
        $this
246
            // default configuration
247
            ->given($this->newTestedInstance())
248
            ->then
249
                ->array($this->testedInstance->getConfig())
250
                    ->isEqualTo([
251
                        'collectionKey' => 'hydra:member',
252
                    ])
253
254
            // custom configuration
255
            ->given($config = [
256
                'collectionKey' => 'collection',
257
            ])
258
                ->and($this->newTestedInstance('', $config))
259
            ->then
260
                ->array($this->testedInstance->getConfig())
261
                    ->isEqualTo($config)
262
        ;
263
    }
264
265
    /**
266
     * getMappingArray
267
     *
268
     * @access private
269
     * @return ClassMetadata[]
270
     */
271
    private function getMappingArray()
272
    {
273
        $order = new ClassMetadata(
274
            'orders',
275
            'Foo\Bar\Model\Order',
276
            'Foo\Bar\Client\OrderClient'
277
        );
278
279
        $orderItem = new ClassMetadata(
280
            'order_items',
281
            'Foo\Bar\Model\OrderItem',
282
            'Foo\Bar\Client\OrderItemClient'
283
        );
284
285
        $client = new ClassMetadata(
286
            'clients',
287
            'Foo\Bar\Model\Client',
288
            'Foo\Bar\Client\ClientClient'
289
        );
290
291
        return [ $order, $orderItem, $client, ];
292
    }
293
}
294