Skip to content

Commit

Permalink
Implemented code review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
kisztof committed Sep 12, 2023
1 parent 261cced commit b439c49
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 42 deletions.
3 changes: 1 addition & 2 deletions src/lib/Server/Output/ValueObjectVisitor/ImageVariation.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ImageVariation extends ValueObjectVisitor
*/
public function visit(Visitor $visitor, Generator $generator, $data)
{
$visitor->setHeader('Content-Type', $generator->getMediaType('ContentImageVariation'));
$generator->startObjectElement('ContentImageVariation');
$this->visitImageVariationAttributes($visitor, $generator, $data);
$generator->endObjectElement('ContentImageVariation');
Expand Down Expand Up @@ -59,7 +60,5 @@ protected function visitImageVariationAttributes(Visitor $visitor, Generator $ge
$generator->startValueElement('fileSize', $data->fileSize);
$generator->endValueElement('fileSize');
}

$visitor->setHeader('Content-Type', $generator->getMediaType('ContentImageVariation'));
}
}
62 changes: 23 additions & 39 deletions tests/bundle/Functional/ImageVariationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,26 @@ final class ImageVariationTest extends RESTFunctionalTestCase
public function testCreateContent(): string
{
$string = $this->addTestSuffix(__FUNCTION__);
$fileName = basename('1px.png');
$fileName = '1px.png';
$fileSize = 4718;
$fileData = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==';

$restPrefixPath = '/api/ezp/v2';
$body = <<< XML
<?xml version="1.0" encoding="UTF-8"?>
<ContentCreate>
<ContentType href="/api/ezp/v2/content/types/5" />
<ContentType href="{$restPrefixPath}/content/types/5" />
<mainLanguageCode>eng-GB</mainLanguageCode>
<LocationCreate>
<ParentLocation href="/api/ezp/v2/content/locations/1/2" />
<ParentLocation href="{$restPrefixPath}/content/locations/1/2" />
<priority>0</priority>
<hidden>false</hidden>
<sortField>PATH</sortField>
<sortOrder>ASC</sortOrder>
</LocationCreate>
<Section href="/api/ezp/v2/content/sections/3" />
<Section href="{$restPrefixPath}/content/sections/3" />
<alwaysAvailable>true</alwaysAvailable>
<remoteId>{$string}</remoteId>
<User href="/api/ezp/v2/user/users/14" />
<User href="{$restPrefixPath}/user/users/14" />
<modificationDate>2012-09-30T12:30:00</modificationDate>
<fields>
<field>
Expand All @@ -57,77 +57,61 @@ public function testCreateContent(): string
XML;
$request = $this->createHttpRequest(
'POST',
'/api/ezp/v2/content/objects',
$restPrefixPath . '/content/objects',
'ContentCreate+xml',
'ContentInfo+json',
$body
);

$response = $this->sendHttpRequest($request);

self::assertEquals('201', $response->getStatusCode());
self::assertHttpResponseHasHeader($response, 'Location');
self::assertHttpResponseHasHeader($response, 'content-type', 'application/vnd.ez.api.ContentInfo+json');
$this->assertHttpResponseCodeEquals($response, Response::HTTP_CREATED);
$this->assertHttpResponseHasHeader($response, 'Location');
$this->assertHttpResponseHasHeader($response, 'content-type', 'application/vnd.ez.api.ContentInfo+json');

$href = $response->getHeader('Location')[0];
$this->addCreatedElement($href);

return $href;
}
$contentInfo = json_decode((string)$response->getBody(), true, 512, JSON_THROW_ON_ERROR);

/**
* @depends testCreateContent
*/
public function testPublishContent(string $restContentHref): string
{
$response = $this->sendHttpRequest(
$this->createHttpRequest('PUBLISH', sprintf('%s/versions/1', $restContentHref))
return sprintf(
'%s/%d', $contentInfo['Content']['Versions']['_href'], $contentInfo['Content']['currentVersionNo']
);
self::assertHttpResponseCodeEquals($response, 204);

return $restContentHref;
}

/**
* @depends testCreateContent
*/
public function testLoadContent(string $restContentHref): void
public function testPublishContent(string $restContentHref): string
{
$response = $this->sendHttpRequest(
$this->createHttpRequest(
'GET',
$restContentHref,
'',
'Version+json'
)
$this->createHttpRequest('PUBLISH', $restContentHref)
);
$this->assertHttpResponseCodeEquals($response, Response::HTTP_NO_CONTENT);

self::assertHttpResponseCodeEquals($response, 200);
self::assertArrayHasKey('content-type', $response->getHeaders());
self::assertHttpResponseHasHeader($response, 'content-type', 'application/vnd.ez.api.ContentInfo+json');
return $restContentHref;
}

/**
* @depends testPublishContent
*/
public function testGetImageVariation(string $hrefToImage): void
public function testGetImageVariation(string $restVersionHref): void
{
$restContentHref = $hrefToImage;
$imageResponse = $this->sendHttpRequest(
$this->createHttpRequest(
'GET',
$restContentHref . '/versions/1',
$restVersionHref,
'',
'Version+json'
)
);

$jsonResponse = json_decode((string)$imageResponse->getBody());
$imageField = $jsonResponse->Version->Fields->field[2];
$jsonResponse = json_decode((string)$imageResponse->getBody(), true, 512, JSON_THROW_ON_ERROR);
$imageField = $jsonResponse['Version']['Fields']['field'][2];

self::assertObjectHasAttribute('variations', $imageField->fieldValue);
self::assertArrayHasKey('variations', $imageField['fieldValue']);

$variationUrl = $imageField->fieldValue->variations->medium->href;
$variationUrl = $imageField['fieldValue']['variations']['medium']['href'];

$variationResponse = $this->sendHttpRequest(
$this->createHttpRequest(
Expand Down
2 changes: 1 addition & 1 deletion tests/bundle/Functional/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ protected function setUp(): void
{
parent::setUp();

$this->httpHost = getenv('EZP_TEST_REST_HOST') ?: 'localhost';
$this->httpHost = getenv('EZP_TEST_REST_HOST') ?: '127.0.0.1:8000';
$this->httpScheme = getenv('EZP_TEST_REST_SCHEME') ?: 'http';
$this->httpAuth = getenv('EZP_TEST_REST_AUTH') ?: 'admin:publish';
[$this->loginUsername, $this->loginPassword] = explode(':', $this->httpAuth);
Expand Down

0 comments on commit b439c49

Please sign in to comment.