Mapping   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 255
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 136
dl 0
loc 255
rs 10
c 0
b 0
f 0
wmc 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getMappingArray() 0 21 1
A testHasClassMetadata() 0 14 1
A testGetKeyFromId() 0 25 1
A testMappingConfiguration() 0 19 1
A testGetClassMetadata() 0 17 1
A testTryGetClassMetadataById() 0 26 1
A testGetModelName() 0 38 1
A testPrefix() 0 9 1
A testGetKeyFromModel() 0 15 1
A testGetMappingKeys() 0 13 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Mapado\RestClientSdk\Tests\Units;
6
7
use atoum;
8
use Mapado\RestClientSdk\Mapping\ClassMetadata;
9
10
/**
11
 * Class Mapping
12
 *
13
 * @author Julien Deniau <[email protected]>
14
 */
15
class Mapping extends atoum
16
{
17
    /**
18
     * testGetModelName
19
     */
20
    public function testGetModelName()
21
    {
22
        $this
23
            // no key given
24
            ->given($this->newTestedInstance)
25
                ->and($this->testedInstance->setMapping([new ClassMetadata('foo', 'foo', 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', 'foo', 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)]))
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 the model name is empty')
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
    public function testTryGetClassMetadataById()
65
    {
66
        $this
67
            // no mapping found
68
            ->given($this->newTestedInstance)
69
                ->and(
70
                    $this->testedInstance->setMapping(
71
                        [
72
                            $classMetadata = new ClassMetadata(
73
                                'bars',
74
                                'Foo\Entity\Bar',
75
                                'Foo\Repository\BarRepository'
76
                            ),
77
                        ]
78
                    )
79
                )
80
            ->when($result = $this->testedInstance->tryGetClassMetadataById('unknown'))
81
            ->then
82
                ->variable($result)
83
                    ->isNull()
84
85
            // model found
86
            ->when($result = $this->testedInstance->tryGetClassMetadataById('/bars/1234'))
87
            ->then
88
                ->variable($result)
89
                    ->isIdenticalTo($classMetadata)
90
        ;
91
    }
92
93
    /**
94
     * testGetMappingKeys
95
     */
96
    public function testGetMappingKeys()
97
    {
98
        $this
99
            ->given($this->newTestedInstance)
100
            ->then
101
                ->array($this->testedInstance->getMappingKeys())
102
                    ->isEmpty()
103
104
            ->given($this->newTestedInstance)
105
                ->and($this->testedInstance->setMapping($this->getMappingArray()))
106
            ->then
107
                ->array($this->testedInstance->getMappingKeys())
108
                    ->isEqualTo(['orders', 'order_items', 'clients'])
109
        ;
110
    }
111
112
    /**
113
     * testGetKeyFromId
114
     */
115
    public function testGetKeyFromId()
116
    {
117
        $this
118
            // no mappings
119
            ->given($this->newTestedInstance)
120
            ->then($testedInstance = $this->testedInstance)
121
                ->exception(function () use ($testedInstance) {
122
                    $testedInstance->getKeyFromId('/orders/8');
123
                })
124
                ->isInstanceOf('Mapado\RestClientSdk\Exception\MappingException')
125
                ->hasMessage('orders key is not mapped')
126
127
            // good instances
128
            ->given($this->newTestedInstance)
129
                ->and($this->testedInstance->setMapping($this->getMappingArray()))
130
            ->then
131
                ->string($this->testedInstance->getKeyFromId('/orders/8'))
132
                    ->isEqualTo('orders')
133
134
            // a really complicated id
135
            ->given($this->newTestedInstance)
136
                ->and($this->testedInstance->setMapping($this->getMappingArray()))
137
            ->then
138
                ->string($this->testedInstance->getKeyFromId('/sales/customers/3/orders/8'))
139
                    ->isEqualTo('orders')
140
        ;
141
    }
142
143
    /**
144
     * testPrefix
145
     */
146
    public function testPrefix()
147
    {
148
        $this
149
            // id prefix
150
            ->given($this->newTestedInstance('/v1'))
151
                ->and($this->testedInstance->setMapping($this->getMappingArray()))
152
            ->then
153
                ->string($this->testedInstance->getKeyFromId('/v1/orders/8'))
154
                    ->isEqualTo('orders')
155
        ;
156
    }
157
158
    /**
159
     * testGetKeyFromModel
160
     */
161
    public function testGetKeyFromModel()
162
    {
163
        $this
164
            ->given($this->newTestedInstance)
165
                ->and($this->testedInstance->setMapping($this->getMappingArray()))
166
            ->then
167
                ->string($this->testedInstance->getKeyFromModel('Foo\Bar\Model\OrderItem'))
168
                    ->isEqualTo('order_items')
169
170
            ->then($testedInstance = $this->testedInstance)
171
            ->exception(function () use ($testedInstance) {
172
                $testedInstance->getKeyFromModel('\Not\Viable\Classname');
173
            })
174
                ->isInstanceOf('Mapado\RestClientSdk\Exception\MappingException')
175
                ->hasMessage('Model name \Not\Viable\Classname not found in mapping')
176
        ;
177
    }
178
179
    /**
180
     * testGetClassMetadata
181
     */
182
    public function testGetClassMetadata()
183
    {
184
        $this
185
            ->given($testedInstance = $this->newTestedInstance)
186
                ->and($this->testedInstance->setMapping($this->getMappingArray()))
187
            ->then
188
                ->object($this->testedInstance->getClassMetadata('Foo\Bar\Model\Order'))
189
                    ->isInstanceOf('Mapado\RestClientSdk\Mapping\ClassMetadata')
190
                ->object($this->testedInstance->getClassMetadata('Foo\Bar\Model\Client'))
191
                    ->isInstanceOf('Mapado\RestClientSdk\Mapping\ClassMetadata')
192
193
            ->then
194
            ->exception(function () use ($testedInstance) {
195
                $testedInstance->getClassMetadata('Foo\Bar');
196
            })
197
                    ->isInstanceOf('Mapado\RestClientSdk\Exception\MappingException')
198
                    ->hasMessage('Foo\Bar model is not mapped')
199
        ;
200
    }
201
202
    /**
203
     * testHasClassMetadata
204
     */
205
    public function testHasClassMetadata()
206
    {
207
        $this
208
            ->given($testedInstance = $this->newTestedInstance)
209
                ->and($this->testedInstance->setMapping($this->getMappingArray()))
210
            ->then
211
                ->boolean($this->testedInstance->hasClassMetadata('Foo\Bar\Model\Order'))
212
                    ->isTrue()
213
                ->boolean($this->testedInstance->hasClassMetadata('Foo\Bar\Model\Client'))
214
                    ->isTrue()
215
216
            ->then
217
                ->boolean($testedInstance->hasClassMetadata('Foo\Bar'))
218
                    ->isFalse()
219
        ;
220
    }
221
222
    public function testMappingConfiguration()
223
    {
224
        $this
225
            // default configuration
226
            ->given($this->newTestedInstance())
227
            ->then
228
                ->array($this->testedInstance->getConfig())
229
                    ->isEqualTo([
230
                        'collectionKey' => 'hydra:member',
231
                    ])
232
233
            // custom configuration
234
            ->given($config = [
235
                'collectionKey' => 'collection',
236
            ])
237
                ->and($this->newTestedInstance('', $config))
238
            ->then
239
                ->array($this->testedInstance->getConfig())
240
                    ->isEqualTo($config)
241
        ;
242
    }
243
244
    /**
245
     * getMappingArray
246
     *
247
     * @return ClassMetadata[]
248
     */
249
    private function getMappingArray()
250
    {
251
        $order = new ClassMetadata(
252
            'orders',
253
            'Foo\Bar\Model\Order',
254
            'Foo\Bar\Client\OrderClient'
255
        );
256
257
        $orderItem = new ClassMetadata(
258
            'order_items',
259
            'Foo\Bar\Model\OrderItem',
260
            'Foo\Bar\Client\OrderItemClient'
261
        );
262
263
        $client = new ClassMetadata(
264
            'clients',
265
            'Foo\Bar\Model\Client',
266
            'Foo\Bar\Client\ClientClient'
267
        );
268
269
        return [$order, $orderItem, $client];
270
    }
271
}
272