示例#1
0
    /**
     * @param bool $formKeyIsValid
     * @param bool $isPost
     * @dataProvider executeDataProvider
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function testExecute($formKeyIsValid, $isPost)
    {
        $this->formKeyValidator->expects($this->any())
            ->method('validate')
            ->willReturn($formKeyIsValid);

        $this->request->expects($this->any())
            ->method('isPost')
            ->willReturn($isPost);

        if (!$formKeyIsValid || !$isPost) {
            $this->messageManager->expects($this->once())
                ->method('addError');

            $this->resultRedirect->expects($this->once())
                ->method('setPath')
                ->with('sales/order/index');

            $this->shipmentLoader->expects($this->never())
                ->method('load');

            $this->assertEquals($this->resultRedirect, $this->saveAction->executeInternal());
        } else {
            $shipmentId = 1000012;
            $orderId = 10003;
            $tracking = [];
            $shipmentData = ['items' => [], 'send_email' => ''];
            $shipment = $this->getMock(
                'Magento\Sales\Model\Order\Shipment',
                ['load', 'save', 'register', 'getOrder', 'getOrderId', '__wakeup'],
                [],
                '',
                false
            );
            $order = $this->getMock(
                'Magento\Sales\Model\Order',
                ['setCustomerNoteNotify', '__wakeup'],
                [],
                '',
                false
            );

            $this->request->expects($this->any())
                ->method('getParam')
                ->will(
                    $this->returnValueMap(
                        [
                            ['order_id', null, $orderId],
                            ['shipment_id', null, $shipmentId],
                            ['shipment', null, $shipmentData],
                            ['tracking', null, $tracking],
                        ]
                    )
                );

            $this->shipmentLoader->expects($this->any())
                ->method('setShipmentId')
                ->with($shipmentId);
            $this->shipmentLoader->expects($this->any())
                ->method('setOrderId')
                ->with($orderId);
            $this->shipmentLoader->expects($this->any())
                ->method('setShipment')
                ->with($shipmentData);
            $this->shipmentLoader->expects($this->any())
                ->method('setTracking')
                ->with($tracking);
            $this->shipmentLoader->expects($this->once())
                ->method('load')
                ->will($this->returnValue($shipment));
            $shipment->expects($this->once())
                ->method('register')
                ->will($this->returnSelf());
            $shipment->expects($this->any())
                ->method('getOrder')
                ->will($this->returnValue($order));
            $order->expects($this->once())
                ->method('setCustomerNoteNotify')
                ->with(false);
            $this->labelGenerator->expects($this->any())
                ->method('create')
                ->with($shipment, $this->request)
                ->will($this->returnValue(true));
            $saveTransaction = $this->getMockBuilder('Magento\Framework\DB\Transaction')
                ->disableOriginalConstructor()
                ->setMethods([])
                ->getMock();
            $saveTransaction->expects($this->at(0))
                ->method('addObject')
                ->with($shipment)
                ->will($this->returnSelf());
            $saveTransaction->expects($this->at(1))
                ->method('addObject')
                ->with($order)
                ->will($this->returnSelf());
            $saveTransaction->expects($this->at(2))
                ->method('save');

            $this->session->expects($this->once())
                ->method('getCommentText')
                ->with(true);

            $this->objectManager->expects($this->once())
                ->method('create')
                ->with('Magento\Framework\DB\Transaction')
                ->will($this->returnValue($saveTransaction));
            $this->objectManager->expects($this->once())
                ->method('get')
                ->with('Magento\Backend\Model\Session')
                ->will($this->returnValue($this->session));
            $path = 'sales/order/view';
            $arguments = ['order_id' => $orderId];
            $shipment->expects($this->once())
                ->method('getOrderId')
                ->will($this->returnValue($orderId));
            $this->prepareRedirect($path, $arguments);

            $this->saveAction->executeInternal();
            $this->assertEquals($this->response, $this->saveAction->getResponse());
        }
    }