paymentTokenManagement = $this->getMock(PaymentTokenManagementInterface::class); $this->observer = new PaymentTokenAssigner($this->paymentTokenManagement); } public function testExecuteNoPublicHash() { $dataObject = new DataObject(); $observer = $this->getPreparedObserverWithMap( [ [AbstractDataAssignObserver::DATA_CODE, $dataObject] ] ); $this->paymentTokenManagement->expects(static::never()) ->method('getByPublicHash'); $this->observer->execute($observer); } public function testExecuteNotOrderPaymentModel() { $dataObject = new DataObject( [ PaymentInterface::KEY_ADDITIONAL_DATA => [ PaymentTokenInterface::PUBLIC_HASH => 'public_hash_value' ] ] ); $paymentModel = $this->getMock(InfoInterface::class); $observer = $this->getPreparedObserverWithMap( [ [AbstractDataAssignObserver::DATA_CODE, $dataObject], [AbstractDataAssignObserver::MODEL_CODE, $paymentModel] ] ); $this->paymentTokenManagement->expects(static::never()) ->method('getByPublicHash'); $this->observer->execute($observer); } public function testExecuteNoCustomerId() { $dataObject = new DataObject( [ PaymentInterface::KEY_ADDITIONAL_DATA => [ PaymentTokenInterface::PUBLIC_HASH => 'public_hash_value' ] ] ); $paymentModel = $this->getMockBuilder(Payment::class) ->disableOriginalConstructor() ->getMock(); $quote = $this->getMock(CartInterface::class); $customer = $this->getMock(CustomerInterface::class); $paymentModel->expects(static::once()) ->method('getQuote') ->willReturn($quote); $quote->expects(static::once()) ->method('getCustomer') ->willReturn($customer); $customer->expects(static::once()) ->method('getId') ->willReturn(null); $observer = $this->getPreparedObserverWithMap( [ [AbstractDataAssignObserver::DATA_CODE, $dataObject], [AbstractDataAssignObserver::MODEL_CODE, $paymentModel] ] ); $this->paymentTokenManagement->expects(static::never()) ->method('getByPublicHash'); $this->observer->execute($observer); } public function testExecuteNoPaymentToken() { $customerId = 1; $publicHash = 'public_hash_value'; $dataObject = new DataObject( [ PaymentInterface::KEY_ADDITIONAL_DATA => [ PaymentTokenInterface::PUBLIC_HASH => $publicHash ] ] ); $paymentModel = $this->getMockBuilder(Payment::class) ->disableOriginalConstructor() ->getMock(); $quote = $this->getMock(CartInterface::class); $customer = $this->getMock(CustomerInterface::class); $paymentModel->expects(static::once()) ->method('getQuote') ->willReturn($quote); $quote->expects(static::once()) ->method('getCustomer') ->willReturn($customer); $customer->expects(static::once()) ->method('getId') ->willReturn($customerId); $this->paymentTokenManagement->expects(static::once()) ->method('getByPublicHash') ->with($publicHash, $customerId) ->willReturn(null); $observer = $this->getPreparedObserverWithMap( [ [AbstractDataAssignObserver::DATA_CODE, $dataObject], [AbstractDataAssignObserver::MODEL_CODE, $paymentModel] ] ); $paymentModel->expects(static::never()) ->method('setAdditionalInformation'); $this->observer->execute($observer); } public function testExecuteSaveMetadata() { $customerId = 1; $publicHash = 'public_hash_value'; $dataObject = new DataObject( [ PaymentInterface::KEY_ADDITIONAL_DATA => [ PaymentTokenInterface::PUBLIC_HASH => $publicHash ] ] ); $paymentModel = $this->getMockBuilder(Payment::class) ->disableOriginalConstructor() ->getMock(); $quote = $this->getMock(CartInterface::class); $customer = $this->getMock(CustomerInterface::class); $paymentToken = $this->getMock(PaymentTokenInterface::class); $paymentModel->expects(static::once()) ->method('getQuote') ->willReturn($quote); $quote->expects(static::once()) ->method('getCustomer') ->willReturn($customer); $customer->expects(static::once()) ->method('getId') ->willReturn($customerId); $this->paymentTokenManagement->expects(static::once()) ->method('getByPublicHash') ->with($publicHash, $customerId) ->willReturn($paymentToken); $paymentModel->expects(static::once()) ->method('setAdditionalInformation') ->with( Vault::TOKEN_METADATA_KEY, [ PaymentTokenInterface::CUSTOMER_ID => $customerId, PaymentTokenInterface::PUBLIC_HASH => $publicHash ] ); $observer = $this->getPreparedObserverWithMap( [ [AbstractDataAssignObserver::DATA_CODE, $dataObject], [AbstractDataAssignObserver::MODEL_CODE, $paymentModel] ] ); $this->observer->execute($observer); } /** * @param array $returnMap * @return \PHPUnit_Framework_MockObject_MockObject|Observer */ private function getPreparedObserverWithMap(array $returnMap) { $observer = $this->getMockBuilder(Observer::class) ->disableOriginalConstructor() ->getMock(); $event = $this->getMockBuilder(Event::class) ->disableOriginalConstructor() ->getMock(); $observer->expects(static::atLeastOnce()) ->method('getEvent') ->willReturn($event); $event->expects(static::atLeastOnce()) ->method('getDataByKey') ->willReturnMap( $returnMap ); return $observer; } }