/** * 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(); } }
/** * Test case when module is enabled * * @return void */ public function testReportCountsTest() { $this->configMock->expects($this->once())->method('isNewRelicEnabled')->willReturn(true); $this->productManagementMock->expects($this->exactly(2))->method('getCount')->willReturn(2); $this->configurableManagementMock->expects($this->once())->method('getCount')->willReturn(2); $this->categoryManagementMock->expects($this->once())->method('getCount')->willReturn(2); $this->countsModelMock->expects($this->any())->method('getCount')->willReturn(1); $this->countsModelMock->expects($this->any())->method('setEntityId')->willReturnSelf(); $this->countsModelMock->expects($this->any())->method('setType')->willReturnSelf(); $this->countsModelMock->expects($this->any())->method('setCount')->willReturnSelf(); $this->countsModelMock->expects($this->any())->method('setUpdatedAt')->willReturnSelf(); $this->countsModelMock->expects($this->any())->method('save')->willReturnSelf(); $this->assertSame($this->model, $this->model->report()); }