Skip to content

Commit f6250bd

Browse files
committed
IBX-XXXX: Add tests
1 parent 429b9b8 commit f6250bd

File tree

8 files changed

+595
-1
lines changed

8 files changed

+595
-1
lines changed

tests/bundle/Functional/ContentTypeTest.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,36 @@
1010

1111
class ContentTypeTest extends RESTFunctionalTestCase
1212
{
13+
public function testCreateView(): void
14+
{
15+
$body = <<< XML
16+
<?xml version="1.0" encoding="UTF-8"?>
17+
<ViewInput>
18+
<identifier>ContentTypeView</identifier>
19+
<ContentTypeQuery>
20+
<Query>
21+
<ContentTypeIdentifierCriterion>folder</ContentTypeIdentifierCriterion>
22+
</Query>
23+
<limit>10</limit>
24+
<offset>0</offset>
25+
</ContentTypeQuery>
26+
</ViewInput>
27+
XML;
28+
$request = $this->createHttpRequest(
29+
'POST',
30+
'/api/ibexa/v2/content/types/view',
31+
'ContentTypeViewInput+xml',
32+
'ContentTypeView+json',
33+
$body
34+
);
35+
36+
$response = $this->sendHttpRequest($request);
37+
$responseData = json_decode($response->getBody(), true);
38+
39+
self::assertArrayHasKey('ContentTypeList', $responseData);
40+
self::assertSame('folder', $responseData['ContentTypeList']['ContentType'][0]['identifier']);
41+
}
42+
1343
/**
1444
* Covers POST /content/typegroups.
1545
*/
@@ -123,7 +153,7 @@ public function testCreateContentType($contentTypeGroupHref)
123153
);
124154
$response = $this->sendHttpRequest($request);
125155

126-
self::assertHttpResponseCodeEquals($response, 201);
156+
self::assertHttpResponseCodeEquals($response, 200);
127157
self::assertHttpResponseHasHeader($response, 'Location');
128158

