|
| 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\CatalogUrlRewrite\Test\Unit\Observer; |
| 10 | + |
| 11 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 12 | +use Magento\Framework\App\RequestInterface; |
| 13 | +use Magento\Framework\Event; |
| 14 | +use Magento\Framework\Event\Observer; |
| 15 | +use Magento\Catalog\Model\Product; |
| 16 | +use Magento\Catalog\Model\Product\Visibility; |
| 17 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 18 | +use Magento\CatalogUrlRewrite\Observer\ProductToWebsiteChangeObserver; |
| 19 | +use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator; |
| 20 | +use Magento\UrlRewrite\Model\UrlPersistInterface; |
| 21 | +use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; |
| 22 | +use Magento\Store\Model\Store; |
| 23 | + |
| 24 | +/** |
| 25 | + * Test for ProductToWebsiteChangeObserver |
| 26 | + * |
| 27 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 28 | + */ |
| 29 | +class ProductToWebsiteChangeObserverTest extends \PHPUnit\Framework\TestCase |
| 30 | +{ |
| 31 | + /** |
| 32 | + * @var ProductRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject |
| 33 | + */ |
| 34 | + private $productRepository; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var ProductUrlRewriteGenerator|\PHPUnit_Framework_MockObject_MockObject |
| 38 | + */ |
| 39 | + private $productUrlRewriteGenerator; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var UrlPersistInterface|\PHPUnit_Framework_MockObject_MockObject |
| 43 | + */ |
| 44 | + private $urlPersist; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var Event|\PHPUnit_Framework_MockObject_MockObject |
| 48 | + */ |
| 49 | + private $event; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var Observer|\PHPUnit_Framework_MockObject_MockObject |
| 53 | + */ |
| 54 | + private $observer; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var Product|\PHPUnit_Framework_MockObject_MockObject |
| 58 | + */ |
| 59 | + private $product; |
| 60 | + |
| 61 | + /** |
| 62 | + * @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject |
| 63 | + */ |
| 64 | + private $request; |
| 65 | + |
| 66 | + /** |
| 67 | + * @var ObjectManager |
| 68 | + */ |
| 69 | + private $objectManager; |
| 70 | + |
| 71 | + /** |
| 72 | + * @var ProductToWebsiteChangeObserver |
| 73 | + */ |
| 74 | + private $model; |
| 75 | + |
| 76 | + /** |
| 77 | + * @var int |
| 78 | + */ |
| 79 | + private $productId; |
| 80 | + |
| 81 | + /** |
| 82 | + * @inheritDoc |
| 83 | + */ |
| 84 | + protected function setUp() |
| 85 | + { |
| 86 | + $this->productId = 3; |
| 87 | + |
| 88 | + $this->urlPersist = $this->getMockBuilder(UrlPersistInterface::class) |
| 89 | + ->setMethods(['deleteByData', 'replace']) |
| 90 | + ->getMockForAbstractClass(); |
| 91 | + $this->productRepository = $this->getMockBuilder(ProductRepositoryInterface::class) |
| 92 | + ->setMethods(['getById']) |
| 93 | + ->getMockForAbstractClass(); |
| 94 | + $this->product = $this->getMockBuilder(Product::class) |
| 95 | + ->disableOriginalConstructor() |
| 96 | + ->setMethods(['getId', 'getVisibility']) |
| 97 | + ->getMock(); |
| 98 | + $this->product->expects($this->any()) |
| 99 | + ->method('getId') |
| 100 | + ->willReturn($this->productId); |
| 101 | + $this->productRepository->expects($this->any()) |
| 102 | + ->method('getById') |
| 103 | + ->with($this->productId, false, Store::DEFAULT_STORE_ID) |
| 104 | + ->willReturn($this->product); |
| 105 | + $this->productUrlRewriteGenerator = $this->getMockBuilder(ProductUrlRewriteGenerator::class) |
| 106 | + ->disableOriginalConstructor() |
| 107 | + ->setMethods(['generate']) |
| 108 | + ->getMock(); |
| 109 | + $this->event = $this->getMockBuilder(Event::class) |
| 110 | + ->disableOriginalConstructor() |
| 111 | + ->setMethods(['getProducts']) |
| 112 | + ->getMock(); |
| 113 | + $this->event->expects($this->any()) |
| 114 | + ->method('getProducts') |
| 115 | + ->willReturn([$this->productId]); |
| 116 | + $this->observer = $this->getMockBuilder(Observer::class) |
| 117 | + ->disableOriginalConstructor() |
| 118 | + ->setMethods(['getEvent']) |
| 119 | + ->getMock(); |
| 120 | + $this->observer->expects($this->any()) |
| 121 | + ->method('getEvent') |
| 122 | + ->willReturn($this->event); |
| 123 | + $this->request = $this->getMockBuilder(RequestInterface::class) |
| 124 | + ->setMethods(['getParam']) |
| 125 | + ->getMockForAbstractClass(); |
| 126 | + $this->request->expects($this->any()) |
| 127 | + ->method('getParam') |
| 128 | + ->with('store_id', Store::DEFAULT_STORE_ID) |
| 129 | + ->willReturn(Store::DEFAULT_STORE_ID); |
| 130 | + |
| 131 | + $this->objectManager = new ObjectManager($this); |
| 132 | + $this->model = $this->objectManager->getObject( |
| 133 | + ProductToWebsiteChangeObserver::class, |
| 134 | + [ |
| 135 | + 'productUrlRewriteGenerator' => $this->productUrlRewriteGenerator, |
| 136 | + 'urlPersist' => $this->urlPersist, |
| 137 | + 'productRepository' => $this->productRepository, |
| 138 | + 'request' => $this->request |
| 139 | + ] |
| 140 | + ); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * @param array $urlRewriteGeneratorResult |
| 145 | + * @param int $numberDeleteByData |
| 146 | + * @param int $productVisibility |
| 147 | + * @param int $numberReplace |
| 148 | + * @throws \Magento\Framework\Exception\NoSuchEntityException |
| 149 | + * @throws \Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException |
| 150 | + * @dataProvider executeDataProvider |
| 151 | + */ |
| 152 | + public function testExecute( |
| 153 | + array $urlRewriteGeneratorResult, |
| 154 | + int $numberDeleteByData, |
| 155 | + int $productVisibility, |
| 156 | + int $numberReplace |
| 157 | + ) { |
| 158 | + $this->productUrlRewriteGenerator->expects($this->any()) |
| 159 | + ->method('generate') |
| 160 | + ->willReturn($urlRewriteGeneratorResult); |
| 161 | + $this->urlPersist->expects($this->exactly($numberDeleteByData)) |
| 162 | + ->method('deleteByData') |
| 163 | + ->with( |
| 164 | + [ |
| 165 | + UrlRewrite::ENTITY_ID => $this->productId, |
| 166 | + UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE, |
| 167 | + UrlRewrite::STORE_ID => Store::DEFAULT_STORE_ID |
| 168 | + ] |
| 169 | + ); |
| 170 | + $this->product->expects($this->any()) |
| 171 | + ->method('getVisibility') |
| 172 | + ->willReturn($productVisibility); |
| 173 | + $this->urlPersist->expects($this->exactly($numberReplace)) |
| 174 | + ->method('replace') |
| 175 | + ->with($urlRewriteGeneratorResult); |
| 176 | + |
| 177 | + $this->model->execute($this->observer); |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Data provider for testExecute |
| 182 | + * |
| 183 | + * @return array |
| 184 | + */ |
| 185 | + public function executeDataProvider(): array |
| 186 | + { |
| 187 | + return [ |
| 188 | + [[], 0, Visibility::VISIBILITY_NOT_VISIBLE, 0], |
| 189 | + [['someRewrite'], 1, Visibility::VISIBILITY_NOT_VISIBLE, 0], |
| 190 | + [['someRewrite'], 1, Visibility::VISIBILITY_BOTH, 1], |
| 191 | + ]; |
| 192 | + } |
| 193 | +} |
0 commit comments