Completed
Push — ezp_30797 ( c9135e...a01256 )
by
unknown
19:32
created

PreviewLocationProviderTest::getContentMock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the eZ Publish Kernel package.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Helper\Tests;
10
11
use eZ\Publish\API\Repository\ContentService;
12
use eZ\Publish\API\Repository\LocationService;
13
use eZ\Publish\API\Repository\Values\Content\ContentInfo as APIContentInfo;
14
use eZ\Publish\API\Repository\Values\Content\Location as APILocation;
15
use eZ\Publish\Core\Helper\PreviewLocationProvider;
16
use eZ\Publish\Core\Repository\Values\Content\Content;
17
use eZ\Publish\Core\Repository\Values\Content\Location;
18
use eZ\Publish\Core\Repository\Values\Content\VersionInfo;
19
use eZ\Publish\SPI\Persistence\Content\Location\Handler as SPILocationHandler;
20
use PHPUnit\Framework\TestCase;
21
22
class PreviewLocationProviderTest extends TestCase
23
{
24
    /** @var \eZ\Publish\API\Repository\LocationService|\PHPUnit\Framework\MockObject\MockObject */
25
    private $contentService;
26
27
    /** @var \eZ\Publish\API\Repository\LocationService|\PHPUnit\Framework\MockObject\MockObject */
28
    private $locationService;
29
30
    /** @var \eZ\Publish\SPI\Persistence\Content\Location\Handler|\PHPUnit\Framework\MockObject\MockObject */
31
    private $locationHandler;
32
33
    /** @var \eZ\Publish\Core\Helper\PreviewLocationProvider */
34
    private $provider;
35
36
    protected function setUp()
37
    {
38
        parent::setUp();
39
40
        $this->contentService = $this->createMock(ContentService::class);
41
        $this->locationService = $this->createMock(LocationService::class);
42
        $this->locationHandler = $this->createMock(SPILocationHandler::class);
43
        $this->provider = new PreviewLocationProvider($this->locationService, $this->contentService, $this->locationHandler);
44
    }
45
46
    public function testGetPreviewLocationDraft()
47
    {
48
        $contentId = 123;
49
        $parentLocationId = 456;
50
        $content = $this->getContentMock($contentId);
51
52
        $this->contentService
53
            ->expects($this->once())
54
            ->method('loadContent')
55
            ->with($contentId)
56
            ->willReturn($content);
57
58
        $this->locationService
59
            ->expects($this->never())
60
            ->method('loadLocation');
61
62
        $this->locationHandler
63
            ->expects($this->once())
64
            ->method('loadParentLocationsForDraftContent')
65
            ->with($contentId)
66
            ->will($this->returnValue([new Location(['id' => $parentLocationId])]));
67
68
        $location = $this->provider->loadMainLocation($contentId);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Helper\P...der::loadMainLocation() has been deprecated with message: Since 7.5.4, rather use loadMainLocationByContent.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
69
        $this->assertInstanceOf(APILocation::class, $location);
70
        $this->assertSame($content, $location->getContent());
71
        $this->assertNull($location->id);
72
        $this->assertEquals($parentLocationId, $location->parentLocationId);
73
    }
74
75
    public function testGetPreviewLocation()
76
    {
77
        $contentId = 123;
78
        $locationId = 456;
79
        $content = $this->getContentMock($contentId, $locationId);
80
81
        $location = $this
82
            ->getMockBuilder(Location::class)
83
            ->setConstructorArgs([['id' => $locationId, 'content' => $content]])
84
            ->getMockForAbstractClass();
85
86
        $this->contentService
87
            ->expects($this->once())
88
            ->method('loadContent')
89
            ->with($contentId)
90
            ->willReturn($content);
91
92
        $this->locationService
93
            ->expects($this->once())
94
            ->method('loadLocation')
95
            ->with($locationId)
96
            ->will($this->returnValue($location));
97
98
        $this->locationHandler->expects($this->never())->method('loadParentLocationsForDraftContent');
99
100
        $returnedLocation = $this->provider->loadMainLocation($contentId);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Helper\P...der::loadMainLocation() has been deprecated with message: Since 7.5.4, rather use loadMainLocationByContent.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
101
        $this->assertSame($location, $returnedLocation);
102
        $this->assertSame($content, $location->getContent());
103
    }
104
105
    public function testGetPreviewLocationNoLocation()
106
    {
107
        $contentId = 123;
108
        $content = $this->getContentMock($contentId);
109
110
        $this->contentService
111
            ->expects($this->once())
112
            ->method('loadContent')
113
            ->with($contentId)
114
            ->willReturn($content);
115
116
        $this->locationHandler
117
            ->expects($this->once())
118
            ->method('loadParentLocationsForDraftContent')
119
            ->with($contentId)
120
            ->will($this->returnValue([]));
121
122
        $this->locationHandler->expects($this->never())->method('loadLocationsByContent');
123
124
        $this->assertNull($this->provider->loadMainLocation($contentId));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Helper\P...der::loadMainLocation() has been deprecated with message: Since 7.5.4, rather use loadMainLocationByContent.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
125
    }
126
127
    private function getContentMock(int $contentId, ?int $mainLocationId = null, bool $published = false): Content
128
    {
129
        $contentInfo = new APIContentInfo([
130
            'id' => $contentId,
131
            'mainLocationId' => $mainLocationId,
132
            'published' => $published,
133
        ]);
134
135
        $versionInfo = $this->createMock(VersionInfo::class);
136
        $versionInfo->expects($this->once())
137
            ->method('getContentInfo')
138
            ->willReturn($contentInfo);
139
140
        $content = $this->createMock(Content::class);
141
        $content->expects($this->once())
142
            ->method('getVersionInfo')
143
            ->willReturn($versionInfo);
144
145
        return $content;
146
    }
147
}
148