Completed
Push — master ( be9a03...23390d )
by Julien
03:34 queued 01:09
created

Mapping::testMappingConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 13
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
     * testGetMappingKeys
63
     *
64
     * @access public
65
     * @return void
66
     */
67
    public function testGetMappingKeys()
68
    {
69
        $this
70
            ->given($this->newTestedInstance)
71
            ->then
72
                ->array($this->testedInstance->getMappingKeys())
73
                    ->isEmpty()
74
75
            ->given($this->newTestedInstance)
76
                ->and($this->testedInstance->setMapping($this->getMappingArray()))
77
            ->then
78
                ->array($this->testedInstance->getMappingKeys())
79
                    ->isEqualTo(['orders', 'order_items', 'clients'])
80
        ;
81
    }
82
83
    /**
84
     * testGetKeyFromId
85
     *
86
     * @access public
87
     * @return void
88
     */
89
    public function testGetKeyFromId()
90
    {
91
        $this
92
            // no mappings
93
            ->given($this->newTestedInstance)
94
            ->then($testedInstance = $this->testedInstance)
95
                ->exception(function () use ($testedInstance) {
96
                    $testedInstance->getKeyFromId('/orders/8');
97
                })
98
                ->isInstanceOf('Mapado\RestClientSdk\Exception\MappingException')
99
                ->hasMessage('orders key is not mapped')
100
101
            // good instances
102
            ->given($this->newTestedInstance)
103
                ->and($this->testedInstance->setMapping($this->getMappingArray()))
104
            ->then
105
                ->string($this->testedInstance->getKeyFromId('/orders/8'))
106
                    ->isEqualTo('orders')
107
108
            // a really complicated id
109
            ->given($this->newTestedInstance)
110
                ->and($this->testedInstance->setMapping($this->getMappingArray()))
111
            ->then
112
                ->string($this->testedInstance->getKeyFromId('/sales/customers/3/orders/8'))
113
                    ->isEqualTo('orders')
114
        ;
115
    }
116
117
    /**
118
     * testPrefix
119
     *
120
     * @access public
121
     * @return void
122
     */
123
    public function testPrefix()
124
    {
125
        $this
126
            // id prefix
127
            ->given($this->newTestedInstance('/v1'))
128
                ->and($this->testedInstance->setMapping($this->getMappingArray()))
129
            ->then
130
                ->string($this->testedInstance->getKeyFromId('/v1/orders/8'))
131
                    ->isEqualTo('orders')
132
        ;
133
    }
134
135
    /**
136
     * testGetKeyFromModel
137
     *
138
     * @access public
139
     * @return void
140
     */
141 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...
142
    {
143
        $this
144
            ->given($this->newTestedInstance)
145
                ->and($this->testedInstance->setMapping($this->getMappingArray()))
146
            ->then
147
                ->string($this->testedInstance->getKeyFromModel('Foo\Bar\Model\OrderItem'))
148
                    ->isEqualTo('order_items')
149
150
            ->then($testedInstance = $this->testedInstance)
151
            ->exception(function () use ($testedInstance) {
152
                $testedInstance->getKeyFromModel('\Not\Viable\Classname');
153
            })
154
                ->isInstanceOf('Mapado\RestClientSdk\Exception\MappingException')
155
                ->hasMessage('Model name \Not\Viable\Classname not found in mapping')
156
        ;
157
    }
158
159
    /**
160
     * testGetClassMetadata
161
     *
162
     * @access public
163
     * @return void
164
     */
165 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...
166
    {
167
        $this
168
            ->given($testedInstance = $this->newTestedInstance)
169
                ->and($this->testedInstance->setMapping($this->getMappingArray()))
170
            ->then
171
                ->object($this->testedInstance->getClassMetadata('Foo\Bar\Model\Order'))
172
                    ->isInstanceOf('Mapado\RestClientSdk\Mapping\ClassMetadata')
173
                ->object($this->testedInstance->getClassMetadata('Foo\Bar\Model\Client'))
174
                    ->isInstanceOf('Mapado\RestClientSdk\Mapping\ClassMetadata')
175
176
            ->then
177
            ->exception(function () use ($testedInstance) {
178
                $testedInstance->getClassMetadata('Foo\Bar');
179
            })
180
                    ->isInstanceOf('Mapado\RestClientSdk\Exception\MappingException')
181
                    ->hasMessage('Foo\Bar model is not mapped')
182
        ;
183
    }
184
185
    /**
186
     * testHasClassMetadata
187
     *
188
     * @access public
189
     * @return void
190
     */
191 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...
192
    {
193
        $this
194
            ->given($testedInstance = $this->newTestedInstance)
195
                ->and($this->testedInstance->setMapping($this->getMappingArray()))
196
            ->then
197
                ->boolean($this->testedInstance->hasClassMetadata('Foo\Bar\Model\Order'))
198
                    ->isTrue()
199
                ->boolean($this->testedInstance->hasClassMetadata('Foo\Bar\Model\Client'))
200
                    ->isTrue()
201
202
            ->then
203
                ->boolean($testedInstance->hasClassMetadata('Foo\Bar'))
204
                    ->isFalse()
205
        ;
206
    }
207
208
    public function testMappingConfiguration()
209
    {
210
        $this
211
            // default configuration
212
            ->given($this->newTestedInstance())
213
            ->then
214
                ->array($this->testedInstance->getConfig())
215
                    ->isEqualTo([
216
                        'collectionKey' => 'hydra:member',
217
                    ])
218
219
            // custom configuration
220
            ->given($config = [
221
                'collectionKey' => 'collection',
222
            ])
223
                ->and($this->newTestedInstance('', $config))
224
            ->then
225
                ->array($this->testedInstance->getConfig())
226
                    ->isEqualTo($config)
227
        ;
228
    }
229
230
    /**
231
     * getMappingArray
232
     *
233
     * @access private
234
     * @return ClassMetadata[]
235
     */
236
    private function getMappingArray()
237
    {
238
        $order = new ClassMetadata(
239
            'orders',
240
            'Foo\Bar\Model\Order',
241
            'Foo\Bar\Client\OrderClient'
242
        );
243
244
        $orderItem = new ClassMetadata(
245
            'order_items',
246
            'Foo\Bar\Model\OrderItem',
247
            'Foo\Bar\Client\OrderItemClient'
248
        );
249
250
        $client = new ClassMetadata(
251
            'clients',
252
            'Foo\Bar\Model\Client',
253
            'Foo\Bar\Client\ClientClient'
254
        );
255
256
        return [ $order, $orderItem, $client, ];
257
    }
258
}
259