Completed
Push — preview_location-getContent ( 01895d )
by André
19:30
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 {@see 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->content);
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 {@see 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->content);
103
        //$this->assertSame($contentInfo, $returnedLocation->contentInfo);
104
    }
105
106
    public function testGetPreviewLocationNoLocation()
107
    {
108
        $contentId = 123;
109
        $content = $this->getContentMock($contentId);
110
111
        $this->contentService
112
            ->expects($this->once())
113
            ->method('loadContent')
114
            ->with($contentId)
115
            ->willReturn($content);
116
117
        $this->locationHandler
118
            ->expects($this->once())
119
            ->method('loadParentLocationsForDraftContent')
120
            ->with($contentId)
121
            ->will($this->returnValue([]));
122
123
        $this->locationHandler->expects($this->never())->method('loadLocationsByContent');
124
125
        $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 {@see 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...
126
    }
127
128
    private function getContentMock(int $contentId, ?int $mainLocationId = null, bool $published = false): Content
129
    {
130
        $contentInfo = new APIContentInfo([
131
            'id' => $contentId,
132
            'mainLocationId' => $mainLocationId,
133
            'published' => $published,
134
        ]);
135
136
        $versionInfo = $this->createMock(VersionInfo::class);
137
        $versionInfo->expects($this->once())
138
            ->method('getContentInfo')
139
            ->willReturn($contentInfo);
140
141
        $content = $this->createMock(Content::class);
142
        $content->expects($this->once())
143
            ->method('getVersionInfo')
144
            ->willReturn($versionInfo);
145
146
        return $content;
147
    }
148
}
149