getMockForAbstractClass(LoggerInterface::class); $this->loggerMock = $this->getMockBuilder(Logger::class) ->disableOriginalConstructor() ->getMock(); $this->adapterMock = $this->getMockBuilder(BraintreeAdapter::class) ->disableOriginalConstructor() ->getMock(); /** @var BraintreeAdapterFactory|MockObject $adapterFactoryMock */ $adapterFactoryMock = $this->getMockBuilder(BraintreeAdapterFactory::class) ->disableOriginalConstructor() ->getMock(); $adapterFactoryMock->expects(self::once()) ->method('create') ->willReturn($this->adapterMock); $this->client = new TransactionRefund($criticalLoggerMock, $this->loggerMock, $adapterFactoryMock); } /** * @return void * * @expectedException \Magento\Payment\Gateway\Http\ClientException * @expectedExceptionMessage Test messages */ public function testPlaceRequestException() { $this->loggerMock->expects($this->once()) ->method('debug') ->with( [ 'request' => $this->getTransferData(), 'client' => TransactionRefund::class, 'response' => [], ] ); $this->adapterMock->expects($this->once()) ->method('refund') ->with($this->transactionId, $this->paymentAmount) ->willThrowException(new \Exception('Test messages')); /** @var TransferInterface|MockObject $transferObjectMock */ $transferObjectMock = $this->getTransferObjectMock(); $this->client->placeRequest($transferObjectMock); } /** * @return void */ public function testPlaceRequestSuccess() { $response = new \stdClass; $response->success = true; $this->adapterMock->expects($this->once()) ->method('refund') ->with($this->transactionId, $this->paymentAmount) ->willReturn($response); $this->loggerMock->expects($this->once()) ->method('debug') ->with( [ 'request' => $this->getTransferData(), 'client' => TransactionRefund::class, 'response' => ['success' => 1], ] ); $actualResult = $this->client->placeRequest($this->getTransferObjectMock()); $this->assertTrue(is_object($actualResult['object'])); $this->assertEquals(['object' => $response], $actualResult); } /** * Creates mock object for TransferInterface. * * @return TransferInterface|MockObject */ private function getTransferObjectMock() { $transferObjectMock = $this->createMock(TransferInterface::class); $transferObjectMock->expects($this->once()) ->method('getBody') ->willReturn($this->getTransferData()); return $transferObjectMock; } /** * Creates stub request data. * * @return array */ private function getTransferData() { return [ 'transaction_id' => $this->transactionId, PaymentDataBuilder::AMOUNT => $this->paymentAmount, ]; } }