commandPool = $this->getMockBuilder(CommandPoolInterface::class) ->disableOriginalConstructor() ->setMethods(['get', '__wakeup']) ->getMock(); $this->initCommandMock(); $this->initTransactionRepositoryMock(); $this->initFilterBuilderMock(); $this->initSearchCriteriaBuilderMock(); $this->coreHelper = $this->getMockBuilder(\Amazon\Core\Helper\Data::class) ->disableOriginalConstructor() ->getMock(); $this->strategyCommand = new CaptureStrategyCommand( $this->commandPool, $this->transactionRepository, $this->searchCriteriaBuilder, $this->filterBuilder, $this->coreHelper ); } /** * Tests if command strategy class returns correct command value when item is authorized but not captured * @throws \Magento\Payment\Gateway\Command\CommandException */ public function testSaleExecute() { $paymentData = $this->getPaymentDataObjectMock(); $subject['payment'] = $paymentData; $this->payment->method('getAuthorizationTransaction') ->willReturn(false); $this->payment->method('getId') ->willReturn(1); $this->coreHelper->method('getPaymentAction')->willReturn('authorize_capture'); $this->buildSearchCriteria(); $this->transactionRepository->method('getTotalCount') ->willReturn(0); $this->commandPool->method('get') ->with(CaptureStrategyCommand::SALE) ->willReturn($this->command); $this->strategyCommand->execute($subject); } /** * Tests if command strategy class returns correct command value when item is to be authorized and captured * @throws \Magento\Payment\Gateway\Command\CommandException */ public function testCaptureExecute() { $paymentData = $this->getPaymentDataObjectMock(); $subject['payment'] = $paymentData; $lastTransId = 'transaction_id'; $this->payment->method('getAuthorizationTransaction') ->willReturn(true); $this->payment->method('getLastTransId') ->willReturn($lastTransId); $this->payment->method('getId') ->willReturn(1); $this->buildSearchCriteria(); $this->transactionRepository->method('getTotalCount') ->willReturn(0); $this->commandPool->method('get') ->with(CaptureStrategyCommand::CAPTURE) ->willReturn($this->command); $this->strategyCommand->execute($subject); } /** * Creates mock for payment data object and order payment * @return MockObject */ private function getPaymentDataObjectMock() { $this->payment = $this->getMockBuilder(Payment::class) ->disableOriginalConstructor() ->getMock(); $mock = $this->getMockBuilder(PaymentDataObject::class) ->setMethods(['getPayment', 'getOrder']) ->disableOriginalConstructor() ->getMock(); $mock->method('getPayment') ->willReturn($this->payment); $order = $this->getMockBuilder(OrderAdapterInterface::class) ->disableOriginalConstructor() ->getMock(); $mock->method('getOrder') ->willReturn($order); return $mock; } /** * Creates mock for gateway command object */ private function initCommandMock() { $this->command = $this->getMockBuilder(GatewayCommand::class) ->disableOriginalConstructor() ->setMethods(['execute']) ->getMock(); $this->command->method('execute') ->willReturn([]); } /** * Creates mock for filter object */ private function initFilterBuilderMock() { $this->filterBuilder = $this->getMockBuilder(FilterBuilder::class) ->disableOriginalConstructor() ->setMethods(['setField', 'setValue', 'create', '__wakeup']) ->getMock(); } /** * Builds search criteria */ private function buildSearchCriteria() { $this->filterBuilder->expects(self::exactly(2)) ->method('setField') ->willReturnSelf(); $this->filterBuilder->expects(self::exactly(2)) ->method('setValue') ->willReturnSelf(); $searchCriteria = new SearchCriteria(); $this->searchCriteriaBuilder->expects(self::exactly(2)) ->method('addFilters') ->willReturnSelf(); $this->searchCriteriaBuilder->method('create') ->willReturn($searchCriteria); $this->transactionRepository->method('getList') ->with($searchCriteria) ->willReturnSelf(); } /** * Create mock for search criteria object */ private function initSearchCriteriaBuilderMock() { $this->searchCriteriaBuilder = $this->getMockBuilder(SearchCriteriaBuilder::class) ->disableOriginalConstructor() ->setMethods(['addFilters', 'create', '__wakeup']) ->getMock(); } /** * Create mock for transaction repository */ private function initTransactionRepositoryMock() { $this->transactionRepository = $this->getMockBuilder(TransactionRepositoryInterface::class) ->disableOriginalConstructor() ->setMethods(['getList', 'getTotalCount', 'delete', 'get', 'save', 'create', '__wakeup']) ->getMock(); } }