Completed
Push — ezp26297-rest_embedding_http_c... ( 6dcd26...7b4207 )
by
unknown
44:13 queued 12:57
created

LocationTest::parseLocationFromResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the Functional\LocationTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 *
9
 * @version //autogentag//
10
 */
11
namespace eZ\Bundle\EzPublishRestBundle\Tests\Functional;
12
13
use Buzz\Message\Response;
14
use eZ\Bundle\EzPublishRestBundle\Tests\Functional\TestCase as RESTFunctionalTestCase;
15
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
16
use eZ\Publish\Core\Repository\Values\Content\Location;
17
use eZ\Publish\API\Repository\Values\Content\LocationList;
18
19
class LocationTest extends RESTFunctionalTestCase
20
{
21
    /**
22
     * @covers POST /content/objects/{contentId}/locations
23
     * @returns string location href
24
     */
25
    public function testCreateLocation()
26
    {
27
        $content = $this->createFolder('testCreateLocation', '/api/ezp/v2/content/locations/1/2');
28
        $contentHref = $content['_href'];
29
30
        $remoteId = $this->addTestSuffix('testCreatelocation');
31
32
        $body = <<< XML
33
<?xml version="1.0" encoding="UTF-8"?>
34
<LocationCreate>
35
  <ParentLocation href="/api/ezp/v2/content/locations/1/43" />
36
  <remoteId>{$remoteId}</remoteId>
37
  <priority>0</priority>
38
  <hidden>false</hidden>
39
  <sortField>PATH</sortField>
40
  <sortOrder>ASC</sortOrder>
41
</LocationCreate>
42
XML;
43
        $request = $this->createHttpRequest('POST', "$contentHref/locations", 'LocationCreate+xml', 'Location+json');
44
        $request->setContent($body);
45
46
        $response = $this->sendHttpRequest($request);
47
        self::assertHttpResponseCodeEquals($response, 201);
48
        self::assertHttpResponseHasHeader($response, 'Location');
49
50
        $href = $response->getHeader('Location');
51
52
        return $href;
53
    }
54
55
    /**
56
     * @depends testCreateLocation
57
     * @covers GET /content/locations?remoteId=<locationRemoteId>
58
     */
59 View Code Duplication
    public function testRedirectLocationByRemoteId($locationHref)
60
    {
61
        $response = $this->sendHttpRequest(
62
            $this->createHttpRequest('GET', '/api/ezp/v2/content/locations?remoteId=' . $this->addTestSuffix('testCreateLocation'))
63
        );
64
65
        self::assertHttpResponseCodeEquals($response, 307);
66
        self::assertHttpResponseHasHeader($response, 'Location', $locationHref);
67
    }
68
69
    /**
70
     * @depends testCreateLocation
71
     * @covers GET /content/locations?id=<locationId>
72
     */
73
    public function testRedirectLocationById($locationHref)
74
    {
75
        $hrefParts = explode('/', $locationHref);
76
        $id = array_pop($hrefParts);
77
        $response = $this->sendHttpRequest(
78
            $this->createHttpRequest('GET', "/api/ezp/v2/content/locations?id=$id")
79
        );
80
81
        self::assertHttpResponseCodeEquals($response, 307);
82
        self::assertHttpResponseHasHeader($response, 'Location', $locationHref);
83
    }
84
85
    /**
86
     * @depends testCreateLocation
87
     * @covers GET /content/locations/{locationPath}
88
     */
89
    public function testLoadLocation($locationHref)
90
    {
91
        $response = $this->sendHttpRequest(
92
            $this->createHttpRequest('GET', $locationHref, '', 'Location+json')
93
        );
94
95
        self::assertHttpResponseCodeEquals($response, 200);
96
97
        $responseLocation = $this->parseLocationFromResponse($response);
98
99
        $this->assertHttpResponseHasCacheTags(
100
            $response,
101
            array_merge(
102
                [
103
                    'location-' . $responseLocation->id,
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\Content\Location. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
104
                    'content-' . $responseLocation->getContentInfo()->id,
105
                    'content-type-' . $responseLocation->getContentInfo()->contentTypeId,
106
                ],
107
                array_map(
108
                    function ($id) {
109
                        return 'path-' . $id;
110
                    },
111
                    $responseLocation->path
0 ignored issues
show
Documentation introduced by
The property $path is declared protected in eZ\Publish\Core\Repository\Values\Content\Location. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
112
                )
113
            )
114
        );
115
    }
116
117
    /**
118
     * @depends testCreateLocation
119
     * @covers COPY /content/locations/{locationPath}
120
     *
121
     * @return string the created location's href
122
     */
123
    public function testCopySubtree($locationHref)
124
    {
125
        $request = $this->createHttpRequest('COPY', $locationHref);
126
        $request->addHeader('Destination: /api/ezp/v2/content/locations/1/43');
127
        $response = $this->sendHttpRequest($request);
128
129
        self::assertHttpResponseCodeEquals($response, 201);
130
        self::assertHttpResponseHasHeader($response, 'Location');
131
132
        return $response->getHeader('Location');
133
    }
134
135
    /**
136
     * @covers MOVE /content/locations/{locationPath}
137
     * @depends testCopySubtree
138
     */
139 View Code Duplication
    public function testMoveSubtree($locationHref)
140
    {
141
        $request = $this->createHttpRequest('MOVE', $locationHref);
142
        $request->addHeader('Destination: /api/ezp/v2/content/locations/1/5');
143
        $response = $this->sendHttpRequest($request);
144
145
        self::assertHttpResponseCodeEquals($response, 201);
146
        self::assertHttpResponseHasHeader($response, 'Location');
147
    }
148
149
    /**
150
     * @depends testCreateLocation
151
     * @covers GET /content/objects/{contentId}/locations
152
     */
153
    public function testLoadLocationsForContent($contentHref)
0 ignored issues
show
Unused Code introduced by
The parameter $contentHref is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
154
    {
155
        self::markTestSkipped('@todo implement');
156
    }
157
158
    /**
159
     * @depends testCreateLocation
160
     * @covers SWAP /content/locations/{locationPath}
161
     */
162
    public function testSwapLocation($locationHref)
0 ignored issues
show
Unused Code introduced by
The parameter $locationHref is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
163
    {
164
        self::markTestSkipped('@todo Implement');
165
166
        /*$content = $this->createFolder( __FUNCTION__, "/api/ezp/v2/content/locations/1/2" );
167
168
        $request = $this->createHttpRequest( 'SWAP', $locationHref );
169
        $request->addHeader( "Destination: $newFolderHref" );
170
171
        $response = $this->sendHttpRequest( $request );
172
        self::assertHttpResponseCodeEquals( $response, 204 );*/
173
    }
174
175
    /**
176
     * @depends testCreateLocation
177
     * @covers GET /content/locations/{locationPath}/children
178
     */
179
    public function testLoadLocationChildren($locationHref)
180
    {
181
        for ($i = 0; $i < 2; ++$i) {
182
            $this->createFolder('Child ' . $i, $locationHref);
183
        }
184
185
        $response = $this->sendHttpRequest(
186
            $this->createHttpRequest('GET', "$locationHref/children", '', 'LocationList+json')
187
        );
188
189
        self::assertHttpResponseCodeEquals($response, 200);
190
        self::assertHttpResponseHasHeader($response, 'Content-Type', $this->generateMediaTypeString('LocationList+json'));
191
192
        $locationList = $this->parseLocationListFromResponse($response);
193
194
        self::assertEquals(2, $locationList->totalCount);
195
196
        $this->assertHttpResponseHasCacheTags(
197
            $response,
198
            array_map(
199
                function (Location  $location) {
200
                    return 'location-' . $location->id;
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\Content\Location. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
201
                },
202
                $locationList->locations
203
            )
204
        );
205
    }
206
207
    /**
208
     * @covers PATCH /content/locations/{locationPath}
209
     * @depends testCreateLocation
210
     */
211
    public function testUpdateLocation($locationHref)
212
    {
213
        $body = <<< XML
214
<LocationUpdate>
215
  <priority>3</priority>
216
  <sortField>PATH</sortField>
217
  <sortOrder>ASC</sortOrder>
218
</LocationUpdate>
219
XML;
220
221
        $request = $this->createHttpRequest('PATCH', $locationHref, 'LocationUpdate+xml', 'Location+json');
222
        $request->setContent($body);
223
224
        $response = $this->sendHttpRequest($request);
225
226
        self::assertHttpResponseCodeEquals($response, 200);
227
    }
228
229
    /**
230
     * @depends testCreateLocation
231
     * @covers DELETE /content/locations/{path}
232
     */
233
    public function testDeleteSubtree($locationHref)
234
    {
235
        $response = $this->sendHttpRequest(
236
            $this->createHttpRequest('DELETE', $locationHref)
237
        );
238
239
        self::assertHttpResponseCodeEquals($response, 204);
240
    }
241
242
/**
243
 * @param \Buzz\Message\Response
244
 * @return \eZ\Publish\API\Repository\Values\Content\Location
245
 */private function parseLocationFromResponse(Response $response)
246
{
247
    $jsonStruct = $this->parseJsonResponse($response);
248
249
    return new Location(
250
            [
251
                'id' => $this->extractLastIdFromHref($jsonStruct['Location']['_href']),
252
                'path' => explode('/', trim($jsonStruct['Location']['pathString'], '/')),
253
                'contentInfo' => new ContentInfo(
254
                    [
255
                        'id' => $this->extractLastIdFromHref($jsonStruct['Location']['ContentInfo']['_href']),
256
                    ]
257
                ),
258
            ]
259
        );
260
}
261
262
/**
263
 * @return \eZ\Publish\API\Repository\Values\Content\LocationList
264
 */private function parseLocationListFromResponse($response)
265
{
266
    $jsonStruct = $this->parseJsonResponse($response);
267
268
    return new LocationList(
269
            [
270
                'locations' => array_map(
271
                    function ($location) {
272
                        return new Location(
273
                            [
274
                                'id' => $this->extractLastIdFromHref($location['_href']),
275
                            ]
276
                        );
277
                    },
278
                    $jsonStruct['LocationList']['Location']
279
                ),
280
                'totalCount' => count($jsonStruct['LocationList']['Location']),
281
            ],
282
            ''
283
        );
284
}
285
286
    /**
287
     * @param Response $response
288
     * @return array
289
     */
290
    private function parseJsonResponse(Response $response)
291
    {
292
        return json_decode($response->getContent(), true);
293
    }
294
}
295