|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright © 2016 Magento. All rights reserved. |
| 5 | + * See COPYING.txt for license details. |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Magento\CatalogUrlRewrite\Test\Unit\Observer; |
| 9 | + |
| 10 | +use Magento\Catalog\Model\Product; |
| 11 | +use Magento\Catalog\Model\Product\Visibility; |
| 12 | +use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator; |
| 13 | +use Magento\CatalogUrlRewrite\Observer\ProductProcessUrlRewriteSavingObserver; |
| 14 | +use Magento\Framework\Event; |
| 15 | +use Magento\Framework\Event\Observer; |
| 16 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; |
| 17 | +use Magento\UrlRewrite\Model\UrlPersistInterface; |
| 18 | + |
| 19 | +/** |
| 20 | + * Class ProductProcessUrlRewriteSavingObserver |
| 21 | + * |
| 22 | + * @SuppressWarnings(PHPMD.TooManyFields) |
| 23 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 24 | + */ |
| 25 | +class ProductProcessUrlRewriteSavingObserverTest extends \PHPUnit_Framework_TestCase |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @var ProductProcessUrlRewriteSavingObserver |
| 29 | + */ |
| 30 | + protected $productProcessUrlRewriteSavingObserver; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var UrlPersistInterface|\PHPUnit_Framework_MockObject_MockObject |
| 34 | + */ |
| 35 | + protected $urlPersist; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var Observer|\PHPUnit_Framework_MockObject_MockObject |
| 39 | + */ |
| 40 | + protected $observer; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var Event|\PHPUnit_Framework_MockObject_MockObject |
| 44 | + */ |
| 45 | + protected $event; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var Product|\PHPUnit_Framework_MockObject_MockObject |
| 49 | + */ |
| 50 | + protected $product; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var ObjectManagerHelper |
| 54 | + */ |
| 55 | + protected $objectManagerHelper; |
| 56 | + |
| 57 | + /** |
| 58 | + * @var ProductUrlRewriteGenerator|\PHPUnit_Framework_MockObject_MockObject |
| 59 | + */ |
| 60 | + protected $productUrlRewriteGenerator; |
| 61 | + |
| 62 | + /** |
| 63 | + * @SuppressWarnings(PHPMD.TooManyFields) |
| 64 | + */ |
| 65 | + public function setUp() |
| 66 | + { |
| 67 | + $this->urlPersist = $this->getMockBuilder(UrlPersistInterface::class) |
| 68 | + ->disableOriginalConstructor() |
| 69 | + ->getMock(); |
| 70 | + |
| 71 | + $this->productUrlRewriteGenerator = $this->getMockBuilder(ProductUrlRewriteGenerator::class) |
| 72 | + ->disableOriginalConstructor() |
| 73 | + ->getMock(); |
| 74 | + |
| 75 | + $this->objectManagerHelper = new ObjectManagerHelper($this); |
| 76 | + $this->productProcessUrlRewriteSavingObserver = $this->objectManagerHelper->getObject( |
| 77 | + ProductProcessUrlRewriteSavingObserver::class, |
| 78 | + [ |
| 79 | + 'productUrlRewriteGenerator' => $this->productUrlRewriteGenerator, |
| 80 | + 'urlPersist' => $this->urlPersist, |
| 81 | + ] |
| 82 | + ); |
| 83 | + |
| 84 | + $this->product = $this->getMockBuilder(Product::class) |
| 85 | + ->disableOriginalConstructor() |
| 86 | + ->setMethods([ |
| 87 | + 'getProductCategories', |
| 88 | + 'dataHasChangedFor', |
| 89 | + 'getVisibility' |
| 90 | + ]) |
| 91 | + ->getMock(); |
| 92 | + |
| 93 | + $this->event = $this->getMock(Event::class, ['getProduct'], [], '', false); |
| 94 | + $this->event->method('getProduct')->willReturn($this->product); |
| 95 | + |
| 96 | + $this->observer = $this->getMock(Observer::class, ['getEvent'], [], '', false); |
| 97 | + $this->observer->method('getEvent')->willReturn($this->event); |
| 98 | + |
| 99 | + } |
| 100 | + |
| 101 | + public function testVisibility() |
| 102 | + { |
| 103 | + $this->productUrlRewriteGenerator->expects(static::once()) |
| 104 | + ->method('generate') |
| 105 | + ->with($this->product) |
| 106 | + ->willReturn([]); |
| 107 | + $this->urlPersist->expects(static::atLeastOnce()) |
| 108 | + ->method('replace') |
| 109 | + ->with([]); |
| 110 | + $this->product->expects(static::atLeastOnce()) |
| 111 | + ->method('getVisibility') |
| 112 | + ->willReturn(Visibility::VISIBILITY_IN_CATALOG); |
| 113 | + $this->product->expects(static::atLeastOnce()) |
| 114 | + ->method('dataHasChangedFor') |
| 115 | + ->willReturnMap([ |
| 116 | + ['visibility', true], |
| 117 | + ]); |
| 118 | + |
| 119 | + $this->productProcessUrlRewriteSavingObserver->execute($this->observer); |
| 120 | + } |
| 121 | +} |
0 commit comments