|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\CmsUrlRewrite\Test\Unit\Plugin\Cms\Model\Store; |
| 10 | + |
| 11 | +use Magento\Cms\Api\Data\PageSearchResultsInterface; |
| 12 | +use Magento\Cms\Api\PageRepositoryInterface; |
| 13 | +use Magento\Cms\Model\Page; |
| 14 | +use Magento\CmsUrlRewrite\Model\CmsPageUrlRewriteGenerator; |
| 15 | +use Magento\CmsUrlRewrite\Plugin\Cms\Model\Store\View; |
| 16 | +use Magento\Framework\Api\SearchCriteria; |
| 17 | +use Magento\Framework\Api\SearchCriteriaBuilder; |
| 18 | +use Magento\Framework\Model\AbstractModel; |
| 19 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 20 | +use Magento\Store\Model\ResourceModel\Store; |
| 21 | +use Magento\UrlRewrite\Model\UrlPersistInterface; |
| 22 | +use PHPUnit\Framework\MockObject\MockObject; |
| 23 | +use PHPUnit\Framework\TestCase; |
| 24 | + |
| 25 | +/** |
| 26 | + * Test for \Magento\CmsUrlRewrite\Plugin\Cms\Model\Store\View. |
| 27 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 28 | + */ |
| 29 | +class ViewTest extends TestCase |
| 30 | +{ |
| 31 | + private const STUB_STORE_ID = 777; |
| 32 | + private const STUB_URL_REWRITE = ['cms/page/view']; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var View |
| 36 | + */ |
| 37 | + private $model; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var SearchCriteria|MockObject |
| 41 | + */ |
| 42 | + private $searchCriteriaMock; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var PageSearchResultsInterface|MockObject |
| 46 | + */ |
| 47 | + private $pageSearchResultMock; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var Page|MockObject |
| 51 | + */ |
| 52 | + private $pageMock; |
| 53 | + |
| 54 | + /** |
| 55 | + * @var Store|MockObject |
| 56 | + */ |
| 57 | + private $storeObjectMock; |
| 58 | + |
| 59 | + /** |
| 60 | + * @var AbstractModel|MockObject |
| 61 | + */ |
| 62 | + private $abstractModelMock; |
| 63 | + |
| 64 | + /** |
| 65 | + * @var UrlPersistInterface|MockObject |
| 66 | + */ |
| 67 | + private $urlPersistMock; |
| 68 | + |
| 69 | + /** |
| 70 | + * @var PageRepositoryInterface|MockObject |
| 71 | + */ |
| 72 | + private $pageRepositoryMock; |
| 73 | + |
| 74 | + /** |
| 75 | + * @var CmsPageUrlRewriteGenerator|MockObject |
| 76 | + */ |
| 77 | + private $cmsPageUrlGeneratorMock; |
| 78 | + |
| 79 | + /** |
| 80 | + * @inheritDoc |
| 81 | + */ |
| 82 | + protected function setUp(): void |
| 83 | + { |
| 84 | + $objectManager = new ObjectManager($this); |
| 85 | + |
| 86 | + $this->storeObjectMock = $this->createMock(Store::class); |
| 87 | + $this->searchCriteriaMock = $this->createMock(SearchCriteria::class); |
| 88 | + $this->pageSearchResultMock = $this->createMock(PageSearchResultsInterface::class); |
| 89 | + |
| 90 | + $this->pageMock = $this->getMockBuilder(Page::class) |
| 91 | + ->disableOriginalConstructor() |
| 92 | + ->addMethods(['setStoreId']) |
| 93 | + ->getMock(); |
| 94 | + |
| 95 | + $this->abstractModelMock = $this->getMockBuilder(AbstractModel::class) |
| 96 | + ->onlyMethods(['isObjectNew', 'getId']) |
| 97 | + ->disableOriginalConstructor() |
| 98 | + ->getMockForAbstractClass(); |
| 99 | + |
| 100 | + $this->urlPersistMock = $this->createMock(UrlPersistInterface::class); |
| 101 | + $this->pageRepositoryMock = $this->createMock(PageRepositoryInterface::class); |
| 102 | + $this->cmsPageUrlGeneratorMock = $this->createMock(CmsPageUrlRewriteGenerator::class); |
| 103 | + |
| 104 | + $searchCriteriaBuilderMock = $this->createMock(SearchCriteriaBuilder::class); |
| 105 | + $searchCriteriaBuilderMock->expects($this->any()) |
| 106 | + ->method('addFilter') |
| 107 | + ->willReturnSelf(); |
| 108 | + $searchCriteriaBuilderMock->expects($this->any()) |
| 109 | + ->method('create') |
| 110 | + ->willReturn($this->searchCriteriaMock); |
| 111 | + |
| 112 | + $this->model = $objectManager->getObject( |
| 113 | + View::class, |
| 114 | + [ |
| 115 | + 'urlPersist' => $this->urlPersistMock, |
| 116 | + 'searchCriteriaBuilder' => $searchCriteriaBuilderMock, |
| 117 | + 'pageRepository' => $this->pageRepositoryMock, |
| 118 | + 'cmsPageUrlRewriteGenerator' => $this->cmsPageUrlGeneratorMock, |
| 119 | + ] |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * After save when object is not new |
| 125 | + * |
| 126 | + * @return void |
| 127 | + */ |
| 128 | + public function testAfterSaveObjectIsNotNew(): void |
| 129 | + { |
| 130 | + $storeResult = clone $this->storeObjectMock; |
| 131 | + |
| 132 | + $this->abstractModelMock->expects($this->once()) |
| 133 | + ->method('isObjectNew') |
| 134 | + ->willReturn(false); |
| 135 | + |
| 136 | + $this->urlPersistMock->expects($this->never()) |
| 137 | + ->method('replace'); |
| 138 | + |
| 139 | + $result = $this->model->afterSave($this->storeObjectMock, $storeResult, $this->abstractModelMock); |
| 140 | + $this->assertEquals($storeResult, $result); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * After save when object is new |
| 145 | + * |
| 146 | + * @return void |
| 147 | + */ |
| 148 | + public function testAfterSaveObjectIsNew(): void |
| 149 | + { |
| 150 | + $storeResult = clone $this->storeObjectMock; |
| 151 | + |
| 152 | + $this->abstractModelMock->expects($this->once()) |
| 153 | + ->method('isObjectNew') |
| 154 | + ->willReturn(true); |
| 155 | + $this->abstractModelMock->expects($this->once()) |
| 156 | + ->method('getId') |
| 157 | + ->willReturn(self::STUB_STORE_ID); |
| 158 | + $this->pageRepositoryMock->expects($this->once()) |
| 159 | + ->method('getList') |
| 160 | + ->with($this->searchCriteriaMock) |
| 161 | + ->willReturn($this->pageSearchResultMock); |
| 162 | + $this->pageSearchResultMock->expects($this->once()) |
| 163 | + ->method('getItems') |
| 164 | + ->willReturn([$this->pageMock]); |
| 165 | + $this->pageMock->expects($this->once()) |
| 166 | + ->method('setStoreId') |
| 167 | + ->with(self::STUB_STORE_ID); |
| 168 | + $this->cmsPageUrlGeneratorMock->expects($this->once()) |
| 169 | + ->method('generate') |
| 170 | + ->with($this->pageMock) |
| 171 | + ->willReturn(self::STUB_URL_REWRITE); |
| 172 | + $this->urlPersistMock->expects($this->once()) |
| 173 | + ->method('replace') |
| 174 | + ->with(self::STUB_URL_REWRITE); |
| 175 | + |
| 176 | + $result = $this->model->afterSave($this->storeObjectMock, $storeResult, $this->abstractModelMock); |
| 177 | + $this->assertEquals($storeResult, $result); |
| 178 | + } |
| 179 | +} |
0 commit comments