/** * Updates the count for a specific model in the database * * @param int $count * @param \Magento\NewRelicReporting\Model\Counts $model * @param string $type * @return void */ protected function updateCount($count, \Magento\NewRelicReporting\Model\Counts $model, $type) { /** @var \Magento\NewRelicReporting\Model\ResourceModel\Counts\Collection $collection */ $collection = $this->countsCollectionFactory->create()->addFieldToFilter('type', ['eq' => $type])->addOrder('updated_at', 'DESC')->setPageSize(1); $latestUpdate = $collection->getFirstItem(); if (!$latestUpdate || $count != $latestUpdate->getCount()) { $model->setEntityId(null); $model->setType($type); $model->setCount($count); $model->save(); } }
/** * Setup * * @return void */ public function setUp() { $this->configMock = $this->getMockBuilder('Magento\\NewRelicReporting\\Model\\Config')->disableOriginalConstructor()->setMethods(['isNewRelicEnabled'])->getMock(); $this->productManagementMock = $this->getMockBuilder('Magento\\Catalog\\Api\\ProductManagementInterface')->disableOriginalConstructor()->getMock(); $this->configurableManagementMock = $this->getMockBuilder('Magento\\ConfigurableProduct\\Api\\ConfigurableProductManagementInterface')->disableOriginalConstructor()->getMock(); $this->categoryManagementMock = $this->getMockBuilder('Magento\\Catalog\\Api\\CategoryManagementInterface')->disableOriginalConstructor()->getMock(); $this->countsFactoryMock = $this->getMockBuilder('Magento\\NewRelicReporting\\Model\\CountsFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock(); $this->countsModelMock = $this->getMockBuilder('Magento\\NewRelicReporting\\Model\\Counts')->disableOriginalConstructor()->getMock(); $this->countsCollectionFactoryMock = $this->getMockBuilder('Magento\\NewRelicReporting\\Model\\ResourceModel\\Counts\\CollectionFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock(); $collectionClassName = 'Magento\\NewRelicReporting\\Model\\ResourceModel\\Counts\\Collection'; $this->countsCollectionMock = $this->getMockBuilder($collectionClassName)->disableOriginalConstructor()->setMethods(['addFieldToFilter', 'addOrder', 'setPageSize', 'getFirstItem'])->getMock(); $this->countsFactoryMock->expects($this->any())->method('create')->willReturn($this->countsModelMock); $this->countsModelMock->expects($this->any())->method('load')->willReturnSelf(); $this->countsCollectionFactoryMock->expects($this->any())->method('create')->willReturn($this->countsCollectionMock); $this->countsCollectionMock->expects($this->any())->method('addFieldToFilter')->willReturnSelf(); $this->countsCollectionMock->expects($this->any())->method('addOrder')->willReturnSelf(); $this->countsCollectionMock->expects($this->any())->method('setPageSize')->willReturnSelf(); $this->countsCollectionMock->expects($this->any())->method('getFirstItem')->willReturn($this->countsModelMock); $this->model = new ReportCounts($this->configMock, $this->productManagementMock, $this->configurableManagementMock, $this->categoryManagementMock, $this->countsFactoryMock, $this->countsCollectionFactoryMock); }