Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ReportNewRelicCronTest.php
Go to the documentation of this file.
1 <?php
7 
9 
13 class ReportNewRelicCronTest extends \PHPUnit\Framework\TestCase
14 {
18  protected $model;
19 
23  protected $config;
24 
28  protected $collect;
29 
33  protected $counter;
34 
38  protected $cronEventFactory;
39 
43  protected $cronEventModel;
44 
49 
53  protected $deploymentsModel;
54 
58  private $logger;
59 
65  protected function setUp()
66  {
67  $this->config = $this->getMockBuilder(\Magento\NewRelicReporting\Model\Config::class)
68  ->disableOriginalConstructor()
69  ->setMethods(['isNewRelicEnabled'])
70  ->getMock();
71  $this->collect = $this->getMockBuilder(\Magento\NewRelicReporting\Model\Module\Collect::class)
72  ->disableOriginalConstructor()
73  ->setMethods(['getModuleData'])
74  ->getMock();
75  $this->counter = $this->getMockBuilder(\Magento\NewRelicReporting\Model\Counter::class)
76  ->disableOriginalConstructor()
77  ->setMethods([
78  'getAllProductsCount',
79  'getConfigurableCount',
80  'getActiveCatalogSize',
81  'getCategoryCount',
82  'getWebsiteCount',
83  'getStoreViewsCount',
84  'getCustomerCount',
85  ])
86  ->getMock();
87  $this->cronEventFactory = $this->getMockBuilder(\Magento\NewRelicReporting\Model\CronEventFactory::class)
88  ->disableOriginalConstructor()
89  ->setMethods(['create'])
90  ->getMock();
91  $this->cronEventModel = $this->getMockBuilder(\Magento\NewRelicReporting\Model\CronEvent::class)
92  ->disableOriginalConstructor()
93  ->setMethods(['addData', 'sendRequest'])
94  ->getMock();
95  $this->deploymentsFactory = $this->getMockBuilder(
96  \Magento\NewRelicReporting\Model\Apm\DeploymentsFactory::class
97  )->disableOriginalConstructor()
98  ->setMethods(['create'])
99  ->getMock();
100  $this->deploymentsModel = $this->getMockBuilder(\Magento\NewRelicReporting\Model\Apm\Deployments::class)
101  ->disableOriginalConstructor()
102  ->setMethods(['setDeployment'])
103  ->getMock();
104 
105  $this->cronEventFactory->expects($this->any())
106  ->method('create')
107  ->willReturn($this->cronEventModel);
108  $this->deploymentsFactory->expects($this->any())
109  ->method('create')
110  ->willReturn($this->deploymentsModel);
111  $this->logger = $this->getMockForAbstractClass(\Psr\Log\LoggerInterface::class);
112 
113  $this->model = new ReportNewRelicCron(
114  $this->config,
115  $this->collect,
116  $this->counter,
117  $this->cronEventFactory,
118  $this->deploymentsFactory,
119  $this->logger
120  );
121  }
122 
129  {
130  $this->config->expects($this->once())
131  ->method('isNewRelicEnabled')
132  ->willReturn(false);
133 
134  $this->assertSame(
135  $this->model,
136  $this->model->report()
137  );
138  }
139 
145  public function testReportNewRelicCron()
146  {
147 
148  $this->config->expects($this->once())
149  ->method('isNewRelicEnabled')
150  ->willReturn(true);
151  $this->counter->expects($this->once())
152  ->method('getAllProductsCount');
153  $this->counter->expects($this->once())
154  ->method('getConfigurableCount');
155  $this->counter->expects($this->once())
156  ->method('getActiveCatalogSize');
157  $this->counter->expects($this->once())
158  ->method('getCategoryCount');
159  $this->counter->expects($this->once())
160  ->method('getWebsiteCount');
161  $this->counter->expects($this->once())
162  ->method('getStoreViewsCount');
163  $this->counter->expects($this->once())
164  ->method('getCustomerCount');
165  $this->cronEventModel->expects($this->once())
166  ->method('addData')
167  ->willReturnSelf();
168  $this->cronEventModel->expects($this->once())
169  ->method('sendRequest');
170 
171  $this->deploymentsModel->expects($this->any())
172  ->method('setDeployment');
173 
174  $this->assertSame(
175  $this->model,
176  $this->model->report()
177  );
178  }
179 
186  {
187 
188  $this->config->expects($this->once())
189  ->method('isNewRelicEnabled')
190  ->willReturn(true);
191  $this->counter->expects($this->once())
192  ->method('getAllProductsCount');
193  $this->counter->expects($this->once())
194  ->method('getConfigurableCount');
195  $this->counter->expects($this->once())
196  ->method('getActiveCatalogSize');
197  $this->counter->expects($this->once())
198  ->method('getCategoryCount');
199  $this->counter->expects($this->once())
200  ->method('getWebsiteCount');
201  $this->counter->expects($this->once())
202  ->method('getStoreViewsCount');
203  $this->counter->expects($this->once())
204  ->method('getCustomerCount');
205  $this->cronEventModel->expects($this->once())
206  ->method('addData')
207  ->willReturnSelf();
208  $this->cronEventModel->expects($this->once())
209  ->method('sendRequest');
210 
211  $this->cronEventModel->expects($this->once())->method('sendRequest')->willThrowException(new \Exception());
212  $this->logger->expects($this->never())->method('critical');
213 
214  $this->deploymentsModel->expects($this->any())
215  ->method('setDeployment');
216 
217  $this->model->report();
218  }
219 }