/** * @inheritdoc */ public function execute() { $response = $this->resultFactory->create(ResultFactory::TYPE_JSON); try { $publicHash = $this->getRequest()->getParam('public_hash'); $customerId = $this->session->getCustomerId(); $result = $this->command->execute(['public_hash' => $publicHash, 'customer_id' => $customerId])->get(); $response->setData(['paymentMethodNonce' => $result['paymentMethodNonce']]); } catch (\Exception $e) { $this->logger->critical($e); return $this->processBadRequest($response); } return $response; }
/** * @covers \Magento\BraintreeTwo\Gateway\Command\GetPaymentNonceCommand::execute */ public function testExecute() { $publicHash = '3wv2m24d2er3'; $customerId = 1; $token = 'jd2vnq'; $nonce = 's1dj23'; $this->subjectReader->expects(static::once())->method('readPublicHash')->willReturn($publicHash); $this->subjectReader->expects(static::once())->method('readCustomerId')->willReturn($customerId); $this->tokenManagement->expects(static::once())->method('getByPublicHash')->with($publicHash, $customerId)->willReturn($this->paymentToken); $this->paymentToken->expects(static::once())->method('getGatewayToken')->willReturn($token); $obj = new \stdClass(); $obj->success = true; $obj->paymentMethodNonce = new \stdClass(); $obj->paymentMethodNonce->nonce = $nonce; $this->adapter->expects(static::once())->method('createNonce')->with($token)->willReturn($obj); $this->responseValidator->expects(static::once())->method('validate')->with(['response' => ['object' => $obj]])->willReturn($this->validationResult); $this->validationResult->expects(static::once())->method('isValid')->willReturn(true); $this->validationResult->expects(static::never())->method('getFailsDescription'); $expected = $this->getMockBuilder(ArrayResult::class)->disableOriginalConstructor()->setMethods(['get'])->getMock(); $expected->expects(static::once())->method('get')->willReturn(['paymentMethodNonce' => $nonce]); $this->resultFactory->expects(static::once())->method('create')->willReturn($expected); $actual = $this->command->execute(['publicHash' => $publicHash, 'customerId' => $customerId]); static::assertEquals($expected, $actual); static::assertEquals($nonce, $actual->get()['paymentMethodNonce']); }