Completed
Pull Request — master (#74)
by Julien
04:30
created

ModelHydrator::testConvertId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Mapado\RestClientSdk\Tests\Units\Model;
4
5
use atoum;
6
use Mapado\RestClientSdk\Mapping;
7
use Mapado\RestClientSdk\Mapping\Attribute;
8
use Mapado\RestClientSdk\Mapping\ClassMetadata;
9
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...
10
use Mapado\RestClientSdk\UnitOfWork;
11
12
/**
13
 * Class ModelHydrator
14
 *
15
 * @author Julien Deniau <[email protected]>
16
 */
17
class ModelHydrator extends atoum
18
{
19
    private $sdk;
20
21
    private $unitOfWork;
22
23
    public function beforeTestMethod($method)
24
    {
25
        $fooMetadata = new ClassMetadata(
26
            'foo',
27
            'Acme\Foo',
28
            ''
29
        );
30
31
        $mapping = new Mapping('/v1');
32
        $mapping->setMapping([
33
            $fooMetadata,
34
        ]);
35
36
        $this->unitOfWork = new UnitOfWork($mapping);
37
38
        $this->mockGenerator->orphanize('__construct');
39
        $this->mockGenerator->shuntParentClassCalls();
40
        $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...
41
        $this->calling($this->sdk)->getMapping = $mapping;
42
    }
43
44
    /**
45
     * testConvertId
46
     */
47
    public function testConvertId()
48
    {
49
        $this
50
            ->given($this->newTestedInstance($this->sdk))
51
            ->then
52
                ->string($this->testedInstance->convertId(2, 'Acme\Foo'))
53
                    ->isEqualTo('/v1/foo/2')
54
                ->string($this->testedInstance->convertId('/v1/foo/2', 'Acme\Foo'))
55
                    ->isEqualTo('/v1/foo/2')
56
        ;
57
    }
58
59
    /**
60
     * testConvertIdWithoutMappingPrefix
61
     */
62
    public function testConvertIdWithoutMappingPrefix()
63
    {
64
        $fooMetadata = new ClassMetadata(
65
            'foo',
66
            'Acme\Foo',
67
            ''
68
        );
69
70
        $mapping = new Mapping();
71
        $mapping->setMapping([
72
            $fooMetadata,
73
        ]);
74
75
        $this->mockGenerator->orphanize('__construct');
76
        $this->mockGenerator->shuntParentClassCalls();
77
        $sdk = new \mock\Mapado\RestClientSdk\SdkClient();
78
        $this->calling($sdk)->getMapping = $mapping;
79
        $this
80
            ->given($this->newTestedInstance($sdk))
81
            ->then
82
                ->string($this->testedInstance->convertId(2, 'Acme\Foo'))
83
                    ->isEqualTo('/foo/2')
84
                ->string($this->testedInstance->convertId('/foo/2', 'Acme\Foo'))
85
                    ->isEqualTo('/foo/2')
86
        ;
87
    }
88
89
    public function testHydrateJsonLdItem()
90
    {
91
        $productMetadata = new ClassMetadata(
92
            'product',
93
            'Mapado\RestClientSdk\Tests\Model\JsonLd\Product',
94
            'Mapado\RestClientSdk\Tests\Model\JsonLd\ModelRepository'
95
        );
96
        $productMetadata->setAttributeList([
97
            new Attribute('@id', 'id', 'string', true),
98
            new Attribute('value', 'value', 'string'),
99
            new Attribute('currency', 'currency', 'string'),
100
        ]);
101
102
        $mapping = new Mapping();
103
        $mapping->setMapping([
104
            $productMetadata,
105
        ]);
106
107
        $this->mockGenerator->orphanize('__construct');
108
        $this->mockGenerator->shuntParentClassCalls();
109
        $sdk = new \mock\Mapado\RestClientSdk\SdkClient();
110
        $this->calling($sdk)->getMapping = $mapping;
111
        $this->calling($sdk)->getSerializer = new Serializer($mapping, $this->unitOfWork);
112
113
        $this
114
            ->given($this->newTestedInstance($sdk))
115
            // test one json-ld entity
116
            ->and($productArray = json_decode(file_get_contents(__DIR__ . '/../../data/product.json-ld.json'), true))
117
            ->then
118
                ->object($product = $this->testedInstance->hydrate($productArray, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Product'))
119
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\JsonLd\Product')
120
                ->string($product->getId())
121
                    ->isEqualTo('/products/1')
122
123
            // test a json-ld list
124
            ->and($productListArray = json_decode(file_get_contents(__DIR__ . '/../../data/productList.json-ld.json'), true))
125
            ->then
126
                ->object($productList = $this->testedInstance->hydrateList($productListArray, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Product'))
127
                    ->isInstanceOf('Mapado\RestClientSdk\Collection\HydraPaginatedCollection')
128
                ->integer($productList->getTotalItems())
129
                    ->isEqualTo(2)
130
131
                ->object($product = $productList[0])
132
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\JsonLd\Product')
133
                ->string($product->getId())
134
                    ->isEqualTo('/products/1')
135
                ->string($productList[1]->getId())
136
                    ->isEqualTo('/products/2')
137
        ;
138
    }
139
140
    public function testHydrateHalItem()
141
    {
142
        $orderMetadata = new ClassMetadata(
143
            'order',
144
            'Mapado\RestClientSdk\Tests\Model\Hal\Order',
145
            'Mapado\RestClientSdk\Tests\Model\JsonLd\ModelRepository'
146
        );
147
        $orderMetadata->setAttributeList([
148
            new Attribute('_links.self.href', 'id', 'string', true),
149
            new Attribute('total', 'total', 'float'),
150
            new Attribute('currency', 'currency', 'string'),
151
            new Attribute('status', 'status', 'string'),
152
        ]);
153
154
        $mapping = new Mapping();
155
        $mapping->setConfig([
156
            'collectionKey' => '_embedded.ea:order',
157
        ]);
158
        $mapping->setMapping([$orderMetadata]);
159
160
        $this->mockGenerator->orphanize('__construct');
161
        $this->mockGenerator->shuntParentClassCalls();
162
        $sdk = new \mock\Mapado\RestClientSdk\SdkClient();
163
        $this->calling($sdk)->getMapping = $mapping;
164
        $this->calling($sdk)->getSerializer = new Serializer($mapping, $this->unitOfWork);
165
166
        $this
167
            ->given($this->newTestedInstance($sdk))
168
            // test one hal entity
169
            ->and($orderArray = json_decode(file_get_contents(__DIR__ . '/../../data/order.hal.json'), true))
170
            ->then
171
                ->object($order = $this->testedInstance->hydrate($orderArray, 'Mapado\RestClientSdk\Tests\Model\Hal\Order'))
172
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\Hal\Order')
173
                ->string($order->getStatus())
174
                    ->isEqualTo('shipped')
175
                ->string($order->getId())
176
                    ->isEqualTo('/orders/123')
177
178
            // test a json-ld list
179
            ->and($orderListArray = json_decode(file_get_contents(__DIR__ . '/../../data/orderList.hal.json'), true))
180
            ->then
181
                ->object($orderList = $this->testedInstance->hydrateList($orderListArray, 'Mapado\RestClientSdk\Tests\Model\Hal\Order'))
182
                    ->isInstanceOf('Mapado\RestClientSdk\Collection\HalCollection')
183
                ->integer($orderList->getTotalItems())
184
                    ->isEqualTo(2)
185
186
                ->object($order = $orderList[0])
187
                    ->isInstanceOf('Mapado\RestClientSdk\Tests\Model\Hal\Order')
188
                ->string($order->getId())
189
                    ->isEqualTo('/orders/123')
190
                ->string($orderList[1]->getId())
191
                    ->isEqualTo('/orders/124')
192
        ;
193
    }
194
}
195