requestBuilderMock = $this->createMock(BuilderInterface::class); $this->transferFactoryMock = $this->createMock(TransferFactoryInterface::class); $this->transferMock = $this->createMock(TransferInterface::class); $this->clientMock = $this->createMock(ClientInterface::class); $this->loggerMock = $this->createMock(LoggerInterface::class); $this->validatorMock = $this->createMock(ValidatorInterface::class); $this->command = new GatewayQueryCommand( $this->requestBuilderMock, $this->transferFactoryMock, $this->clientMock, $this->loggerMock, $this->validatorMock ); } public function testNormalExecution() { $buildSubject = [ 'foo' => '123' ]; $request = [ 'bar' => '321' ]; $response = [ 'transaction' => [ 'transactionType' => 'foo', 'transactionStatus' => 'bar', 'responseCode' => 'baz' ] ]; $validationSubject = $buildSubject; $validationSubject['response'] = $response; $this->requestBuilderMock->method('build') ->with($buildSubject) ->willReturn($request); $this->transferFactoryMock->method('create') ->with($request) ->willReturn($this->transferMock); $this->clientMock->method('placeRequest') ->with($this->transferMock) ->willReturn($response); $this->validatorMock->method('validate') ->with($validationSubject) ->willReturn(new Result(true)); $result = $this->command->execute($buildSubject); $this->assertInstanceOf(ArrayResult::class, $result); $this->assertEquals($response, $result->get()); } /** * @expectedExceptionMessage There was an error while trying to process the request. * @expectedException \Magento\Payment\Gateway\Command\CommandException */ public function testExceptionIsThrownAndLoggedWhenRequestFails() { $buildSubject = [ 'foo' => '123' ]; $request = [ 'bar' => '321' ]; $this->requestBuilderMock->method('build') ->with($buildSubject) ->willReturn($request); $this->transferFactoryMock->method('create') ->with($request) ->willReturn($this->transferMock); $e = new \Exception('foobar'); $this->clientMock->method('placeRequest') ->with($this->transferMock) ->willThrowException($e); // assert the exception is logged $this->loggerMock->expects($this->once()) ->method('critical') ->with($e); $this->command->execute($buildSubject); } /** * @expectedExceptionMessage There was an error while trying to process the request. * @expectedException \Magento\Payment\Gateway\Command\CommandException */ public function testExceptionIsThrownWhenResponseIsInvalid() { $buildSubject = [ 'foo' => '123' ]; $request = [ 'bar' => '321' ]; $response = [ 'baz' => '456' ]; $validationSubject = $buildSubject; $validationSubject['response'] = $response; $this->requestBuilderMock->method('build') ->with($buildSubject) ->willReturn($request); $this->transferFactoryMock->method('create') ->with($request) ->willReturn($this->transferMock); $this->clientMock->method('placeRequest') ->with($this->transferMock) ->willReturn($response); $this->validatorMock->method('validate') ->with($validationSubject) ->willReturn(new Result(false)); $this->command->execute($buildSubject); } }