Skip to content

Commit b3b389e

Browse files
committed
Unit test added for page service class and code review changes
1 parent 5aba121 commit b3b389e

File tree

2 files changed

+185
-3
lines changed

2 files changed

+185
-3
lines changed

app/code/Magento/Cms/Model/Page/Service/PageService.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ class PageService
2323
* @param CustomLayoutManagerInterface $customLayoutManager
2424
*/
2525
public function __construct(
26-
private PageRepositoryInterface $pageRepository,
27-
private PageFactory $pageFactory,
28-
private CustomLayoutManagerInterface $customLayoutManager,
26+
private readonly PageRepositoryInterface $pageRepository,
27+
private readonly PageFactory $pageFactory,
28+
private readonly CustomLayoutManagerInterface $customLayoutManager,
2929
) {
3030
}
3131

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Cms\Test\Unit\Model\Page\Service;
9+
10+
use Magento\Cms\Api\Data\PageInterface;
11+
use Magento\Cms\Api\PageRepositoryInterface;
12+
use Magento\Cms\Model\Page\CustomLayoutManagerInterface;
13+
use Magento\Cms\Model\Page\Service\PageService;
14+
use Magento\Cms\Model\PageFactory;
15+
use Magento\Framework\Exception\LocalizedException;
16+
use Magento\Framework\Exception\NoSuchEntityException;
17+
use PHPUnit\Framework\MockObject\MockObject;
18+
use PHPUnit\Framework\TestCase;
19+
20+
/**
21+
* Unit tests for PageService
22+
*/
23+
class PageServiceTest extends TestCase
24+
{
25+
/**
26+
* @var PageRepositoryInterface|MockObject
27+
*/
28+
private $pageRepositoryMock;
29+
30+
/**
31+
* @var PageFactory|MockObject
32+
*/
33+
private $pageFactoryMock;
34+
35+
/**
36+
* @var CustomLayoutManagerInterface|MockObject
37+
*/
38+
private $customLayoutManagerMock;
39+
40+
/**
41+
* @var PageService
42+
*/
43+
private $pageService;
44+
45+
/**
46+
* @var PageInterface|MockObject
47+
*/
48+
private $pageMock;
49+
50+
/**
51+
* Set up test environment
52+
*/
53+
protected function setUp(): void
54+
{
55+
$this->pageRepositoryMock = $this->createMock(PageRepositoryInterface::class);
56+
$this->pageFactoryMock = $this->createMock(PageFactory::class);
57+
$this->customLayoutManagerMock = $this->createMock(CustomLayoutManagerInterface::class);
58+
$this->pageMock = $this->createMock(PageInterface::class);
59+
60+
$this->pageService = new PageService(
61+
$this->pageRepositoryMock,
62+
$this->pageFactoryMock,
63+
$this->customLayoutManagerMock
64+
);
65+
}
66+
67+
/**
68+
* Test getPageById returns existing page when page exists
69+
*/
70+
public function testGetPageByIdReturnsExistingPage(): void
71+
{
72+
$pageId = 1;
73+
74+
$this->pageRepositoryMock->expects($this->once())
75+
->method('getById')
76+
->with($pageId)
77+
->willReturn($this->pageMock);
78+
79+
$this->pageFactoryMock->expects($this->never())
80+
->method('create');
81+
82+
$result = $this->pageService->getPageById($pageId);
83+
84+
$this->assertSame($this->pageMock, $result);
85+
}
86+
87+
/**
88+
* Test getPageById returns new page instance when page doesn't exist
89+
*/
90+
public function testGetPageByIdReturnsNewPageWhenNotExists(): void
91+
{
92+
$pageId = 999;
93+
$newPageMock = $this->createMock(PageInterface::class);
94+
95+
$this->pageRepositoryMock->expects($this->once())
96+
->method('getById')
97+
->with($pageId)
98+
->willThrowException(new NoSuchEntityException(__('Page not found')));
99+
100+
$this->pageFactoryMock->expects($this->once())
101+
->method('create')
102+
->willReturn($newPageMock);
103+
104+
$result = $this->pageService->getPageById($pageId);
105+
106+
$this->assertSame($newPageMock, $result);
107+
}
108+
109+
/**
110+
* Test getPageById handles LocalizedException and returns new page
111+
*/
112+
public function testGetPageByIdHandlesLocalizedException(): void
113+
{
114+
$pageId = 1;
115+
$newPageMock = $this->createMock(PageInterface::class);
116+
117+
$this->pageRepositoryMock->expects($this->once())
118+
->method('getById')
119+
->with($pageId)
120+
->willThrowException(new LocalizedException(__('Some localized error')));
121+
122+
$this->pageFactoryMock->expects($this->once())
123+
->method('create')
124+
->willReturn($newPageMock);
125+
126+
$result = $this->pageService->getPageById($pageId);
127+
128+
$this->assertSame($newPageMock, $result);
129+
}
130+
131+
/**
132+
* Test createPageFactory returns new page instance
133+
*/
134+
public function testCreatePageFactory(): void
135+
{
136+
$newPageMock = $this->createMock(PageInterface::class);
137+
138+
$this->pageFactoryMock->expects($this->once())
139+
->method('create')
140+
->willReturn($newPageMock);
141+
142+
$result = $this->pageService->createPageFactory();
143+
144+
$this->assertSame($newPageMock, $result);
145+
}
146+
147+
/**
148+
* Test fetchAvailableCustomLayouts returns available layouts
149+
*/
150+
public function testFetchAvailableCustomLayouts(): void
151+
{
152+
$expectedLayouts = [
153+
'layout1.xml',
154+
'layout2.xml',
155+
'custom_layout.xml'
156+
];
157+
158+
$this->customLayoutManagerMock->expects($this->once())
159+
->method('fetchAvailableFiles')
160+
->with($this->pageMock)
161+
->willReturn($expectedLayouts);
162+
163+
$result = $this->pageService->fetchAvailableCustomLayouts($this->pageMock);
164+
165+
$this->assertSame($expectedLayouts, $result);
166+
}
167+
168+
/**
169+
* Test fetchAvailableCustomLayouts returns empty array when no layouts available
170+
*/
171+
public function testFetchAvailableCustomLayoutsReturnsEmptyArray(): void
172+
{
173+
$this->customLayoutManagerMock->expects($this->once())
174+
->method('fetchAvailableFiles')
175+
->with($this->pageMock)
176+
->willReturn([]);
177+
178+
$result = $this->pageService->fetchAvailableCustomLayouts($this->pageMock);
179+
180+
$this->assertSame([], $result);
181+
}
182+
}

0 commit comments

Comments
 (0)