Completed
Push — location_references ( 47d67d )
by
unknown
13:55
created

LocationReferenceResolverTest::testResolvePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace eZ\Publish\Core\LocationReference\Tests;
6
7
use eZ\Publish\Core\LocationReference\ExpressionLanguage\ExpressionLanguage;
8
use eZ\Publish\Core\LocationReference\LimitedLocationService;
9
use eZ\Publish\Core\LocationReference\LocationReferenceResolver;
10
use eZ\Publish\Core\LocationReference\Tests\Stubs\NamedReferencesProviderStub;
11
use eZ\Publish\API\Repository\Tests\BaseTest;
12
use eZ\Publish\API\Repository\Values\Content\Location;
13
14
final class LocationReferenceResolverTest extends BaseTest
15
{
16
    private const ROOT_LOCATION_ID = 2;
17
    private const EXAMPLE_REMOTE_ID = 'example';
18
19
    /** @var \eZ\Publish\Core\LocationReference\LocationReferenceResolverInterface */
20
    private $locationReferenceResolver;
21
22
    /** @var \eZ\Publish\API\Repository\Values\Content\Location */
23
    private $exampleLocation;
24
25
    protected function setUp(): void
26
    {
27
        $this->exampleLocation = $this->createExampleLocation();
28
29
        $this->locationReferenceResolver = new LocationReferenceResolver(
30
            new LimitedLocationService($this->getRepository()->getLocationService()),
31
            new NamedReferencesProviderStub([
32
                '__root' => 'local_id(2)',
33
                'example' => sprintf('remote_id("%s")', self::EXAMPLE_REMOTE_ID),
34
            ]),
35
            new ExpressionLanguage()
36
        );
37
    }
38
39
    public function testResolveLocalId(): void
40
    {
41
        $this->assertLocationReference(
42
            sprintf('local_id(%d)', $this->exampleLocation->id),
43
            $this->exampleLocation->id
44
        );
45
    }
46
47
    public function testResolveRemoteId(): void
48
    {
49
        $this->assertLocationReference(
50
            sprintf('remote_id("%s")', $this->exampleLocation->remoteId),
51
            $this->exampleLocation->id
52
        );
53
    }
54
55
    public function testResolvePath(): void
56
    {
57
        $this->assertLocationReference(
58
            sprintf('path("%s")', $this->exampleLocation->pathString),
59
            $this->exampleLocation->id
60
        );
61
    }
62
63
    public function testResolveRoot(): void
64
    {
65
        $this->assertLocationReference('root()', self::ROOT_LOCATION_ID);
66
    }
67
68
    public function testResolveParent(): void
69
    {
70
        $this->assertLocationReference(
71
            sprintf('parent(local_id(%d))', $this->exampleLocation->id),
72
            $this->exampleLocation->parentLocationId
73
        );
74
    }
75
76
    public function testResolveNamed(): void
77
    {
78
        $this->assertLocationReference('named("example")', $this->exampleLocation->id);
79
    }
80
81
    private function assertLocationReference(string $reference, ?int $expectedLocationId): void
82
    {
83
        $expectedLocation = $this->getRepository()->getLocationService()->loadLocation(
84
            $expectedLocationId
85
        );
86
87
        $actualLocation = $this->locationReferenceResolver->resolve($reference);
88
89
        $this->assertEquals($expectedLocation, $actualLocation);
90
    }
91
92
    private function createExampleLocation(): Location
93
    {
94
        $locationService = $this->getRepository()->getLocationService();
95
        $contentService = $this->getRepository()->getContentService();
96
97
        $contentType = $this->getRepository()
98
            ->getContentTypeService()
99
            ->loadContentTypeByIdentifier('folder');
100
101
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
102
        $contentCreateStruct->setField('name', 'Example folder');
103
104
        $locationCreateStruct = $locationService->newLocationCreateStruct(self::ROOT_LOCATION_ID);
105
        $locationCreateStruct->remoteId = self::EXAMPLE_REMOTE_ID;
106
107
        $content = $contentService->publishVersion(
108
            $contentService->createContent($contentCreateStruct, [$locationCreateStruct]
109
        )->getVersionInfo());
110
111
        return $locationService->loadLocation(
112
            $content->contentInfo->mainLocationId
113
        );
114
    }
115
}
116