/** * When an order fails to be submitted - e.g. transaction fails, quote * should be kept active, */ public function testSubmitOrderFailure() { // Stub out dependencies to get through the basic flow of submitOrder. $this->_stubForBasicOrderSubmitCompletion(); // Create the exception expected to be thrown while saving the // order submit transaction. $failureMessage = 'Failure exception from unit test.'; $failureException = new Exception($failureMessage); // Side-effect tests: // Ensure the transaction is saved. In the failure case, saving the // transaction should throw an exception to prevent the order from being // created. $this->_transaction->expects($this->once())->method('save')->will($this->throwException($failureException)); // Ensure proper events are dispatched while failing to submit an order. // Defer to the checkout dispatcher for ensuring the proper Magento // events happen, but still need to ensure the correct ones would be // triggered when failing to create 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('dispatchOrderSubmitFailure')->with($this->identicalTo($this->_quote), $this->identicalTo($this->_order))->will($this->returnSelf()); $this->_checkoutDispatcher->expects($this->never())->method('dispatchAfterOrderSubmit'); $this->_checkoutDispatcher->expects($this->never())->method('dispatchOrderSubmitSuccess'); // Ensure that failed order cleanup is triggered. More thorough testing // of this cleanup handled in a more specific test. This should just // make sure that at least some of it has been triggered. $this->_order->expects($this->once())->method('setId')->with($this->isNull())->will($this->returnSelf()); $this->setExpectedException(get_class($failureException), $failureMessage); $this->_serviceQuote->submitOrder(); }
/** * Submit the quote. Quote submit process will create the order based on * quote data. * * @return Mage_Sales_Model_Order */ public function submitOrder() { $this->_prepareQuote(); $order = $this->_createOrder(); // Create the transaction to place the order. $transaction = $this->_multishippingFactory->createOrderSaveTransaction($this->_quote, $order, $this->_quote->getCustomerId() ? $this->_quote->getCustomer() : null); // Dispatch events for when the order has been instantiated but before // any attempt has been made to actually save the order. Allows for // additional modifications to the order to be made before attempting // to save and place the order. $this->_checkoutDispatcher->dispatchBeforeOrderSubmit($this->_quote, $order); try { // Saving the transaction should trigger the order to be created - // save quote and order objects (incl. addresses & items), place // payments, and dispatch one last event that may be able to block // the order from being created. $transaction->save(); $this->_inactivateQuote(); $this->_checkoutDispatcher->dispatchOrderSubmitSuccess($this->_quote, $order); } catch (Exception $e) { $this->_cleanupFailedOrder($order); // Dispatch events signaling that the order failed to be created. // Allows related modules to cleanup or roll back actions taken // while attempting to creating the order. $this->_checkoutDispatcher->dispatchOrderSubmitFailure($this->_quote, $order); // Re-throw the exception to be handled in a user-friendly way elsewhere. throw $e; } // Signal that the order create was a success and the order has been // placed and saved. $this->_checkoutDispatcher->dispatchAfterOrderSubmit($this->_quote, $order); $this->_order = $order; return $order; }