public function setUp()
 {
     // Mock session storage for previously collected quantity results
     $this->_inventorySession = $this->getModelMockBuilder('ebayenterprise_inventory/session')->setMethods(['getQuantityResults', 'setQuantityResults', 'getResultsCollectedFor', 'setResultsCollectedFor'])->disableOriginalConstructor()->getMock();
     // Mock of the sdk helper for getting new quantity results from the API
     $this->_quantitySdkHelper = $this->getHelperMock('ebayenterprise_inventory/quantity_sdk', ['requestQuantityForItems']);
     // A quantity results stub, expected to be returned by the session or
     // sdk helper.
     $this->_quantityResults = $this->getModelMockBuilder('ebayenterprise_inventory/quantity_results')->disableOriginalConstructor()->setMethods(['checkResultsApplyToItems'])->getMock();
     // Stub quote object to collect quantity records for.
     $this->_quote = $this->getModelMock('sales/quote', ['getAllItems']);
     // Stub quote item that should be sent to the inventory service.
     $this->_quoteItem = $this->getModelMock('sales/quote_item', []);
     // Quote items and filtered quote items allow tests to distinguish
     // between all items in the quote and just items that matter to the
     // inventory service. All quote items includes an additional quote item
     // that is not expected to be sent to the quantity API.
     $this->_quoteItems = [$this->_quoteItem, $this->getModelMock('sales/quote_item', [])];
     $this->_filteredQuoteItems = [$this->_quoteItem];
     // Stub quote to return all quote items.
     $this->_quote->expects($this->any())->method('getAllItems')->will($this->returnValue($this->_quoteItems));
     // Stub filtering from the full quote items down to the filtered
     // array of quote items. Tests that expect a list of items that matter
     // to the inventory service can expect the filted quote items array
     // instead of the unfiltered quote items array.
     $this->_itemSelection = $this->getHelperMock('ebayenterprise_inventory/item_selection', ['selectFrom']);
     $this->_itemSelection->expects($this->any())->method('selectFrom')->with($this->identicalTo($this->_quoteItems))->will($this->returnValue($this->_filteredQuoteItems));
     // Mock helper for calculating item quantities.
     $this->_quantityHelper = $this->getHelperMock('ebayenterprise_inventory/quantity', ['calculateTotalQuantitiesBySku']);
     $this->_currentItemQuantityData = ['a-sku' => 100];
     $this->_quantityHelper->expects($this->any())->method('calculateTotalQuantitiesBySku')->with($this->_filteredQuoteItems)->will($this->returnValue($this->_currentItemQuantityData));
     $this->_quantityCollector = Mage::getModel('ebayenterprise_inventory/quantity_collector', ['quantity_sdk_helper' => $this->_quantitySdkHelper, 'quantity_helper' => $this->_quantityHelper, 'item_selection' => $this->_itemSelection, 'inventory_session' => $this->_inventorySession]);
 }
 /**
  * When an order is submitted, a new order should be created from the quote,
  * saved with relevant data in a single DB transaction, and finally the new
  * order should be returned.
  */
 public function testSubmitOrderSuccess()
 {
     // Stub out dependencies to get through the basic flow of submitOrder.
     $this->_stubForBasicOrderSubmitCompletion();
     // Side-effect tests: assert required behaviors
     // Ensure inherited methods are called - the behavior of these methods
     // is taken as being correct so the methods are not tested, only that
     // interactions with the methods is maintained.
     $this->_serviceQuote->expects($this->once())->method('_deleteNominalItems')->will($this->returnSelf());
     $this->_serviceQuote->expects($this->once())->method('_validate')->will($this->returnSelf());
     $this->_serviceQuote->expects($this->once())->method('_inactivateQuote')->will($this->returnSelf());
     // Ensure proper events are dispatched while submitting the order.
     // Defer to the checkout dispatcher for ensuring the proper events happen,
     // but still need to ensure the correct ones would be triggered when
     // successfully creating an order.
     $this->_checkoutDispatcher->expects($this->once())->method('dispatchBeforeOrderSubmit')->with($this->identicalTo($this->_quote), $this->identicalTo($this->_order))->will($this->returnSelf());
     $this->_checkoutDispatcher->expects($this->once())->method('dispatchAfterOrderSubmit')->with($this->identicalTo($this->_quote), $this->identicalTo($this->_order))->will($this->returnSelf());
     $this->_checkoutDispatcher->expects($this->once())->method('dispatchOrderSubmitSuccess')->with($this->identicalTo($this->_quote), $this->identicalTo($this->_order))->will($this->returnSelf());
     $this->_checkoutDispatcher->expects($this->never())->method('dispatchOrderSubmitFailure');
     // Ensure that an order is reserved for the quote.
     $this->_quote->expects($this->once())->method('reserveOrderId')->will($this->returnSelf());
     // Ensure that the transaction for order creation is saved successfully.
     // This will result in the order being saved and "created".
     $this->_transaction->expects($this->once())->method('save')->will($this->returnSelf());
     // When all goes successfully, the newly created order object should be
     // returned when submitting the order.
     $this->assertSame($this->_order, $this->_serviceQuote->submitOrder());
 }
 /**
  * When inventory errors are encountered for a child item, error info should
  * be added to the parent item, child item and quote.
  */
 public function testCheckQuoteInventoryParentItemMessages()
 {
     $itemSku = 'the-item';
     $itemName = 'Item Name';
     $itemQty = 5;
     $itemId = 39;
     // Force an error by setting available quantity to be less than requested quantity.
     $availQty = 1;
     // Error messages to be added to the quote and item.
     $quoteErrorMessage = EbayEnterprise_Inventory_Model_Quantity_Service::QUOTE_INSUFFICIENT_STOCK_MESSAGE;
     $itemErrorMessage = EbayEnterprise_Inventory_Model_Quantity_Service::ITEM_INSUFFICIENT_STOCK_MESSAGE;
     // Create a parent item and a child item. Child item will be handled by
     // the service but when errors are present, they need to also be added
     // to the parent.
     $parentQuoteItem = $this->_mockQuoteItem($itemSku, 1, 38, $itemName);
     $quoteItem = $this->_mockQuoteItem($itemSku, $itemQty, $itemId, $itemName, $parentQuoteItem);
     $quoteItems = [$quoteItem];
     $this->_mockQuoteItemSelection($quoteItems);
     // Mock the mechanism used to determine total quantity of an item,
     // can return the expected quantity of the item requested.
     $this->_quantityHelper->expects($this->any())->method('calculateTotalQuantityRequested')->with($this->identicalTo($quoteItem), $this->identicalTo($quoteItems))->will($this->returnValue($itemQty));
     // item which will be returned from the quantity results
     // model when getting the quantity result by sku using the
     // item's sku or id when using the item's id.
     $itemQuantityResult = $this->_createQuantity($availQty, $itemSku, $itemId);
     $this->_mockQuantityResults([['sku' => $itemSku, 'item_id' => $itemId, 'quantity' => $itemQuantityResult]]);
     // Side-effect tests: ensure that the item, parent item and
     // the quote have error info added for the unavailable
     // item.
     $parentQuoteItem->expects($this->once())->method('addErrorInfo')->with($this->identicalTo(EbayEnterprise_Inventory_Model_Quantity_Service::ERROR_INFO_SOURCE), $this->identicalTo(EbayEnterprise_Inventory_Model_Quantity_Service::INSUFFICIENT_STOCK_ERROR_CODE), $this->identicalTo($itemErrorMessage))->will($this->returnSelf());
     $quoteItem->expects($this->once())->method('addErrorInfo')->with($this->identicalTo(EbayEnterprise_Inventory_Model_Quantity_Service::ERROR_INFO_SOURCE), $this->identicalTo(EbayEnterprise_Inventory_Model_Quantity_Service::INSUFFICIENT_STOCK_ERROR_CODE), $this->identicalTo($itemErrorMessage))->will($this->returnSelf());
     $this->_quote->expects($this->once())->method('addErrorInfo')->with($this->identicalTo(EbayEnterprise_Inventory_Model_Quantity_Service::ERROR_INFO_TYPE), $this->identicalTo(EbayEnterprise_Inventory_Model_Quantity_Service::ERROR_INFO_SOURCE), $this->identicalTo(EbayEnterprise_Inventory_Model_Quantity_Service::INSUFFICIENT_STOCK_ERROR_CODE), $this->identicalTo($quoteErrorMessage))->will($this->returnSelf());
     $this->assertSame($this->_quantityService, $this->_quantityService->checkQuoteInventory($this->_quote));
 }
 public function setUp()
 {
     // Mock the log context helper to prevent session hits when
     // getting log metadata.
     $logContext = $this->getHelperMock('ebayenterprise_magelog/context', ['getMetaData']);
     $logContext->expects($this->any())->method('getMetaData')->will($this->returnValue([]));
     // SDK Payloads
     $this->_taxResponsePayload = $this->getMockForAbstractClass('eBayEnterprise\\RetailOrderManagement\\Payload\\TaxDutyFee\\ITaxDutyFeeQuoteReply', ['getShipGroups']);
     $this->_shipGroup = $this->getMockForAbstractClass('eBayEnterprise\\RetailOrderManagement\\Payload\\TaxDutyFee\\ITaxedShipGroup', ['getDestination']);
     $this->_shipGroups = new ArrayIterator([$this->_shipGroup]);
     $this->_shipDestination = $this->getMockForAbstractClass('eBayEnterprise\\RetailOrderManagement\\Payload\\TaxDutyFee\\IDestination', ['getId']);
     // Link together the payloads.
     $this->_taxResponsePayload->expects($this->any())->method('getShipGroups')->will($this->returnValue($this->_shipGroups));
     $this->_shipGroup->expects($this->any())->method('getDestination')->will($this->returnValue($this->_shipDestination));
     $this->_shipDestination->expects($this->any())->method('getId')->will($this->returnValue($this->_destinationId));
     // Mocks needed to map ship groups to quote addresses.
     $this->_quote = $this->getModelMock('sales/quote', ['getAddressesCollection']);
     $this->_addressCollection = $this->getResourceModelMock('sales/quote_address_collection', ['getItemByColumnValue']);
     $this->_address = Mage::getModel('sales/quote_address');
     $this->_quote->expects($this->any())->method('getAddressesCollection')->will($this->returnValue($this->_addressCollection));
     // The address collection should return the expected address when given
     // the expected destination id. Otherwise, should return null.
     $this->_addressCollection->expects($this->any())->method('getItemByColumnValue')->will($this->returnValueMap([['destination_id', $this->_destinationId, $this->_address]]));
     // Mocks for building tax records for ship groups.
     $this->_taxFactory = $this->getHelperMock('ebayenterprise_tax/factory', ['createResponseAddressParser']);
     // Create an address response parser to synthesize extracting additional
     // tax data from addresses. Constructor disabled to prevent needing to
     // provide dependencies.
     $this->_addressParser = $this->getModelMockBuilder('ebayenterprise_tax/response_parser_address')->disableOriginalConstructor()->setMethods(['getTaxRecords', 'getTaxDuties', 'getTaxFees'])->getMock();
     // Create some mock tax data - record, duty and fee - to stand in for tax data
     // extracted from an address. All constructors disabled to prevent needing to
     // provide dependencies.
     $this->_addressTaxRecord = $this->getModelMockBuilder('ebayenterprise_tax/record')->disableOriginalConstructor()->getMock();
     $this->_addressTaxDuty = $this->getModelMockBuilder('ebayenterprise_tax/duty')->disableOriginalConstructor()->getMock();
     $this->_addressTaxFee = $this->getModelMockBuilder('ebayenterprise_tax/fee')->disableOriginalConstructor()->getMock();
     $this->_addressTaxRecords = [$this->_addressTaxRecord];
     $this->_addressTaxDuties = [$this->_addressTaxDuty];
     $this->_addressTaxFees = [$this->_addressTaxFee];
     // Set the expected address parser to return the mocked up set of
     // address tax records.
     $this->_addressParser->expects($this->any())->method('getTaxRecords')->will($this->returnValue($this->_addressTaxRecords));
     $this->_addressParser->expects($this->any())->method('getTaxDuties')->will($this->returnValue($this->_addressTaxDuties));
     $this->_addressParser->expects($this->any())->method('getTaxFees')->will($this->returnValue($this->_addressTaxFees));
     $this->_quoteParser = Mage::getModel('ebayenterprise_tax/response_parser_quote', ['quote' => $this->_quote, 'tax_response' => $this->_taxResponsePayload, 'tax_factory' => $this->_taxFactory, 'log_context' => $logContext]);
 }
 /**
  * Set up the quote and billing address mocks to be considered
  * valid by the tax collector.
  *
  * @return self
  */
 protected function _makeQuoteValid()
 {
     $this->_billingAddress->expects($this->any())->method('getFirstname')->will($this->returnValue('Firstname'));
     $this->_billingAddress->expects($this->any())->method('getLastname')->will($this->returnValue('Lastname'));
     $this->_billingAddress->expects($this->any())->method('getStreetFull')->will($this->returnValue('Street Address'));
     $this->_billingAddress->expects($this->any())->method('getCountryId')->will($this->returnValue('US'));
     $this->_quote->expects($this->any())->method('getItemsCount')->will($this->returnValue(3));
     return $this;
 }
 /**
  * When the tax collector fails to collect new tax data, the session should
  * not get updated and quote totals should not be recollected.
  */
 public function testHandleSalesQuoteCollectTotalsCollectFailure()
 {
     // Indicate new tax collection is required in the session flags.
     $this->coreSession->expects($this->any())->method('isTaxUpdateRequired')->will($this->returnValue(true));
     // Set the tax collector to fail to make the TDF request and throw
     // an exception.
     $this->taxCollector->expects($this->once())->method('collectTaxes')->will($this->throwException(Mage::exception('EbayEnterprise_Tax_Exception_Collector')));
     // Ensure session quote data is not updated, flags are not reset and
     // quote totals are not re-collected.
     $this->coreSession->expects($this->never())->method('updateWithQuote');
     $this->coreSession->expects($this->never())->method('resetTaxUpdateRequired');
     $this->quote->expects($this->never())->method('collectTotals');
     $this->taxObserver->handleSalesQuoteCollectTotalsAfter($this->eventObserver);
 }
 /**
  * When creating a new order fails with an exception, the exception should
  * be thrown from the multishipping checkout type's createOrders and
  * any changes to the quote should not be saved.
  */
 public function testCreateOrdersFailureWithExceptions()
 {
     $exceptionMessage = 'Some exception thrown while submitting order.';
     $orderSubmitException = new Exception($exceptionMessage);
     // If the order cannot be created, an exception may be thrown when
     // submitting the order. Any such exceptions should not be caught and
     // should prevent the quote from being saved.
     $this->_quoteService->method('submitOrder')->will($this->throwException($orderSubmitException));
     // Side-effect test: if the order was not created, do not save quote
     // changes caused by creating the order.
     $this->_quote->expects($this->never())->method('save');
     // Expect that the exception thrown by submitOrder are not caught.
     $this->setExpectedException('Exception', $exceptionMessage);
     $this->_multishippingCheckout->createOrders();
 }