129159
$this->addCreatedElement($response->getHeader('Location')[0]);
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Ibexa\Tests\Rest\Server\Input\Parser\ContentType;
10+
11+
use Ibexa\Contracts\Core\Repository\Values\ContentType\Query\ContentTypeQuery as ContentTypeQueryValueObject;
12+
use Ibexa\Contracts\Core\Repository\Values\ContentType\Query\Criterion\ContainsFieldDefinitionId;
13+
use Ibexa\Contracts\Core\Repository\Values\ContentType\Query\Criterion\ContentTypeGroupId;
14+
use Ibexa\Contracts\Core\Repository\Values\ContentType\Query\Criterion\ContentTypeId;
15+
use Ibexa\Contracts\Core\Repository\Values\ContentType\Query\Criterion\ContentTypeIdentifier;
16+
use Ibexa\Contracts\Core\Repository\Values\ContentType\Query\Criterion\IsSystem;
17+
use Ibexa\Contracts\Core\Repository\Values\ContentType\Query\Criterion\LogicalAnd;
18+
use Ibexa\Contracts\Core\Repository\Values\ContentType\Query\SortClause;
19+
use Ibexa\Contracts\Core\Repository\Values\ContentType\Query\SortClause\Identifier;
20+
use Ibexa\Rest\Server\Input\Parser\ContentType\Criterion\CriterionProcessor;
21+
use Ibexa\Rest\Server\Input\Parser\ContentType\Query\ContentTypeQuery;
22+
use Ibexa\Rest\Server\Input\Parser\ContentType\SortClause\SortClauseProcessor;
23+
use Ibexa\Tests\Rest\Server\Input\Parser\BaseTest;
24+
use PHPUnit\Framework\MockObject\MockObject;
25+
26+
final class ContentTypeQueryTest extends BaseTest
27+
{
28+
public function testParse(): void
29+
{
30+
$data = [
31+
'limit' => 1,
32+
'offset' => 0,
33+
'Query' => [
34+
'ContentTypeIdCriterion' => [1, 2],
35+
'ContentTypeIdentifierCriterion' => 'folder',
36+
'IsSystemCriterion' => true,
37+
'ContentTypeGroupIdCriterion' => 1,
38+
'ContainsFieldDefinitionIdCriterion' => 2,
39+
],
40+
'SortClauses' => [
41+
'Identifier' => 'descending',
42+
],
43+
];
44+
45+
$parsingDispatcherMock = $this->getParsingDispatcherMock();
46+
self::assertInstanceOf(MockObject::class, $parsingDispatcherMock);
47+
48+
$parsingDispatcherMock
49+
->expects(self::at(0))
50+
->method('parse')
51+
->willReturn(new ContentTypeId([1, 2]));
52+
53+
$parsingDispatcherMock
54+
->expects(self::at(1))
55+
->method('parse')
56+
->willReturn(new ContentTypeIdentifier('folder'));
57+
58+
$parsingDispatcherMock
59+
->expects(self::at(2))
60+
->method('parse')
61+
->willReturn(new IsSystem(true));
62+
63+
$parsingDispatcherMock
64+
->expects(self::at(3))
65+
->method('parse')
66+
->willReturn(new ContentTypeGroupId(1));
67+
68+
$parsingDispatcherMock
69+
->expects(self::at(4))
70+
->method('parse')
71+
->willReturn(new ContainsFieldDefinitionId(1));
72+
73+
$parsingDispatcherMock
74+
->expects(self::at(5))
75+
->method('parse')
76+
->willReturn(new Identifier(SortClause::SORT_DESC));
77+
78+
$result = $this->getParser()->parse($data, $this->getParsingDispatcherMock());
79+
80+
self::assertInstanceOf(ContentTypeQueryValueObject::class, $result);
81+
self::assertSame(1, $result->getLimit());
82+
self::assertSame(0, $result->getOffset());
83+
self::assertInstanceOf(Identifier::class, $result->getSortClauses()[0]);
84+
85+
$criterion = $result->getCriterion();
86+
self::assertInstanceOf(LogicalAnd::class, $criterion);
87+
self::assertCount(5, $criterion->getCriteria());
88+
}
89+
90+
protected function internalGetParser(): ContentTypeQuery
91+
{
92+
$criterionProcessor = new CriterionProcessor($this->getParsingDispatcherMock());
93+
$sortClause = new SortClauseProcessor($this->getParsingDispatcherMock());
94+
95+
return new ContentTypeQuery(
96+
$criterionProcessor,
97+
$sortClause,
98+
);
99+
}
100+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Ibexa\Tests\Rest\Server\Input\Parser\ContentType\Criterion;
10+
11+
use Ibexa\Contracts\Core\Repository\Values\ContentType\Query\Criterion\ContainsFieldDefinitionId as ContainsFieldDefinitionIdCriterion;
12+
use Ibexa\Contracts\Rest\Exceptions\Parser;
13+
use Ibexa\Contracts\Rest\Input\ParsingDispatcher;
14+
use Ibexa\Rest\Server\Input\Parser\ContentType\Criterion\ContainsFieldDefinitionId;
15+
use PHPUnit\Framework\TestCase;
16+
17+
final class ContainsFieldDefinitionIdTest extends TestCase
18+
{
19+
private ContainsFieldDefinitionId $parser;
20+
21+
protected function setUp(): void
22+
{
23+
$this->parser = new ContainsFieldDefinitionId();
24+
}
25+
26+
public function testValidInput(): void
27+
{
28+
self::assertEquals(
29+
new ContainsFieldDefinitionIdCriterion([1, 5]),
30+
$this->parser->parse(
31+
['ContainsFieldDefinitionIdCriterion' => [1, 5]],
32+
$this->createMock(ParsingDispatcher::class)
33+
)
34+
);
35+
}
36+
37+
/**
38+
* @dataProvider provideForTestInvalidInput
39+
*
40+
* @phpstan-param string $exceptionMessage
41+
* @phpstan-param array{
42+
* array<string, string>
43+
* } $input
44+
*/
45+
public function testInvalidInput(string $exceptionMessage, array $input): void
46+
{
47+
$this->expectException(Parser::class);
48+
$this->expectExceptionMessage($exceptionMessage);
49+
50+
$this->parser->parse(
51+
$input,
52+
$this->createMock(ParsingDispatcher::class)
53+
);
54+
}
55+
56+
/**
57+
* @phpstan-return iterable<
58+
* array{
59+
* string,
60+
* array<string, string>,
61+
* },
62+
* >
63+
*/
64+
public function provideForTestInvalidInput(): iterable
65+
{
66+
yield [
67+
'Invalid <ContainsFieldDefinitionIdCriterion>',
68+
[
69+
'bar' => 'foo',
70+
],
71+
];
72+
}
73+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Ibexa\Tests\Rest\Server\Input\Parser\ContentType\Criterion;
10+
11+
use Ibexa\Contracts\Core\Repository\Values\ContentType\Query\Criterion\ContentTypeGroupId as ContentTypeGroupIdCriterion;
12+
use Ibexa\Contracts\Rest\Exceptions\Parser;
13+
use Ibexa\Contracts\Rest\Input\ParsingDispatcher;
14+
use Ibexa\Rest\Server\Input\Parser\ContentType\Criterion\ContentTypeGroupId;
15+
use PHPUnit\Framework\TestCase;
16+
17+
final class ContentTypeGroupIdTest extends TestCase
18+
{
19+
private ContentTypeGroupId $parser;
20+
21+
protected function setUp(): void
22+
{
23+
$this->parser = new ContentTypeGroupId();
24+
}
25+
26+
public function testValidInput(): void
27+
{
28+
self::assertEquals(
29+
new ContentTypeGroupIdCriterion([1, 5]),
30+
$this->parser->parse(
31+
['ContentTypeGroupIdCriterion' => [1, 5]],
32+
$this->createMock(ParsingDispatcher::class)
33+
)
34+
);
35+
}
36+
37+
/**
38+
* @dataProvider provideForTestInvalidInput
39+
*
40+
* @phpstan-param string $exceptionMessage
41+
* @phpstan-param array{
42+
* array<string, string>
43+
* } $input
44+
*/
45+
public function testInvalidInput(string $exceptionMessage, array $input): void
46+
{
47+
$this->expectException(Parser::class);
48+
$this->expectExceptionMessage($exceptionMessage);
49+
50+
$this->parser->parse(
51+
$input,
52+
$this->createMock(ParsingDispatcher::class)
53+
);
54+
}
55+
56+
/**
57+
* @phpstan-return iterable<
58+
* array{
59+
* string,
60+
* array<string, string>,
61+
* },
62+
* >
63+
*/
64+
public function provideForTestInvalidInput(): iterable
65+
{
66+
yield [
67+
'Invalid <ContentTypeGroupIdCriterion>',
68+
[
69+
'bar' => 'foo',
70+
],
71+
];
72+
}
73+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Ibexa\Tests\Rest\Server\Input\Parser\ContentType\Criterion;
10+
11+
use Ibexa\Contracts\Core\Repository\Values\ContentType\Query\Criterion\ContentTypeId as ContentTypeIdCriterion;
12+
use Ibexa\Contracts\Rest\Exceptions\Parser;
13+
use Ibexa\Contracts\Rest\Input\ParsingDispatcher;
14+
use Ibexa\Rest\Server\Input\Parser\ContentType\Criterion\ContentTypeId;
15+
use PHPUnit\Framework\TestCase;
16+
17+
final class ContentTypeIdTest extends TestCase
18+
{
19+
private ContentTypeId $parser;
20+
21+
protected function setUp(): void
22+
{
23+
$this->parser = new ContentTypeId();
24+
}
25+
26+
public function testValidInput(): void
27+
{
28+
self::assertEquals(
29+
new ContentTypeIdCriterion([1, 5]),
30+
$this->parser->parse(
31+
['ContentTypeIdCriterion' => [1, 5]],
32+
$this->createMock(ParsingDispatcher::class)
33+
)
34+
);
35+
}
36+
37+
/**
38+
* @dataProvider provideForTestInvalidInput
39+
*
40+
* @phpstan-param string $exceptionMessage
41+
* @phpstan-param array{
42+
* array<string, string>
43+
* } $input
44+
*/
45+
public function testInvalidInput(string $exceptionMessage, array $input): void
46+
{
47+
$this->expectException(Parser::class);
48+
$this->expectExceptionMessage($exceptionMessage);
49+
50+
$this->parser->parse(
51+
$input,
52+
$this->createMock(ParsingDispatcher::class)
53+
);
54+
}
55+
56+
/**
57+
* @phpstan-return iterable<
58+
* array{
59+
* string,
60+
* array<string, string>,
61+
* },
62+
* >
63+
*/
64+
public function provideForTestInvalidInput(): iterable
65+
{
66+
yield [
67+
'Invalid <ContentTypeIdCriterion>',
68+
[
69+
'bar' => 'foo',
70+
],
71+
];
72+
}
73+
}

0 commit comments

Comments
 (0)