-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
/languages
and /languages/{code}
endpo…
…ints (cherry picked from commit 674e612e176bc85556513a716556d643f81d722d)
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Rest\Server\Controller; | ||
|
||
use Ibexa\Contracts\Core\Repository\LanguageService; | ||
use Ibexa\Contracts\Core\Repository\Values\Content\Language as ApiLanguage; | ||
use Ibexa\Rest\Server\Controller as RestController; | ||
use Ibexa\Rest\Server\Values\LanguageList; | ||
use Traversable; | ||
|
||
final class Language extends RestController | ||
Check failure on line 17 in src/lib/Server/Controller/Language.php GitHub Actions / Unit tests (8.0)
Check failure on line 17 in src/lib/Server/Controller/Language.php GitHub Actions / Unit tests (8.2)
Check failure on line 17 in src/lib/Server/Controller/Language.php GitHub Actions / Unit tests (7.4)
|
||
{ | ||
private LanguageService $languageService; | ||
Check failure on line 19 in src/lib/Server/Controller/Language.php GitHub Actions / Unit tests (8.0)
Check failure on line 19 in src/lib/Server/Controller/Language.php GitHub Actions / Unit tests (8.2)
Check failure on line 19 in src/lib/Server/Controller/Language.php GitHub Actions / Unit tests (7.4)
|
||
|
||
public function __construct(LanguageService $languageService) | ||
Check failure on line 21 in src/lib/Server/Controller/Language.php GitHub Actions / Unit tests (8.0)
Check failure on line 21 in src/lib/Server/Controller/Language.php GitHub Actions / Unit tests (8.2)
Check failure on line 21 in src/lib/Server/Controller/Language.php GitHub Actions / Unit tests (7.4)
|
||
{ | ||
$this->languageService = $languageService; | ||
} | ||
|
||
public function listLanguages(): LanguageList | ||
{ | ||
$languages = $this->languageService->loadLanguages(); | ||
Check failure on line 28 in src/lib/Server/Controller/Language.php GitHub Actions / Unit tests (8.0)
Check failure on line 28 in src/lib/Server/Controller/Language.php GitHub Actions / Unit tests (8.2)
Check failure on line 28 in src/lib/Server/Controller/Language.php GitHub Actions / Unit tests (7.4)
|
||
|
||
if ($languages instanceof Traversable) { | ||
$languages = iterator_to_array($languages); | ||
} | ||
|
||
return new LanguageList($languages); | ||
} | ||
|
||
public function loadLanguage(string $languageCode): ApiLanguage | ||
Check failure on line 37 in src/lib/Server/Controller/Language.php GitHub Actions / Unit tests (8.0)
Check failure on line 37 in src/lib/Server/Controller/Language.php GitHub Actions / Unit tests (8.2)
Check failure on line 37 in src/lib/Server/Controller/Language.php GitHub Actions / Unit tests (7.4)
|
||
{ | ||
return $this->languageService->loadLanguage($languageCode); | ||
Check failure on line 39 in src/lib/Server/Controller/Language.php GitHub Actions / Unit tests (8.0)
Check failure on line 39 in src/lib/Server/Controller/Language.php GitHub Actions / Unit tests (8.2)
Check failure on line 39 in src/lib/Server/Controller/Language.php GitHub Actions / Unit tests (7.4)
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Rest\Server\Output\ValueObjectVisitor; | ||
|
||
use Ibexa\Contracts\Rest\Output\Generator; | ||
use Ibexa\Contracts\Rest\Output\ValueObjectVisitor; | ||
use Ibexa\Contracts\Rest\Output\Visitor; | ||
|
||
final class LanguageList extends ValueObjectVisitor | ||
Check failure on line 15 in src/lib/Server/Output/ValueObjectVisitor/LanguageList.php GitHub Actions / Unit tests (8.0)
Check failure on line 15 in src/lib/Server/Output/ValueObjectVisitor/LanguageList.php GitHub Actions / Unit tests (8.2)
Check failure on line 15 in src/lib/Server/Output/ValueObjectVisitor/LanguageList.php GitHub Actions / Unit tests (7.4)
|
||
{ | ||
/** | ||
* @param \Ibexa\Rest\Server\Values\LanguageList $data | ||
*/ | ||
public function visit(Visitor $visitor, Generator $generator, $data): void | ||
Check failure on line 20 in src/lib/Server/Output/ValueObjectVisitor/LanguageList.php GitHub Actions / Unit tests (8.0)
Check failure on line 20 in src/lib/Server/Output/ValueObjectVisitor/LanguageList.php GitHub Actions / Unit tests (8.0)
Check failure on line 20 in src/lib/Server/Output/ValueObjectVisitor/LanguageList.php GitHub Actions / Unit tests (8.2)
Check failure on line 20 in src/lib/Server/Output/ValueObjectVisitor/LanguageList.php GitHub Actions / Unit tests (8.2)
Check failure on line 20 in src/lib/Server/Output/ValueObjectVisitor/LanguageList.php GitHub Actions / Unit tests (7.4)
Check failure on line 20 in src/lib/Server/Output/ValueObjectVisitor/LanguageList.php GitHub Actions / Unit tests (7.4)
Check failure on line 20 in src/lib/Server/Output/ValueObjectVisitor/LanguageList.php GitHub Actions / Unit tests (7.3)
Check failure on line 20 in src/lib/Server/Output/ValueObjectVisitor/LanguageList.php GitHub Actions / Unit tests (7.3)
|
||
{ | ||
$generator->startObjectElement('LanguageList'); | ||
$visitor->setHeader('Content-Type', $generator->getMediaType('LanguageList')); | ||
|
||
$generator->attribute('href', $this->router->generate('ibexa.rest.languages.list')); | ||
|
||
$generator->startList('Language'); | ||
foreach ($data->languages as $language) { | ||
$visitor->visitValueObject($language); | ||
} | ||
$generator->endList('Language'); | ||
|
||
$generator->endObjectElement('LanguageList'); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Rest\Server\Values; | ||
|
||
use Ibexa\Rest\Value as RestValue; | ||
|
||
final class LanguageList extends RestValue | ||
{ | ||
/** @var \Ibexa\Contracts\Core\Repository\Values\Content\Language[] */ | ||
public array $languages; | ||
|
||
/** | ||
* @param array<\Ibexa\Contracts\Core\Repository\Values\Content\Language> $languages | ||
*/ | ||
public function __construct(array $languages) | ||
{ | ||
$this->languages = $languages; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"type": "object", | ||
"properties": { | ||
"Language": { | ||
"type": "object", | ||
"properties": { | ||
"_media-type": { | ||
"type": "string" | ||
}, | ||
"_href": { | ||
"type": "string" | ||
}, | ||
"languageId": { | ||
"type": "integer" | ||
}, | ||
"languageCode": { | ||
"type": "string", | ||
"minLength": 1, | ||
"pattern": "^[[:alnum:]_]+" | ||
}, | ||
"name": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"_media-type", | ||
"_href", | ||
"languageId", | ||
"languageCode", | ||
"name" | ||
] | ||
} | ||
}, | ||
"required": [ | ||
"Language" | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"type": "object", | ||
"properties": { | ||
"LanguageList": { | ||
"type": "object", | ||
"properties": { | ||
"_media-type": { | ||
"type": "string" | ||
}, | ||
"_href": { | ||
"type": "string" | ||
}, | ||
"Language": { | ||
"type":"array", | ||
"items": { | ||
"properties": { | ||
"_media-type": { | ||
"type": "string" | ||
}, | ||
"_href": { | ||
"type": "string" | ||
}, | ||
"languageId": { | ||
"type": "integer" | ||
}, | ||
"languageCode": { | ||
"type": "string", | ||
"minLength": 1, | ||
"pattern": "^[[:alnum:]_]+" | ||
}, | ||
"name": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"_media-type", | ||
"_href", | ||
"languageId", | ||
"languageCode", | ||
"name" | ||
] | ||
} | ||
} | ||
}, | ||
"required": [ | ||
"_media-type", | ||
"_href", | ||
"Language" | ||
] | ||
} | ||
}, | ||
"required": [ | ||
"LanguageList" | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Tests\Bundle\Rest\Functional; | ||
|
||
use Ibexa\Tests\Bundle\Rest\Functional\TestCase as RESTFunctionalTestCase; | ||
|
||
final class LanguageTest extends RESTFunctionalTestCase | ||
{ | ||
use ResourceAssertionsTrait; | ||
|
||
private const SNAPSHOT_DIR = __DIR__ . '/_snapshot'; | ||
|
||
public function testLanguageListJson(): void | ||
{ | ||
$request = $this->createHttpRequest('GET', '/api/ibexa/v2/languages', '', 'LanguageList+json'); | ||
$response = $this->sendHttpRequest($request); | ||
|
||
self::assertHttpResponseCodeEquals($response, 200); | ||
$content = $response->getBody()->getContents(); | ||
self::assertJson($content); | ||
|
||
self::assertJsonResponseIsValid($content, 'LanguageList'); | ||
self::assertResponseMatchesJsonSnapshot($content, self::SNAPSHOT_DIR . '/LanguageList.json'); | ||
} | ||
|
||
public function testLanguageListXml(): void | ||
{ | ||
$request = $this->createHttpRequest('GET', '/api/ibexa/v2/languages'); | ||
$response = $this->sendHttpRequest($request); | ||
|
||
self::assertHttpResponseCodeEquals($response, 200); | ||
$content = $response->getBody()->getContents(); | ||
self::assertResponseMatchesXmlSnapshot($content, self::SNAPSHOT_DIR . '/LanguageList.xml'); | ||
} | ||
|
||
public function testLanguageViewJson(): void | ||
{ | ||
$request = $this->createHttpRequest('GET', '/api/ibexa/v2/languages/eng-GB', '', 'LanguageList+json'); | ||
$response = $this->sendHttpRequest($request); | ||
|
||
self::assertHttpResponseCodeEquals($response, 200); | ||
$content = $response->getBody()->getContents(); | ||
self::assertJson($content); | ||
|
||
self::assertJsonResponseIsValid($content, 'Language'); | ||
self::assertResponseMatchesJsonSnapshot($content, self::SNAPSHOT_DIR . '/Language.json'); | ||
} | ||
|
||
public function testLanguageViewXml(): void | ||
{ | ||
$request = $this->createHttpRequest('GET', '/api/ibexa/v2/languages/eng-GB'); | ||
$response = $this->sendHttpRequest($request); | ||
|
||
self::assertHttpResponseCodeEquals($response, 200); | ||
$content = $response->getBody()->getContents(); | ||
self::assertResponseMatchesXmlSnapshot($content, self::SNAPSHOT_DIR . '/Language.xml'); | ||
} | ||
} |