Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GetNonceTest.php
Go to the documentation of this file.
1 <?php
7 
17 use Magento\Payment\Gateway\Command\ResultInterface as CommandResultInterface;
18 use PHPUnit_Framework_MockObject_MockObject as MockObject;
19 use Psr\Log\LoggerInterface;
20 
26 class GetNonceTest extends \PHPUnit\Framework\TestCase
27 {
31  private $action;
32 
36  private $commandMock;
37 
41  private $sessionMock;
42 
46  private $loggerMock;
47 
51  private $resultFactoryMock;
52 
56  private $resultMock;
57 
61  private $requestMock;
62 
66  private $commandResultMock;
67 
68  protected function setUp()
69  {
70  $this->initResultFactoryMock();
71 
72  $this->requestMock = $this->getMockBuilder(RequestInterface::class)
73  ->disableOriginalConstructor()
74  ->setMethods(['getParam'])
75  ->getMock();
76 
77  $this->commandMock = $this->getMockBuilder(GetPaymentNonceCommand::class)
78  ->disableOriginalConstructor()
79  ->setMethods(['execute', '__wakeup'])
80  ->getMock();
81 
82  $this->commandResultMock = $this->getMockBuilder(CommandResultInterface::class)
83  ->setMethods(['get'])
84  ->getMock();
85 
86  $this->sessionMock = $this->getMockBuilder(Session::class)
87  ->disableOriginalConstructor()
88  ->setMethods(['getCustomerId', 'getStoreId'])
89  ->getMock();
90  $this->sessionMock->expects(static::once())
91  ->method('getStoreId')
92  ->willReturn(null);
93 
94  $this->loggerMock = $this->createMock(LoggerInterface::class);
95 
96  $context = $this->getMockBuilder(Context::class)
97  ->disableOriginalConstructor()
98  ->getMock();
99  $context->expects(static::any())
100  ->method('getRequest')
101  ->willReturn($this->requestMock);
102  $context->expects(static::any())
103  ->method('getResultFactory')
104  ->willReturn($this->resultFactoryMock);
105 
106  $managerHelper = new ObjectManager($this);
107  $this->action = $managerHelper->getObject(GetNonce::class, [
108  'context' => $context,
109  'logger' => $this->loggerMock,
110  'session' => $this->sessionMock,
111  'command' => $this->commandMock,
112  ]);
113  }
114 
118  public function testExecuteWithException()
119  {
120  $this->requestMock->expects(static::once())
121  ->method('getParam')
122  ->with('public_hash')
123  ->willReturn(null);
124 
125  $this->sessionMock->expects(static::once())
126  ->method('getCustomerId')
127  ->willReturn(null);
128 
129  $exception = new \Exception('The "publicHash" field does not exists');
130  $this->commandMock->expects(static::once())
131  ->method('execute')
132  ->willThrowException($exception);
133 
134  $this->loggerMock->expects(static::once())
135  ->method('critical')
136  ->with($exception);
137 
138  $this->resultMock->expects(static::once())
139  ->method('setHttpResponseCode')
141  $this->resultMock->expects(static::once())
142  ->method('setData')
143  ->with(['message' => 'Sorry, but something went wrong']);
144 
145  $this->action->execute();
146  }
147 
151  public function testExecute()
152  {
153  $customerId = 1;
154  $publicHash = '65b7bae0dcb690d93';
155  $nonce = 'f1hc45';
156 
157  $this->requestMock->expects(static::once())
158  ->method('getParam')
159  ->with('public_hash')
160  ->willReturn($publicHash);
161 
162  $this->sessionMock->expects(static::once())
163  ->method('getCustomerId')
164  ->willReturn($customerId);
165 
166  $this->commandResultMock->expects(static::once())
167  ->method('get')
168  ->willReturn([
169  'paymentMethodNonce' => $nonce
170  ]);
171  $this->commandMock->expects(static::once())
172  ->method('execute')
173  ->willReturn($this->commandResultMock);
174 
175  $this->resultMock->expects(static::once())
176  ->method('setData')
177  ->with(['paymentMethodNonce' => $nonce]);
178 
179  $this->loggerMock->expects(static::never())
180  ->method('critical');
181 
182  $this->resultMock->expects(static::never())
183  ->method('setHttpResponseCode');
184 
185  $this->action->execute();
186  }
187 
191  private function initResultFactoryMock()
192  {
193  $this->resultMock = $this->getMockBuilder(ResultInterface::class)
194  ->setMethods(['setHttpResponseCode', 'renderResult', 'setHeader', 'setData'])
195  ->getMock();
196 
197  $this->resultFactoryMock = $this->getMockBuilder(ResultFactory::class)
198  ->disableOriginalConstructor()
199  ->setMethods(['create'])
200  ->getMock();
201 
202  $this->resultFactoryMock->expects(static::once())
203  ->method('create')
204  ->willReturn($this->resultMock);
205  }
206 }