/** * Setup * * @return void */ public function setUp() { $this->config = $this->getMockBuilder('Magento\\NewRelicReporting\\Model\\Config')->disableOriginalConstructor()->setMethods(['isNewRelicEnabled'])->getMock(); $this->ordersFactory = $this->getMockBuilder('Magento\\NewRelicReporting\\Model\\OrdersFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock(); $this->ordersModel = $this->getMockBuilder('Magento\\NewRelicReporting\\Model\\Orders')->disableOriginalConstructor()->getMock(); $this->ordersFactory->expects($this->any())->method('create')->willReturn($this->ordersModel); $this->model = new ReportOrderPlaced($this->config, $this->ordersFactory); }
/** * Reports orders placed to the database reporting_orders table * * @param Observer $observer * @return void */ public function execute(Observer $observer) { if ($this->config->isNewRelicEnabled()) { /** @var \Magento\Sales\Model\Order $order */ $order = $observer->getEvent()->getOrder(); $itemCount = $order->getTotalItemCount(); if (!is_numeric($itemCount) && empty($itemCount)) { $itemCount = $order->getTotalQtyOrdered(); } $modelData = ['customer_id' => $order->getCustomerId(), 'total' => $order->getGrandTotal(), 'total_base' => $order->getBaseGrandTotal(), 'item_count' => $itemCount]; /** @var \Magento\NewRelicReporting\Model\Orders $orderModel */ $orderModel = $this->ordersFactory->create(); $orderModel->setData($modelData); $orderModel->save(); } }