123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
- <?php
- /**
- * Unit Test for \Magento\Framework\Profiler
- *
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\Test\Unit;
- class ProfilerTest extends \PHPUnit\Framework\TestCase
- {
- protected function tearDown()
- {
- \Magento\Framework\Profiler::reset();
- }
- public function testEnable()
- {
- \Magento\Framework\Profiler::enable();
- $this->assertTrue(\Magento\Framework\Profiler::isEnabled());
- }
- public function testDisable()
- {
- \Magento\Framework\Profiler::disable();
- $this->assertFalse(\Magento\Framework\Profiler::isEnabled());
- }
- public function testSetDefaultTags()
- {
- $expected = ['some_key' => 'some_value'];
- \Magento\Framework\Profiler::setDefaultTags($expected);
- $this->assertAttributeEquals($expected, '_defaultTags', \Magento\Framework\Profiler::class);
- }
- public function testAddTagFilter()
- {
- \Magento\Framework\Profiler::addTagFilter('tag1', 'value_1.1');
- \Magento\Framework\Profiler::addTagFilter('tag2', 'value_2.1');
- \Magento\Framework\Profiler::addTagFilter('tag1', 'value_1.2');
- $expected = ['tag1' => ['value_1.1', 'value_1.2'], 'tag2' => ['value_2.1']];
- $this->assertAttributeEquals($expected, '_tagFilters', \Magento\Framework\Profiler::class);
- $this->assertAttributeEquals(true, '_hasTagFilters', \Magento\Framework\Profiler::class);
- }
- public function testAdd()
- {
- $mock = $this->_getDriverMock();
- \Magento\Framework\Profiler::add($mock);
- $this->assertTrue(\Magento\Framework\Profiler::isEnabled());
- $expected = [$mock];
- $this->assertAttributeEquals($expected, '_drivers', \Magento\Framework\Profiler::class);
- }
- /**
- * @return \PHPUnit_Framework_MockObject_MockObject
- */
- protected function _getDriverMock()
- {
- return $this->getMockBuilder(
- \Magento\Framework\Profiler\DriverInterface::class
- )->setMethods(
- ['start', 'stop', 'clear']
- )->getMockForAbstractClass();
- }
- /**
- * @expectedException \InvalidArgumentException
- * @expectedExceptionMessage Timer name must not contain a nesting separator.
- */
- public function testStartException()
- {
- \Magento\Framework\Profiler::enable();
- \Magento\Framework\Profiler::start('timer ' . \Magento\Framework\Profiler::NESTING_SEPARATOR . ' name');
- }
- public function testDisabledProfiler()
- {
- $driver = $this->_getDriverMock();
- $driver->expects($this->never())->method('start');
- $driver->expects($this->never())->method('stop');
- \Magento\Framework\Profiler::add($driver);
- \Magento\Framework\Profiler::disable();
- \Magento\Framework\Profiler::start('test');
- \Magento\Framework\Profiler::stop('test');
- }
- public function testStartStopSimple()
- {
- $driver = $this->_getDriverMock();
- $driver->expects($this->once())->method('start')->with('root_level_timer', null);
- $driver->expects($this->once())->method('stop')->with('root_level_timer');
- \Magento\Framework\Profiler::add($driver);
- \Magento\Framework\Profiler::start('root_level_timer');
- \Magento\Framework\Profiler::stop('root_level_timer');
- }
- public function testStartNested()
- {
- $driver = $this->_getDriverMock();
- $driver->expects($this->at(0))->method('start')->with('root_level_timer', null);
- $driver->expects($this->at(1))->method('start')->with('root_level_timer->some_other_timer', null);
- $driver->expects($this->at(2))->method('stop')->with('root_level_timer->some_other_timer');
- $driver->expects($this->at(3))->method('stop')->with('root_level_timer');
- \Magento\Framework\Profiler::add($driver);
- \Magento\Framework\Profiler::start('root_level_timer');
- \Magento\Framework\Profiler::start('some_other_timer');
- \Magento\Framework\Profiler::stop('some_other_timer');
- \Magento\Framework\Profiler::stop('root_level_timer');
- }
- /**
- * @expectedException \InvalidArgumentException
- * @expectedExceptionMessage Timer "unknown" has not been started.
- */
- public function testStopExceptionUnknown()
- {
- \Magento\Framework\Profiler::enable();
- \Magento\Framework\Profiler::start('timer');
- \Magento\Framework\Profiler::stop('unknown');
- }
- public function testStopOrder()
- {
- $driver = $this->_getDriverMock();
- $driver->expects($this->at(0))->method('start')->with('timer1', null);
- $driver->expects($this->at(1))->method('start')->with('timer1->timer2', null);
- $driver->expects($this->at(2))->method('start')->with('timer1->timer2->timer1', null);
- $driver->expects($this->at(3))->method('start')->with('timer1->timer2->timer1->timer3', null);
- $driver->expects($this->at(4))->method('stop')->with('timer1->timer2->timer1->timer3');
- $driver->expects($this->at(5))->method('stop')->with('timer1->timer2->timer1');
- $driver->expects($this->exactly(4))->method('start');
- $driver->expects($this->exactly(2))->method('stop');
- \Magento\Framework\Profiler::add($driver);
- \Magento\Framework\Profiler::start('timer1');
- \Magento\Framework\Profiler::start('timer2');
- \Magento\Framework\Profiler::start('timer1');
- \Magento\Framework\Profiler::start('timer3');
- \Magento\Framework\Profiler::stop('timer1');
- }
- public function testStopSameName()
- {
- $driver = $this->_getDriverMock();
- $driver->expects($this->at(0))->method('start')->with('timer1', null);
- $driver->expects($this->at(1))->method('start')->with('timer1->timer1', null);
- $driver->expects($this->at(2))->method('stop')->with('timer1->timer1');
- $driver->expects($this->at(3))->method('stop')->with('timer1');
- \Magento\Framework\Profiler::add($driver);
- \Magento\Framework\Profiler::start('timer1');
- \Magento\Framework\Profiler::start('timer1');
- \Magento\Framework\Profiler::stop('timer1');
- \Magento\Framework\Profiler::stop('timer1');
- }
- public function testStopLatest()
- {
- $driver = $this->_getDriverMock();
- $driver->expects($this->at(0))->method('start')->with('root_level_timer', null);
- $driver->expects($this->at(1))->method('stop')->with('root_level_timer');
- \Magento\Framework\Profiler::add($driver);
- \Magento\Framework\Profiler::start('root_level_timer');
- \Magento\Framework\Profiler::stop();
- }
- public function testTags()
- {
- $driver = $this->_getDriverMock();
- $driver->expects($this->at(0))->method('start')->with('root_level_timer', ['default_tag' => 'default']);
- $driver->expects(
- $this->at(1)
- )->method(
- 'start'
- )->with(
- 'root_level_timer->some_other_timer',
- ['default_tag' => 'default', 'type' => 'test']
- );
- \Magento\Framework\Profiler::add($driver);
- \Magento\Framework\Profiler::setDefaultTags(['default_tag' => 'default']);
- \Magento\Framework\Profiler::start('root_level_timer');
- \Magento\Framework\Profiler::start('some_other_timer', ['type' => 'test']);
- }
- public function testClearTimer()
- {
- $driver = $this->_getDriverMock();
- $driver->expects($this->at(0))->method('clear')->with('timer');
- \Magento\Framework\Profiler::add($driver);
- \Magento\Framework\Profiler::clear('timer');
- }
- /**
- * @expectedException \InvalidArgumentException
- * @expectedExceptionMessage Timer name must not contain a nesting separator.
- */
- public function testClearException()
- {
- \Magento\Framework\Profiler::enable();
- \Magento\Framework\Profiler::clear('timer ' . \Magento\Framework\Profiler::NESTING_SEPARATOR . ' name');
- }
- public function testResetProfiler()
- {
- $driver = $this->_getDriverMock();
- $driver->expects($this->once())->method('clear')->with(null);
- \Magento\Framework\Profiler::add($driver);
- \Magento\Framework\Profiler::reset();
- $this->assertAttributeEquals([], '_currentPath', \Magento\Framework\Profiler::class);
- $this->assertAttributeEquals([], '_tagFilters', \Magento\Framework\Profiler::class);
- $this->assertAttributeEquals([], '_defaultTags', \Magento\Framework\Profiler::class);
- $this->assertAttributeEquals([], '_drivers', \Magento\Framework\Profiler::class);
- $this->assertAttributeEquals(false, '_hasTagFilters', \Magento\Framework\Profiler::class);
- $this->assertAttributeEquals(0, '_pathCount', \Magento\Framework\Profiler::class);
- $this->assertAttributeEquals([], '_pathIndex', \Magento\Framework\Profiler::class);
- }
- /**
- * @param string $timerName
- * @param array $tags
- * @dataProvider skippedFilterDataProvider
- */
- public function testTagFilterSkip($timerName, array $tags = null)
- {
- $driver = $this->_getDriverMock();
- $driver->expects($this->never())->method('start');
- \Magento\Framework\Profiler::add($driver);
- \Magento\Framework\Profiler::addTagFilter('type', 'test');
- \Magento\Framework\Profiler::start($timerName, $tags);
- }
- /**
- * @return array
- */
- public function skippedFilterDataProvider()
- {
- return [
- 'no tags' => ['timer', null],
- 'no expected tags' => ['timer', ['tag' => 'value']],
- 'no expected tag value' => ['timer', ['type' => 'db']]
- ];
- }
- /**
- * @param string $timerName
- * @param array $tags
- * @dataProvider passedFilterDataProvider
- */
- public function testTagFilterPass($timerName, array $tags = null)
- {
- $driver = $this->_getDriverMock();
- $driver->expects($this->once())->method('start')->with($timerName, $tags);
- \Magento\Framework\Profiler::add($driver);
- \Magento\Framework\Profiler::addTagFilter('type', 'test');
- \Magento\Framework\Profiler::start($timerName, $tags);
- }
- /**
- * @return array
- */
- public function passedFilterDataProvider()
- {
- return [
- 'one expected tag' => ['timer', ['type' => 'test']],
- 'more than one tag with expected' => ['timer', ['tag' => 'value', 'type' => 'test']]
- ];
- }
- public function testApplyConfig()
- {
- $mockDriver = $this->createMock(\Magento\Framework\Profiler\DriverInterface::class);
- $driverConfig = ['type' => 'foo'];
- $mockDriverFactory = $this->getMockBuilder(
- \Magento\Framework\Profiler\Driver\Factory::class
- )->disableOriginalConstructor()->getMock();
- $config = [
- 'drivers' => [$driverConfig],
- 'driverFactory' => $mockDriverFactory,
- 'tagFilters' => ['tagName' => 'tagValue'],
- ];
- $mockDriverFactory->expects(
- $this->once()
- )->method(
- 'create'
- )->with(
- $driverConfig
- )->will(
- $this->returnValue($mockDriver)
- );
- \Magento\Framework\Profiler::applyConfig($config, '');
- $this->assertAttributeEquals([$mockDriver], '_drivers', \Magento\Framework\Profiler::class);
- $this->assertAttributeEquals(
- ['tagName' => ['tagValue']],
- '_tagFilters',
- \Magento\Framework\Profiler::class
- );
- $this->assertAttributeEquals(true, '_enabled', \Magento\Framework\Profiler::class);
- }
- /**
- * @dataProvider parseConfigDataProvider
- * @param array $data
- * @param boolean $isAjax
- * @param array $expected
- */
- public function testParseConfig($data, $isAjax, $expected)
- {
- $method = new \ReflectionMethod(\Magento\Framework\Profiler::class, '_parseConfig');
- $method->setAccessible(true);
- $this->assertEquals($expected, $method->invoke(null, $data, '', $isAjax));
- }
- /**
- * @return array
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- */
- public function parseConfigDataProvider()
- {
- $driverFactory = new \Magento\Framework\Profiler\Driver\Factory();
- $otherDriverFactory = $this->createMock(\Magento\Framework\Profiler\Driver\Factory::class);
- return [
- 'Empty configuration' => [
- [],
- false,
- [
- 'driverConfigs' => [],
- 'driverFactory' => $driverFactory,
- 'tagFilters' => [],
- 'baseDir' => null
- ],
- ],
- 'Full configuration' => [
- [
- 'drivers' => [['type' => 'foo']],
- 'driverFactory' => $otherDriverFactory,
- 'tagFilters' => ['key' => 'value'],
- 'baseDir' => '/custom/base/dir',
- ],
- false,
- [
- 'driverConfigs' => [['type' => 'foo', 'baseDir' => '/custom/base/dir']],
- 'driverFactory' => $otherDriverFactory,
- 'tagFilters' => ['key' => 'value'],
- 'baseDir' => '/custom/base/dir'
- ],
- ],
- 'Driver configuration with type in index' => [
- ['drivers' => ['foo' => 1]],
- false,
- [
- 'driverConfigs' => [['type' => 'foo']],
- 'driverFactory' => $driverFactory,
- 'tagFilters' => [],
- 'baseDir' => null
- ],
- ],
- 'Driver configuration with type in value' => [
- ['drivers' => ['foo']],
- false,
- [
- 'driverConfigs' => [['type' => 'foo']],
- 'driverFactory' => $driverFactory,
- 'tagFilters' => [],
- 'baseDir' => null
- ],
- ],
- 'Driver ignored configuration' => [
- ['drivers' => ['foo' => 0]],
- false,
- [
- 'driverConfigs' => [],
- 'driverFactory' => $driverFactory,
- 'tagFilters' => [],
- 'baseDir' => null
- ],
- ],
- 'Non ajax call' => [
- 1,
- false,
- [
- 'driverConfigs' => [['output' => 'html']],
- 'driverFactory' => $driverFactory,
- 'tagFilters' => [],
- 'baseDir' => ''
- ],
- ]
- ];
- }
- }
|