Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProfilerTest.php
Go to the documentation of this file.
1 <?php
9 
10 class ProfilerTest extends \PHPUnit\Framework\TestCase
11 {
12  protected function tearDown()
13  {
15  }
16 
17  public function testEnable()
18  {
20  $this->assertTrue(\Magento\Framework\Profiler::isEnabled());
21  }
22 
23  public function testDisable()
24  {
26  $this->assertFalse(\Magento\Framework\Profiler::isEnabled());
27  }
28 
29  public function testSetDefaultTags()
30  {
31  $expected = ['some_key' => 'some_value'];
33  $this->assertAttributeEquals($expected, '_defaultTags', \Magento\Framework\Profiler::class);
34  }
35 
36  public function testAddTagFilter()
37  {
38  \Magento\Framework\Profiler::addTagFilter('tag1', 'value_1.1');
39  \Magento\Framework\Profiler::addTagFilter('tag2', 'value_2.1');
40  \Magento\Framework\Profiler::addTagFilter('tag1', 'value_1.2');
41 
42  $expected = ['tag1' => ['value_1.1', 'value_1.2'], 'tag2' => ['value_2.1']];
43  $this->assertAttributeEquals($expected, '_tagFilters', \Magento\Framework\Profiler::class);
44  $this->assertAttributeEquals(true, '_hasTagFilters', \Magento\Framework\Profiler::class);
45  }
46 
47  public function testAdd()
48  {
49  $mock = $this->_getDriverMock();
51 
52  $this->assertTrue(\Magento\Framework\Profiler::isEnabled());
53 
54  $expected = [$mock];
55  $this->assertAttributeEquals($expected, '_drivers', \Magento\Framework\Profiler::class);
56  }
57 
61  protected function _getDriverMock()
62  {
63  return $this->getMockBuilder(
64  \Magento\Framework\Profiler\DriverInterface::class
65  )->setMethods(
66  ['start', 'stop', 'clear']
67  )->getMockForAbstractClass();
68  }
69 
74  public function testStartException()
75  {
77  \Magento\Framework\Profiler::start('timer ' . \Magento\Framework\Profiler::NESTING_SEPARATOR . ' name');
78  }
79 
80  public function testDisabledProfiler()
81  {
82  $driver = $this->_getDriverMock();
83  $driver->expects($this->never())->method('start');
84  $driver->expects($this->never())->method('stop');
85 
88  \Magento\Framework\Profiler::start('test');
89  \Magento\Framework\Profiler::stop('test');
90  }
91 
92  public function testStartStopSimple()
93  {
94  $driver = $this->_getDriverMock();
95  $driver->expects($this->once())->method('start')->with('root_level_timer', null);
96  $driver->expects($this->once())->method('stop')->with('root_level_timer');
97 
99  \Magento\Framework\Profiler::start('root_level_timer');
100  \Magento\Framework\Profiler::stop('root_level_timer');
101  }
102 
103  public function testStartNested()
104  {
105  $driver = $this->_getDriverMock();
106  $driver->expects($this->at(0))->method('start')->with('root_level_timer', null);
107  $driver->expects($this->at(1))->method('start')->with('root_level_timer->some_other_timer', null);
108 
109  $driver->expects($this->at(2))->method('stop')->with('root_level_timer->some_other_timer');
110  $driver->expects($this->at(3))->method('stop')->with('root_level_timer');
111 
113  \Magento\Framework\Profiler::start('root_level_timer');
114  \Magento\Framework\Profiler::start('some_other_timer');
115  \Magento\Framework\Profiler::stop('some_other_timer');
116  \Magento\Framework\Profiler::stop('root_level_timer');
117  }
118 
123  public function testStopExceptionUnknown()
124  {
126  \Magento\Framework\Profiler::start('timer');
127  \Magento\Framework\Profiler::stop('unknown');
128  }
129 
130  public function testStopOrder()
131  {
132  $driver = $this->_getDriverMock();
133  $driver->expects($this->at(0))->method('start')->with('timer1', null);
134  $driver->expects($this->at(1))->method('start')->with('timer1->timer2', null);
135  $driver->expects($this->at(2))->method('start')->with('timer1->timer2->timer1', null);
136  $driver->expects($this->at(3))->method('start')->with('timer1->timer2->timer1->timer3', null);
137 
138  $driver->expects($this->at(4))->method('stop')->with('timer1->timer2->timer1->timer3');
139  $driver->expects($this->at(5))->method('stop')->with('timer1->timer2->timer1');
140 
141  $driver->expects($this->exactly(4))->method('start');
142  $driver->expects($this->exactly(2))->method('stop');
143 
145  \Magento\Framework\Profiler::start('timer1');
146  \Magento\Framework\Profiler::start('timer2');
147  \Magento\Framework\Profiler::start('timer1');
148  \Magento\Framework\Profiler::start('timer3');
149  \Magento\Framework\Profiler::stop('timer1');
150  }
151 
152  public function testStopSameName()
153  {
154  $driver = $this->_getDriverMock();
155  $driver->expects($this->at(0))->method('start')->with('timer1', null);
156  $driver->expects($this->at(1))->method('start')->with('timer1->timer1', null);
157 
158  $driver->expects($this->at(2))->method('stop')->with('timer1->timer1');
159  $driver->expects($this->at(3))->method('stop')->with('timer1');
160 
162  \Magento\Framework\Profiler::start('timer1');
163  \Magento\Framework\Profiler::start('timer1');
164  \Magento\Framework\Profiler::stop('timer1');
165  \Magento\Framework\Profiler::stop('timer1');
166  }
167 
168  public function testStopLatest()
169  {
170  $driver = $this->_getDriverMock();
171  $driver->expects($this->at(0))->method('start')->with('root_level_timer', null);
172 
173  $driver->expects($this->at(1))->method('stop')->with('root_level_timer');
174 
176  \Magento\Framework\Profiler::start('root_level_timer');
177  \Magento\Framework\Profiler::stop();
178  }
179 
180  public function testTags()
181  {
182  $driver = $this->_getDriverMock();
183  $driver->expects($this->at(0))->method('start')->with('root_level_timer', ['default_tag' => 'default']);
184  $driver->expects(
185  $this->at(1)
186  )->method(
187  'start'
188  )->with(
189  'root_level_timer->some_other_timer',
190  ['default_tag' => 'default', 'type' => 'test']
191  );
192 
194  \Magento\Framework\Profiler::setDefaultTags(['default_tag' => 'default']);
195  \Magento\Framework\Profiler::start('root_level_timer');
196  \Magento\Framework\Profiler::start('some_other_timer', ['type' => 'test']);
197  }
198 
199  public function testClearTimer()
200  {
201  $driver = $this->_getDriverMock();
202  $driver->expects($this->at(0))->method('clear')->with('timer');
203 
205  \Magento\Framework\Profiler::clear('timer');
206  }
207 
212  public function testClearException()
213  {
215  \Magento\Framework\Profiler::clear('timer ' . \Magento\Framework\Profiler::NESTING_SEPARATOR . ' name');
216  }
217 
218  public function testResetProfiler()
219  {
220  $driver = $this->_getDriverMock();
221  $driver->expects($this->once())->method('clear')->with(null);
222 
225 
226  $this->assertAttributeEquals([], '_currentPath', \Magento\Framework\Profiler::class);
227  $this->assertAttributeEquals([], '_tagFilters', \Magento\Framework\Profiler::class);
228  $this->assertAttributeEquals([], '_defaultTags', \Magento\Framework\Profiler::class);
229  $this->assertAttributeEquals([], '_drivers', \Magento\Framework\Profiler::class);
230  $this->assertAttributeEquals(false, '_hasTagFilters', \Magento\Framework\Profiler::class);
231  $this->assertAttributeEquals(0, '_pathCount', \Magento\Framework\Profiler::class);
232  $this->assertAttributeEquals([], '_pathIndex', \Magento\Framework\Profiler::class);
233  }
234 
240  public function testTagFilterSkip($timerName, array $tags = null)
241  {
242  $driver = $this->_getDriverMock();
243  $driver->expects($this->never())->method('start');
244 
247  \Magento\Framework\Profiler::start($timerName, $tags);
248  }
249 
253  public function skippedFilterDataProvider()
254  {
255  return [
256  'no tags' => ['timer', null],
257  'no expected tags' => ['timer', ['tag' => 'value']],
258  'no expected tag value' => ['timer', ['type' => 'db']]
259  ];
260  }
261 
267  public function testTagFilterPass($timerName, array $tags = null)
268  {
269  $driver = $this->_getDriverMock();
270  $driver->expects($this->once())->method('start')->with($timerName, $tags);
271 
274  \Magento\Framework\Profiler::start($timerName, $tags);
275  }
276 
280  public function passedFilterDataProvider()
281  {
282  return [
283  'one expected tag' => ['timer', ['type' => 'test']],
284  'more than one tag with expected' => ['timer', ['tag' => 'value', 'type' => 'test']]
285  ];
286  }
287 
288  public function testApplyConfig()
289  {
290  $mockDriver = $this->createMock(\Magento\Framework\Profiler\DriverInterface::class);
291  $driverConfig = ['type' => 'foo'];
292  $mockDriverFactory = $this->getMockBuilder(
293  \Magento\Framework\Profiler\Driver\Factory::class
294  )->disableOriginalConstructor()->getMock();
295  $config = [
296  'drivers' => [$driverConfig],
297  'driverFactory' => $mockDriverFactory,
298  'tagFilters' => ['tagName' => 'tagValue'],
299  ];
300 
301  $mockDriverFactory->expects(
302  $this->once()
303  )->method(
304  'create'
305  )->with(
306  $driverConfig
307  )->will(
308  $this->returnValue($mockDriver)
309  );
310 
312  $this->assertAttributeEquals([$mockDriver], '_drivers', \Magento\Framework\Profiler::class);
313  $this->assertAttributeEquals(
314  ['tagName' => ['tagValue']],
315  '_tagFilters',
316  \Magento\Framework\Profiler::class
317  );
318  $this->assertAttributeEquals(true, '_enabled', \Magento\Framework\Profiler::class);
319  }
320 
327  public function testParseConfig($data, $isAjax, $expected)
328  {
329  $method = new \ReflectionMethod(\Magento\Framework\Profiler::class, '_parseConfig');
330  $method->setAccessible(true);
331  $this->assertEquals($expected, $method->invoke(null, $data, '', $isAjax));
332  }
333 
338  public function parseConfigDataProvider()
339  {
340  $driverFactory = new \Magento\Framework\Profiler\Driver\Factory();
341  $otherDriverFactory = $this->createMock(\Magento\Framework\Profiler\Driver\Factory::class);
342  return [
343  'Empty configuration' => [
344  [],
345  false,
346  [
347  'driverConfigs' => [],
348  'driverFactory' => $driverFactory,
349  'tagFilters' => [],
350  'baseDir' => null
351  ],
352  ],
353  'Full configuration' => [
354  [
355  'drivers' => [['type' => 'foo']],
356  'driverFactory' => $otherDriverFactory,
357  'tagFilters' => ['key' => 'value'],
358  'baseDir' => '/custom/base/dir',
359  ],
360  false,
361  [
362  'driverConfigs' => [['type' => 'foo', 'baseDir' => '/custom/base/dir']],
363  'driverFactory' => $otherDriverFactory,
364  'tagFilters' => ['key' => 'value'],
365  'baseDir' => '/custom/base/dir'
366  ],
367  ],
368  'Driver configuration with type in index' => [
369  ['drivers' => ['foo' => 1]],
370  false,
371  [
372  'driverConfigs' => [['type' => 'foo']],
373  'driverFactory' => $driverFactory,
374  'tagFilters' => [],
375  'baseDir' => null
376  ],
377  ],
378  'Driver configuration with type in value' => [
379  ['drivers' => ['foo']],
380  false,
381  [
382  'driverConfigs' => [['type' => 'foo']],
383  'driverFactory' => $driverFactory,
384  'tagFilters' => [],
385  'baseDir' => null
386  ],
387  ],
388  'Driver ignored configuration' => [
389  ['drivers' => ['foo' => 0]],
390  false,
391  [
392  'driverConfigs' => [],
393  'driverFactory' => $driverFactory,
394  'tagFilters' => [],
395  'baseDir' => null
396  ],
397  ],
398  'Non ajax call' => [
399  1,
400  false,
401  [
402  'driverConfigs' => [['output' => 'html']],
403  'driverFactory' => $driverFactory,
404  'tagFilters' => [],
405  'baseDir' => ''
406  ],
407  ]
408  ];
409  }
410 }
$config
Definition: fraud_order.php:17
static add(DriverInterface $driver)
Definition: Profiler.php:137
testTagFilterSkip($timerName, array $tags=null)
static setDefaultTags(array $tags)
Definition: Profiler.php:86
$method
Definition: info.phtml:13
static applyConfig($config, $baseDir, $isAjax=false)
Definition: Profiler.php:332
static addTagFilter($tagName, $tagValue)
Definition: Profiler.php:98
testTagFilterPass($timerName, array $tags=null)
testParseConfig($data, $isAjax, $expected)