configMock = $this->getMockForAbstractClass(ScopeConfigInterface::class); $this->urlMock = $this->getMockForAbstractClass(UrlInterface::class); $this->responseMock = $this->getMockBuilder(ResponseInterface::class) ->disableOriginalConstructor() ->setMethods(['setRedirect']) ->getMockForAbstractClass(); $this->redirectMock = $this->getMockForAbstractClass(RedirectInterface::class); $this->newsletterConfig = $this->createMock(Config::class); $this->objectManager = new ObjectManager($this); $this->mockObjectMock = new PredispatchNewsletterObserver( $this->configMock, $this->urlMock, $this->newsletterConfig ); } /** * Test with enabled newsletter active config. */ public function testNewsletterEnabled() : void { $observerMock = $this->getMockBuilder(Observer::class) ->disableOriginalConstructor() ->setMethods(['getResponse', 'getData', 'setRedirect']) ->getMockForAbstractClass(); $this->newsletterConfig->expects($this->once()) ->method('isActive') ->with(ScopeInterface::SCOPE_STORE) ->willReturn(true); $observerMock->expects($this->never()) ->method('getData') ->with('controller_action') ->willReturnSelf(); $observerMock->expects($this->never()) ->method('getResponse') ->willReturnSelf(); $this->assertNull($this->mockObjectMock->execute($observerMock)); } /** * Test with disabled newsletter active config. */ public function testNewsletterDisabled() : void { $observerMock = $this->getMockBuilder(Observer::class) ->disableOriginalConstructor() ->setMethods(['getControllerAction', 'getResponse']) ->getMockForAbstractClass(); $this->newsletterConfig->expects($this->once()) ->method('isActive') ->with(ScopeInterface::SCOPE_STORE) ->willReturn(false); $expectedRedirectUrl = 'https://test.com/index'; $this->configMock->expects($this->once()) ->method('getValue') ->with('web/default/no_route', ScopeInterface::SCOPE_STORE) ->willReturn($expectedRedirectUrl); $this->urlMock->expects($this->once()) ->method('getUrl') ->willReturn($expectedRedirectUrl); $observerMock->expects($this->once()) ->method('getControllerAction') ->willReturnSelf(); $observerMock->expects($this->once()) ->method('getResponse') ->willReturn($this->responseMock); $this->responseMock->expects($this->once()) ->method('setRedirect') ->with($expectedRedirectUrl); $this->assertNull($this->mockObjectMock->execute($observerMock)); } }