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]);
 }
 /**
  * Calculate the total quantity requested of a given item. All items in the
  * quote with the same SKU as the given item will be counted toward the
  * total quantity.
  *
  * @param Mage_Sales_Model_Quote_Item_Abstract
  * @return int
  */
 protected function _calculateTotalQuantityRequested(Mage_Sales_Model_Quote_Item_Abstract $item)
 {
     $inventoryItems = $this->_inventoryItemSelection->selectFrom($item->getQuote()->getAllItems());
     return $this->_quantityHelper->calculateTotalQuantityRequested($item, $inventoryItems);
 }
 public function fillOutShippingItem(IOrderItem $itemPayload, Mage_Sales_Model_Quote_Item_Abstract $item, Mage_Customer_Model_Address_Abstract $address)
 {
     $shippingMethod = $this->shippingHelper->getUsableMethod($address);
     $itemPayload->setItemId($item->getSku())->setLineId($item->getAddressItemId() ?: $item->getId())->setQuantity($this->quantityHelper->getRequestedItemQuantity($item))->setGiftWrapRequested($this->isItemGiftWrapped($item))->setAddressLines($address->getStreet(static::ADDRESS_ALL_STREET_LINES))->setAddressCity($address->getCity())->setAddressCountryCode($address->getCountryId())->setShippingMethod($this->shippingHelper->getMethodSdkId($shippingMethod))->setAddressMainDivision($address->getRegionCode())->setAddressPostalCode($address->getPostcode())->setShippingMethodDisplayText($this->shippingHelper->getMethodTitle($shippingMethod));
 }
 /**
  * Create a quantity results model with the provided quantity models,
  * sku quantity data from the provided items and an expiration time based
  * on the current time offset by the configured quantity cache lifetime.
  *
  * @param EbayEnterprise_Inventory_Model_Quantity[]
  * @param Mage_Sales_Model_Quote_Item_Abstract[]
  * @return EbayEnterprise_Inventory_Model_Quantity_Result
  */
 public function createQuantityResults(array $quantityResults, array $requestedItems)
 {
     return Mage::getModel('ebayenterprise_inventory/quantity_results', ['quantities' => $quantityResults, 'expiration_time' => $this->_getQuantityExpirationTime(new DateTime()), 'sku_quantity_data' => $this->_quantityHelper->calculateTotalQuantitiesBySku($requestedItems)]);
 }
 /**
  * Get existing quantity results from the session. Will only return results
  * if they are valid and apply to the current state of the provided items.
  *
  * @param Mage_Sales_Model_Quote_Item[]
  * @return EbayEnterprise_Inventory_Model_Quantity_Restults|null
  */
 protected function _getSessionResults(array $items)
 {
     $results = $this->_getInventorySession()->getQuantityResults();
     $skuQuantityData = $this->_quantityHelper->calculateTotalQuantitiesBySku($items);
     return $results && $results->checkResultsApplyToItems($skuQuantityData) ? $this->_getInventorySession()->getQuantityResults() : null;
 }