|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\Catalog\Test\Unit\Observer; |
| 8 | + |
| 9 | +use Magento\Catalog\Helper\Data; |
| 10 | +use Magento\Catalog\Observer\CatalogCheckIsUsingStaticUrlsAllowedObserver; |
| 11 | +use Magento\Framework\Event; |
| 12 | +use Magento\Framework\Event\Observer; |
| 13 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | + |
| 16 | +/** |
| 17 | + * Provide tests for CatalogCheckIsUsingStaticUrlsAllowedObserver observer. |
| 18 | + */ |
| 19 | +class CatalogCheckIsUsingStaticUrlsAllowedObserverTest extends TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * Test subject. |
| 23 | + * |
| 24 | + * @var CatalogCheckIsUsingStaticUrlsAllowedObserver |
| 25 | + */ |
| 26 | + private $model; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var Data|\PHPUnit_Framework_MockObject_MockObject |
| 30 | + */ |
| 31 | + private $catalogData; |
| 32 | + |
| 33 | + /** |
| 34 | + * @inheritdoc |
| 35 | + */ |
| 36 | + protected function setUp() |
| 37 | + { |
| 38 | + $objectManager = new ObjectManager($this); |
| 39 | + $this->catalogData = $this->getMockBuilder(Data::class) |
| 40 | + ->disableOriginalConstructor() |
| 41 | + ->getMock(); |
| 42 | + $this->model = $objectManager->getObject( |
| 43 | + CatalogCheckIsUsingStaticUrlsAllowedObserver::class, |
| 44 | + ['catalogData' => $this->catalogData] |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Test observer can correctly handle non integer store id values. |
| 50 | + * |
| 51 | + * @dataProvider executeDataProvider |
| 52 | + * @param string|int $storeId |
| 53 | + * @return void |
| 54 | + */ |
| 55 | + public function testExecute($storeId) |
| 56 | + { |
| 57 | + $result = new \stdClass(); |
| 58 | + /** @var Observer|\PHPUnit_Framework_MockObject_MockObject $observer */ |
| 59 | + $observer = $this->getMockBuilder(Observer::class) |
| 60 | + ->disableOriginalConstructor() |
| 61 | + ->getMock(); |
| 62 | + $event = $this->getMockBuilder(Event::class) |
| 63 | + ->disableOriginalConstructor() |
| 64 | + ->getMock(); |
| 65 | + $event->expects($this->exactly(2)) |
| 66 | + ->method('getData') |
| 67 | + ->withConsecutive( |
| 68 | + $this->identicalTo('store_id'), |
| 69 | + $this->identicalTo('result') |
| 70 | + )->willReturnOnConsecutiveCalls( |
| 71 | + $storeId, |
| 72 | + $result |
| 73 | + ); |
| 74 | + $observer->expects($this->exactly(2)) |
| 75 | + ->method('getEvent') |
| 76 | + ->willReturn($event); |
| 77 | + $this->catalogData->expects($this->once()) |
| 78 | + ->method('setStoreId') |
| 79 | + ->with(0) |
| 80 | + ->willReturnSelf(); |
| 81 | + $this->catalogData->expects($this->once()) |
| 82 | + ->method('isUsingStaticUrlsAllowed') |
| 83 | + ->willReturn(true); |
| 84 | + $this->model->execute($observer); |
| 85 | + $this->assertTrue($result->isAllowed); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Provide test data for testExecute(). |
| 90 | + * |
| 91 | + * @return array |
| 92 | + */ |
| 93 | + public function executeDataProvider() |
| 94 | + { |
| 95 | + return [ |
| 96 | + [ |
| 97 | + 'store_id' => 0, |
| 98 | + ], |
| 99 | + [ |
| 100 | + 'store_id' => '' |
| 101 | + ] |
| 102 | + ]; |
| 103 | + } |
| 104 | +} |
0 commit comments