Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ReportCountsTest.php
Go to the documentation of this file.
1 <?php
7 
12 
16 class ReportCountsTest extends \PHPUnit\Framework\TestCase
17 {
21  protected $model;
22 
26  protected $configMock;
27 
32 
37 
42 
46  protected $countsFactoryMock;
47 
51  protected $countsModelMock;
52 
58 
63 
69  protected function setUp()
70  {
71  $this->configMock = $this->getMockBuilder(\Magento\NewRelicReporting\Model\Config::class)
72  ->disableOriginalConstructor()
73  ->setMethods(['isNewRelicEnabled'])
74  ->getMock();
75  $this->productManagementMock = $this->getMockBuilder(\Magento\Catalog\Api\ProductManagementInterface::class)
76  ->setMethods(['getCount'])
77  ->disableOriginalConstructor()
78  ->getMock();
79  $this->configurableManagementMock = $this
80  ->getMockBuilder(\Magento\ConfigurableProduct\Api\ConfigurableProductManagementInterface::class)
81  ->setMethods(['getCount'])
82  ->disableOriginalConstructor()
83  ->getMockForAbstractClass();
84  $this->categoryManagementMock = $this->getMockBuilder(\Magento\Catalog\Api\CategoryManagementInterface::class)
85  ->setMethods(['getCount'])
86  ->disableOriginalConstructor()
87  ->getMockForAbstractClass();
88  $this->countsFactoryMock = $this->getMockBuilder(\Magento\NewRelicReporting\Model\CountsFactory::class)
89  ->disableOriginalConstructor()
90  ->setMethods(['create'])
91  ->getMock();
92  $this->countsModelMock = $this->getMockBuilder(\Magento\NewRelicReporting\Model\Counts::class)
93  ->setMethods(['getCount', 'load', 'setEntityId', 'setType', 'setCount', 'setUpdatedAt', 'save'])
94  ->disableOriginalConstructor()
95  ->getMock();
96  $this->countsCollectionFactoryMock = $this
97  ->getMockBuilder(\Magento\NewRelicReporting\Model\ResourceModel\Counts\CollectionFactory::class)
98  ->disableOriginalConstructor()
99  ->setMethods(['create'])
100  ->getMock();
101  $collectionClassName = \Magento\NewRelicReporting\Model\ResourceModel\Counts\Collection::class;
102  $this->countsCollectionMock = $this->getMockBuilder($collectionClassName)
103  ->disableOriginalConstructor()
104  ->setMethods(['addFieldToFilter', 'addOrder', 'setPageSize', 'getFirstItem'])
105  ->getMock();
106 
107  $this->countsFactoryMock->expects($this->any())
108  ->method('create')
109  ->willReturn($this->countsModelMock);
110  $this->countsModelMock->expects($this->any())
111  ->method('load')
112  ->willReturnSelf();
113  $this->countsCollectionFactoryMock->expects($this->any())
114  ->method('create')
115  ->willReturn($this->countsCollectionMock);
116  $this->countsCollectionMock->expects($this->any())
117  ->method('addFieldToFilter')
118  ->willReturnSelf();
119  $this->countsCollectionMock->expects($this->any())
120  ->method('addOrder')
121  ->willReturnSelf();
122  $this->countsCollectionMock->expects($this->any())
123  ->method('setPageSize')
124  ->willReturnSelf();
125  $this->countsCollectionMock->expects($this->any())
126  ->method('getFirstItem')
127  ->willReturn($this->countsModelMock);
128 
129  $this->model = new ReportCounts(
130  $this->configMock,
131  $this->productManagementMock,
132  $this->configurableManagementMock,
133  $this->categoryManagementMock,
134  $this->countsFactoryMock,
135  $this->countsCollectionFactoryMock
136  );
137  }
138 
145  {
146  $this->configMock->expects($this->once())
147  ->method('isNewRelicEnabled')
148  ->willReturn(false);
149 
150  $this->assertSame(
151  $this->model,
152  $this->model->report()
153  );
154  }
155 
161  public function testReportCountsTest()
162  {
163  $this->configMock->expects($this->once())
164  ->method('isNewRelicEnabled')
165  ->willReturn(true);
166  $this->productManagementMock->expects($this->exactly(2))
167  ->method('getCount')
168  ->willReturn(2);
169  $this->configurableManagementMock->expects($this->once())
170  ->method('getCount')
171  ->willReturn(2);
172  $this->categoryManagementMock->expects($this->once())
173  ->method('getCount')
174  ->willReturn(2);
175 
176  $this->countsModelMock->expects($this->any())
177  ->method('getCount')
178  ->willReturn(1);
179  $this->countsModelMock->expects($this->any())
180  ->method('setEntityId')
181  ->willReturnSelf();
182  $this->countsModelMock->expects($this->any())
183  ->method('setType')
184  ->willReturnSelf();
185  $this->countsModelMock->expects($this->any())
186  ->method('setCount')
187  ->willReturnSelf();
188  $this->countsModelMock->expects($this->any())
189  ->method('setUpdatedAt')
190  ->willReturnSelf();
191  $this->countsModelMock->expects($this->any())
192  ->method('save')
193  ->willReturnSelf();
194 
195  $this->assertSame(
196  $this->model,
197  $this->model->report()
198  );
199  }
200 }