ModelHydrator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
eloc 116
c 1
b 1
f 1
dl 0
loc 175
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testHydrateJsonLdItem() 0 48 1
A testConvertIdWithoutMappingPrefix() 0 24 1
A testHydrateHalItem() 0 52 1
A testConvertId() 0 9 1
A beforeTestMethod() 0 19 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Mapado\RestClientSdk\Tests\Units\Model;
6
7
use atoum;
8
use Mapado\RestClientSdk\Mapping;
9
use Mapado\RestClientSdk\Mapping\Attribute;
10
use Mapado\RestClientSdk\Mapping\ClassMetadata;
11
use Mapado\RestClientSdk\Model\Serializer;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Mapado\RestClientSdk\Tests\Units\Model\Serializer. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
12
use Mapado\RestClientSdk\UnitOfWork;
13
14
/**
15
 * Class ModelHydrator
16
 *
17
 * @author Julien Deniau <[email protected]>
18
 */
19
class ModelHydrator extends atoum
20
{
21
    private $sdk;
22
23
    private $unitOfWork;
24
25
    public function beforeTestMethod($method)
26
    {
27
        $fooMetadata = new ClassMetadata(
28
            'foo',
29
            'Acme\Foo',
30
            ''
31
        );
32
33
        $mapping = new Mapping('/v1');
34
        $mapping->setMapping([
35
            $fooMetadata,
36
        ]);
37
38
        $this->unitOfWork = new UnitOfWork($mapping);
39
40
        $this->mockGenerator->orphanize('__construct');
41
        $this->mockGenerator->shuntParentClassCalls();
42
        $this->sdk = new \mock\Mapado\RestClientSdk\SdkClient();
0 ignored issues
show
Bug introduced by
The type mock\Mapado\RestClientSdk\SdkClient was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
43
        $this->calling($this->sdk)->getMapping = $mapping;
44
    }
45
46
    /**
47
     * testConvertId
48
     */
49
    public function testConvertId()
50
    {
51
        $this
52
            ->given($this->newTestedInstance($this->sdk))
53
            ->then
54
                ->string($this->testedInstance->convertId(2, 'Acme\Foo'))
55
                    ->isEqualTo('/v1/foo/2')
56
                ->string($this->testedInstance->convertId('/v1/foo/2', 'Acme\Foo'))
57
                    ->isEqualTo('/v1/foo/2')
58
        ;
59
    }
60
61
    /**
62
     * testConvertIdWithoutMappingPrefix
63
     */
64
    public function testConvertIdWithoutMappingPrefix()
65
    {
66
        $fooMetadata = new ClassMetadata(
67
            'foo',
68
            'Acme\Foo',
69
            ''
70
        );
71
72
        $mapping = new Mapping();
73
        $mapping->setMapping([
74
            $fooMetadata,
75
        ]);
76
77
        $this->mockGenerator->orphanize('__construct');
78
        $this->mockGenerator->shuntParentClassCalls();
79
        $sdk = new \mock\Mapado\RestClientSdk\SdkClient();
80
        $this->calling($sdk)->getMapping = $mapping;
81
        $this
82
            ->given($this->newTestedInstance($sdk))
83
            ->then
84
                ->string($this->testedInstance->convertId(2, 'Acme\Foo'))
85
                    ->isEqualTo('/foo/2')
86
                ->string($this->testedInstance->convertId('/foo/2', 'Acme\Foo'))
87
                    ->isEqualTo('/foo/2')
88
        ;
89
    }
90
91
    public function testHydrateJsonLdItem()
92
    {
93
        $productMetadata = new ClassMetadata(
94
            'product',
95
            'Mapado\RestClientSdk\Tests\Model\JsonLd\Product',
96
            'Mapado\RestClientSdk\Tests\Model\JsonLd\ModelRepository'
97
        );
98
        $productMetadata->setAttributeList([
99
            new Attribute('@id', 'id', 'string', true),
100
            new Attribute('value', 'value', 'string'),
101
            new Attribute('currency', 'currency', 'string'),
102
        ]);
103
104
        $mapping = new Mapping();
105
        $mapping->setMapping([
106
            $productMetadata,
107
        ]);
108
109
        $this->mockGenerator->orphanize('__construct');
110
        $this->mockGenerator->shuntParentClassCalls();
111
        $sdk = new \mock\Mapado\RestClientSdk\SdkClient();
112
        $this->calling($sdk)->getMapping = $mapping;
113
        $this->calling($sdk)->getSerializer = new Serializer($mapping, $this->unitOfWork);
114
115
        $this
116
            ->given($this->newTestedInstance($sdk))
117
            // test one json-ld entity
118
            ->and($productArray = json_decode(file_get_contents(__DIR__ . '/../../data/product.json-ld.json'), true))
119
            ->then
120
                ->object($product = $this->testedInstance->hydrate($productArray, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Product'))
121
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\JsonLd\Product')
122
                ->string($product->getId())
123
                    ->isEqualTo('/products/1')
124
125
            // test a json-ld list
126
            ->and($productListArray = json_decode(file_get_contents(__DIR__ . '/../../data/productList.json-ld.json'), true))
127
            ->then
128
                ->object($productList = $this->testedInstance->hydrateList($productListArray, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Product'))
129
                    ->isInstanceOf('Mapado\RestClientSdk\Collection\HydraPaginatedCollection')
130
                ->integer($productList->getTotalItems())
131
                    ->isEqualTo(2)
132
133
                ->object($product = $productList[0])
134
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\JsonLd\Product')
135
                ->string($product->getId())
136
                    ->isEqualTo('/products/1')
137
                ->string($productList[1]->getId())
138
                    ->isEqualTo('/products/2')
139
        ;
140
    }
141
142
    public function testHydrateHalItem()
143
    {
144
        $orderMetadata = new ClassMetadata(
145
            'order',
146
            'Mapado\RestClientSdk\Tests\Model\Hal\Order',
147
            'Mapado\RestClientSdk\Tests\Model\JsonLd\ModelRepository'
148
        );
149
        $orderMetadata->setAttributeList([
150
            new Attribute('_links.self.href', 'id', 'string', true),
151
            new Attribute('total', 'total', 'float'),
152
            new Attribute('currency', 'currency', 'string'),
153
            new Attribute('status', 'status', 'string'),
154
        ]);
155
156
        $mapping = new Mapping();
157
        $mapping->setConfig([
158
            'collectionKey' => '_embedded.ea:order',
159
        ]);
160
        $mapping->setMapping([$orderMetadata]);
161
162
        $this->mockGenerator->orphanize('__construct');
163
        $this->mockGenerator->shuntParentClassCalls();
164
        $sdk = new \mock\Mapado\RestClientSdk\SdkClient();
165
        $this->calling($sdk)->getMapping = $mapping;
166
        $this->calling($sdk)->getSerializer = new Serializer($mapping, $this->unitOfWork);
167
168
        $this
169
            ->given($this->newTestedInstance($sdk))
170
            // test one hal entity
171
            ->and($orderArray = json_decode(file_get_contents(__DIR__ . '/../../data/order.hal.json'), true))
172
            ->then
173
                ->object($order = $this->testedInstance->hydrate($orderArray, 'Mapado\RestClientSdk\Tests\Model\Hal\Order'))
174
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\Hal\Order')
175
                ->string($order->getStatus())
176
                    ->isEqualTo('shipped')
177
                ->string($order->getId())
178
                    ->isEqualTo('/orders/123')
179
180
            // test a json-ld list
181
            ->and($orderListArray = json_decode(file_get_contents(__DIR__ . '/../../data/orderList.hal.json'), true))
182
            ->then
183
                ->object($orderList = $this->testedInstance->hydrateList($orderListArray, 'Mapado\RestClientSdk\Tests\Model\Hal\Order'))
184
                    ->isInstanceOf('Mapado\RestClientSdk\Collection\HalCollection')
185
                ->integer($orderList->getTotalItems())
186
                    ->isEqualTo(2)
187
188
                ->object($order = $orderList[0])
189
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\Hal\Order')
190
                ->string($order->getId())
191
                    ->isEqualTo('/orders/123')
192
                ->string($orderList[1]->getId())
193
                    ->isEqualTo('/orders/124')
194
        ;
195
    }
196
}
197