|
| 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 | +class ProductProcessUrlRewriteSavingObserverTest extends \PHPUnit_Framework_TestCase |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var ProductProcessUrlRewriteSavingObserver |
| 26 | + */ |
| 27 | + protected $productProcessUrlRewriteSavingObserver; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var UrlPersistInterface|\PHPUnit_Framework_MockObject_MockObject |
| 31 | + */ |
| 32 | + protected $urlPersist; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var Observer|\PHPUnit_Framework_MockObject_MockObject |
| 36 | + */ |
| 37 | + protected $observer; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var Product|\PHPUnit_Framework_MockObject_MockObject |
| 41 | + */ |
| 42 | + protected $product; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var ProductUrlRewriteGenerator|\PHPUnit_Framework_MockObject_MockObject |
| 46 | + */ |
| 47 | + protected $productUrlRewriteGenerator; |
| 48 | + |
| 49 | + /** |
| 50 | + * @SuppressWarnings(PHPMD.TooManyFields) |
| 51 | + */ |
| 52 | + public function setUp() |
| 53 | + { |
| 54 | + $this->urlPersist = $this->getMockBuilder(UrlPersistInterface::class) |
| 55 | + ->disableOriginalConstructor() |
| 56 | + ->getMock(); |
| 57 | + |
| 58 | + $this->productUrlRewriteGenerator = $this->getMockBuilder(ProductUrlRewriteGenerator::class) |
| 59 | + ->disableOriginalConstructor() |
| 60 | + ->getMock(); |
| 61 | + |
| 62 | + $objectManagerHelper = new ObjectManagerHelper($this); |
| 63 | + $this->productProcessUrlRewriteSavingObserver = $objectManagerHelper->getObject( |
| 64 | + ProductProcessUrlRewriteSavingObserver::class, |
| 65 | + [ |
| 66 | + 'productUrlRewriteGenerator' => $this->productUrlRewriteGenerator, |
| 67 | + 'urlPersist' => $this->urlPersist, |
| 68 | + ] |
| 69 | + ); |
| 70 | + |
| 71 | + $this->product = $this->getMockBuilder(Product::class) |
| 72 | + ->disableOriginalConstructor() |
| 73 | + ->setMethods([ |
| 74 | + 'getProductCategories' |
| 75 | + ]) |
| 76 | + ->getMock(); |
| 77 | + |
| 78 | + $event = $this->getMock(Event::class, ['getProduct'], [], '', false); |
| 79 | + $event->method('getProduct')->willReturn($this->product); |
| 80 | + |
| 81 | + $this->observer = $this->getMock(Observer::class, ['getEvent'], [], '', false); |
| 82 | + $this->observer->method('getEvent')->willReturn($event); |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @dataProvider visibilityProvider |
| 88 | + * @param integer $before |
| 89 | + * @param integer $after |
| 90 | + */ |
| 91 | + public function testVisibility($before, $after) |
| 92 | + { |
| 93 | + $this->product->setOrigData('visibility', $before); |
| 94 | + $this->product->setData('visibility', $after); |
| 95 | + |
| 96 | + $this->productUrlRewriteGenerator->expects(static::once()) |
| 97 | + ->method('generate') |
| 98 | + ->with($this->product) |
| 99 | + ->willReturn(['test']); |
| 100 | + $this->urlPersist->expects(static::once()) |
| 101 | + ->method('replace') |
| 102 | + ->with(['test']); |
| 103 | + |
| 104 | + $this->productProcessUrlRewriteSavingObserver->execute($this->observer); |
| 105 | + } |
| 106 | + |
| 107 | + public function visibilityProvider() |
| 108 | + { |
| 109 | + return [ |
| 110 | + ['origData' => Visibility::VISIBILITY_NOT_VISIBLE, 'data' => Visibility::VISIBILITY_IN_CATALOG], |
| 111 | + ['origData' => null, 'data' => Visibility::VISIBILITY_IN_CATALOG], |
| 112 | + ['origData' => Visibility::VISIBILITY_BOTH, 'data' => Visibility::VISIBILITY_IN_SEARCH], |
| 113 | + ['origData' => Visibility::VISIBILITY_IN_CATALOG, 'data' => Visibility::VISIBILITY_NOT_VISIBLE], |
| 114 | + ['origData' => null, 'data' => Visibility::VISIBILITY_NOT_VISIBLE], |
| 115 | + ]; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * @dataProvider notVisibilityProvider |
| 120 | + * @param integer $before |
| 121 | + * @param integer $after |
| 122 | + */ |
| 123 | + public function testNotVisibility($before, $after) |
| 124 | + { |
| 125 | + $this->product->setOrigData('visibility', $before); |
| 126 | + $this->product->setData('visibility', $after); |
| 127 | + |
| 128 | + $this->productUrlRewriteGenerator->expects(static::never())->method('generate'); |
| 129 | + $this->urlPersist->expects(static::never())->method('replace'); |
| 130 | + |
| 131 | + $this->productProcessUrlRewriteSavingObserver->execute($this->observer); |
| 132 | + } |
| 133 | + |
| 134 | + public function notVisibilityProvider() |
| 135 | + { |
| 136 | + return [ |
| 137 | + ['origData' => Visibility::VISIBILITY_IN_SEARCH, 'data' => Visibility::VISIBILITY_IN_SEARCH], |
| 138 | + ]; |
| 139 | + } |
| 140 | +} |
0 commit comments