|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types = 1); |
| 7 | + |
| 8 | +namespace Magento\Review\Test\Unit\Observer; |
| 9 | + |
| 10 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 11 | +use Magento\Framework\App\Response\RedirectInterface; |
| 12 | +use Magento\Framework\App\ResponseInterface; |
| 13 | +use Magento\Framework\Event\Observer; |
| 14 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 15 | +use Magento\Framework\UrlInterface; |
| 16 | +use Magento\Review\Observer\PredispatchReviewObserver; |
| 17 | +use Magento\Store\Model\ScopeInterface; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | + |
| 20 | +/** |
| 21 | + * Test class for \Magento\Review\Observer\PredispatchReviewObserver |
| 22 | + */ |
| 23 | +class PredispatchReviewObserverTest extends TestCase |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var Observer|\PHPUnit_Framework_MockObject_MockObject |
| 27 | + */ |
| 28 | + private $mockObject; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject |
| 32 | + */ |
| 33 | + private $configMock; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var UrlInterface|\PHPUnit_Framework_MockObject_MockObject |
| 37 | + */ |
| 38 | + private $urlMock; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var \Magento\Framework\App\Response\RedirectInterface|\PHPUnit_Framework_MockObject_MockObject |
| 42 | + */ |
| 43 | + private $redirectMock; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var ResponseInterface|\PHPUnit_Framework_MockObject_MockObject |
| 47 | + */ |
| 48 | + private $responseMock; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var ObjectManager |
| 52 | + */ |
| 53 | + private $objectManager; |
| 54 | + |
| 55 | + /** |
| 56 | + * @inheritdoc |
| 57 | + */ |
| 58 | + protected function setUp() : void |
| 59 | + { |
| 60 | + $this->configMock = $this->getMockBuilder(ScopeConfigInterface::class) |
| 61 | + ->disableOriginalConstructor() |
| 62 | + ->getMock(); |
| 63 | + $this->urlMock = $this->getMockBuilder(UrlInterface::class) |
| 64 | + ->disableOriginalConstructor() |
| 65 | + ->getMock(); |
| 66 | + $this->responseMock = $this->getMockBuilder(ResponseInterface::class) |
| 67 | + ->disableOriginalConstructor() |
| 68 | + ->setMethods(['setRedirect']) |
| 69 | + ->getMockForAbstractClass(); |
| 70 | + $this->redirectMock = $this->getMockBuilder(RedirectInterface::class) |
| 71 | + ->getMock(); |
| 72 | + $this->objectManager = new ObjectManager($this); |
| 73 | + $this->mockObject = $this->objectManager->getObject( |
| 74 | + PredispatchReviewObserver::class, |
| 75 | + [ |
| 76 | + 'scopeConfig' => $this->configMock, |
| 77 | + 'url' => $this->urlMock |
| 78 | + ] |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Test with enabled review active config. |
| 84 | + */ |
| 85 | + public function testReviewEnabled() : void |
| 86 | + { |
| 87 | + $observerMock = $this->getMockBuilder(Observer::class) |
| 88 | + ->disableOriginalConstructor() |
| 89 | + ->setMethods(['getResponse', 'getData', 'setRedirect']) |
| 90 | + ->getMockForAbstractClass(); |
| 91 | + |
| 92 | + $this->configMock->method('getValue') |
| 93 | + ->with(PredispatchReviewObserver::XML_PATH_REVIEW_ACTIVE, ScopeInterface::SCOPE_STORE) |
| 94 | + ->willReturn(true); |
| 95 | + $observerMock->expects($this->never()) |
| 96 | + ->method('getData') |
| 97 | + ->with('controller_action') |
| 98 | + ->willReturnSelf(); |
| 99 | + |
| 100 | + $observerMock->expects($this->never()) |
| 101 | + ->method('getResponse') |
| 102 | + ->willReturnSelf(); |
| 103 | + |
| 104 | + $this->assertNull($this->mockObject->execute($observerMock)); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Test with disabled review active config. |
| 109 | + */ |
| 110 | + public function testReviewDisabled() : void |
| 111 | + { |
| 112 | + $observerMock = $this->getMockBuilder(Observer::class) |
| 113 | + ->disableOriginalConstructor() |
| 114 | + ->setMethods(['getControllerAction', 'getResponse']) |
| 115 | + ->getMockForAbstractClass(); |
| 116 | + |
| 117 | + $this->configMock->expects($this->at(0)) |
| 118 | + ->method('getValue') |
| 119 | + ->with(PredispatchReviewObserver::XML_PATH_REVIEW_ACTIVE, ScopeInterface::SCOPE_STORE) |
| 120 | + ->willReturn(false); |
| 121 | + |
| 122 | + $expectedRedirectUrl = 'https://test.com/index'; |
| 123 | + |
| 124 | + $this->configMock->expects($this->at(1)) |
| 125 | + ->method('getValue') |
| 126 | + ->with('web/default/no_route', ScopeInterface::SCOPE_STORE) |
| 127 | + ->willReturn($expectedRedirectUrl); |
| 128 | + |
| 129 | + $this->urlMock->expects($this->once()) |
| 130 | + ->method('getUrl') |
| 131 | + ->willReturn($expectedRedirectUrl); |
| 132 | + |
| 133 | + $observerMock->expects($this->once()) |
| 134 | + ->method('getControllerAction') |
| 135 | + ->willReturnSelf(); |
| 136 | + |
| 137 | + $observerMock->expects($this->once()) |
| 138 | + ->method('getResponse') |
| 139 | + ->willReturn($this->responseMock); |
| 140 | + |
| 141 | + $this->responseMock->expects($this->once()) |
| 142 | + ->method('setRedirect') |
| 143 | + ->with($expectedRedirectUrl); |
| 144 | + |
| 145 | + $this->assertNull($this->mockObject->execute($observerMock)); |
| 146 | + } |
| 147 | +} |
0 commit comments