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

createResolverMockForValidReference()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 10
rs 9.9332
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\LocationReference;
8
use eZ\Publish\Core\LocationReference\LocationReferenceResolverInterface;
9
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
10
use eZ\Publish\API\Repository\Exceptions\UnauthorizedException;
11
use eZ\Publish\API\Repository\Values\Content\Location;
12
use PHPUnit\Framework\TestCase;
13
use Throwable;
14
15
final class LocationReferenceTest extends TestCase
16
{
17
    private const EXAMPLE_REFERENCE = 'remote_id("babe4a915b1dd5d369e79adb9d6c0c6a")';
18
19
    public function testGetLocation(): void
20
    {
21
        $location = $this->createMock(Location::class);
22
23
        $reference = new LocationReference(
24
            $this->createResolverMockForValidReference(self::EXAMPLE_REFERENCE, $location),
25
            self::EXAMPLE_REFERENCE
26
        );
27
28
        $this->assertEquals($location, $reference->getLocation());
29
    }
30
31
    public function testGetLocationOrNullReturnsLocation(): void
32
    {
33
        $location = $this->createMock(Location::class);
34
35
        $reference = new LocationReference(
36
            $this->createResolverMockForValidReference(self::EXAMPLE_REFERENCE, $location),
37
            self::EXAMPLE_REFERENCE
38
        );
39
40
        $this->assertEquals($location, $reference->getLocationOrNull());
41
    }
42
43
    /**
44
     * @dataProvider dataProviderForExceptionsThrownByLoadLocation
45
     */
46
    public function testGetLocationOrNullReturnsNull(Throwable $exception): void
47
    {
48
        $reference = new LocationReference(
49
            $this->createResolverMockForInvalidReference(self::EXAMPLE_REFERENCE, $exception),
50
            self::EXAMPLE_REFERENCE
51
        );
52
53
        $this->assertNull($reference->getLocationOrNull());
54
    }
55
56
    public function testGetLocationOrDefaultReturnsLocation(): void
57
    {
58
        $location = $this->createMock(Location::class);
59
        $fallback = $this->createMock(Location::class);
60
61
        $reference = new LocationReference(
62
            $this->createResolverMockForValidReference(self::EXAMPLE_REFERENCE, $location),
63
            self::EXAMPLE_REFERENCE
64
        );
65
66
        $this->assertEquals($location, $reference->getLocationOrDefault($fallback));
67
    }
68
69
    /**
70
     * @dataProvider dataProviderForExceptionsThrownByLoadLocation
71
     */
72
    public function testGetLocationOrDefaultReturnsDefault(Throwable $exception): void
73
    {
74
        $fallback = $this->createMock(Location::class);
75
76
        $reference = new LocationReference(
77
            $this->createResolverMockForInvalidReference(self::EXAMPLE_REFERENCE, $exception),
78
            self::EXAMPLE_REFERENCE
79
        );
80
81
        $this->assertEquals($fallback, $reference->getLocationOrDefault($fallback));
82
    }
83
84
    public function dataProviderForExceptionsThrownByLoadLocation(): array
85
    {
86
        return [
87
            [$this->createMock(NotFoundException::class)],
88
            [$this->createMock(UnauthorizedException::class)],
89
        ];
90
    }
91
92
    private function createResolverMockForValidReference(string $reference, Location $location): LocationReferenceResolverInterface
93
    {
94
        $locationReferenceResolver = $this->createMock(LocationReferenceResolverInterface::class);
95
        $locationReferenceResolver
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
96
            ->method('resolve')
97
            ->with($reference)
98
            ->willReturn($location);
99
100
        return $locationReferenceResolver;
101
    }
102
103
    private function createResolverMockForInvalidReference(string $reference, Throwable $exception): LocationReferenceResolverInterface
104
    {
105
        $locationReferenceResolver = $this->createMock(LocationReferenceResolverInterface::class);
106
        $locationReferenceResolver
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
107
            ->method('resolve')
108
            ->with($reference)
109
            ->willThrowException($exception);
110
111
        return $locationReferenceResolver;
112
    }
113
}
114