initResultFactoryMock(); $this->requestMock = $this->getMockBuilder(RequestInterface::class) ->disableOriginalConstructor() ->setMethods(['getParam']) ->getMock(); $this->commandMock = $this->getMockBuilder(GetPaymentNonceCommand::class) ->disableOriginalConstructor() ->setMethods(['execute', '__wakeup']) ->getMock(); $this->commandResultMock = $this->getMockBuilder(CommandResultInterface::class) ->setMethods(['get']) ->getMock(); $this->sessionMock = $this->getMockBuilder(Session::class) ->disableOriginalConstructor() ->setMethods(['getCustomerId', 'getStoreId']) ->getMock(); $this->sessionMock->expects(static::once()) ->method('getStoreId') ->willReturn(null); $this->loggerMock = $this->createMock(LoggerInterface::class); $context = $this->getMockBuilder(Context::class) ->disableOriginalConstructor() ->getMock(); $context->expects(static::any()) ->method('getRequest') ->willReturn($this->requestMock); $context->expects(static::any()) ->method('getResultFactory') ->willReturn($this->resultFactoryMock); $managerHelper = new ObjectManager($this); $this->action = $managerHelper->getObject(GetNonce::class, [ 'context' => $context, 'logger' => $this->loggerMock, 'session' => $this->sessionMock, 'command' => $this->commandMock, ]); } /** * @covers \Magento\Braintree\Controller\Payment\GetNonce::execute */ public function testExecuteWithException() { $this->requestMock->expects(static::once()) ->method('getParam') ->with('public_hash') ->willReturn(null); $this->sessionMock->expects(static::once()) ->method('getCustomerId') ->willReturn(null); $exception = new \Exception('The "publicHash" field does not exists'); $this->commandMock->expects(static::once()) ->method('execute') ->willThrowException($exception); $this->loggerMock->expects(static::once()) ->method('critical') ->with($exception); $this->resultMock->expects(static::once()) ->method('setHttpResponseCode') ->with(Exception::HTTP_BAD_REQUEST); $this->resultMock->expects(static::once()) ->method('setData') ->with(['message' => 'Sorry, but something went wrong']); $this->action->execute(); } /** * @covers \Magento\Braintree\Controller\Payment\GetNonce::execute */ public function testExecute() { $customerId = 1; $publicHash = '65b7bae0dcb690d93'; $nonce = 'f1hc45'; $this->requestMock->expects(static::once()) ->method('getParam') ->with('public_hash') ->willReturn($publicHash); $this->sessionMock->expects(static::once()) ->method('getCustomerId') ->willReturn($customerId); $this->commandResultMock->expects(static::once()) ->method('get') ->willReturn([ 'paymentMethodNonce' => $nonce ]); $this->commandMock->expects(static::once()) ->method('execute') ->willReturn($this->commandResultMock); $this->resultMock->expects(static::once()) ->method('setData') ->with(['paymentMethodNonce' => $nonce]); $this->loggerMock->expects(static::never()) ->method('critical'); $this->resultMock->expects(static::never()) ->method('setHttpResponseCode'); $this->action->execute(); } /** * Create mock for result factory object */ private function initResultFactoryMock() { $this->resultMock = $this->getMockBuilder(ResultInterface::class) ->setMethods(['setHttpResponseCode', 'renderResult', 'setHeader', 'setData']) ->getMock(); $this->resultFactoryMock = $this->getMockBuilder(ResultFactory::class) ->disableOriginalConstructor() ->setMethods(['create']) ->getMock(); $this->resultFactoryMock->expects(static::once()) ->method('create') ->willReturn($this->resultMock); } }