diff --git a/.htaccess b/.htaccess index 404488eb7ff0ecc6197ab8c100555c79eb1cf0ce..8df703b46897031679d81d4ead262d8b9d36dead 100644 --- a/.htaccess +++ b/.htaccess @@ -189,3 +189,4 @@ ## http://developer.yahoo.com/performance/rules.html#etags #FileETag none +SetEnv MAGE_PROFILER 2 diff --git a/lib/internal/Magento/Framework/ObjectManager/Profiler/FactoryDecorator.php b/lib/internal/Magento/Framework/ObjectManager/Profiler/FactoryDecorator.php index ad3864500bbc9502a1d8d6ecbaa296c76ac82d19..895377327281b908c2a8d0944a9415bbdb400275 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Profiler/FactoryDecorator.php +++ b/lib/internal/Magento/Framework/ObjectManager/Profiler/FactoryDecorator.php @@ -8,6 +8,11 @@ namespace Magento\Framework\ObjectManager\Profiler; class FactoryDecorator implements \Magento\Framework\ObjectManager\FactoryInterface { + /** + * Name of the class that generates logging wrappers + */ + const GENERATOR_NAME = 'Magento\Framework\ObjectManager\Profiler\Code\Generator\Logger'; + /** * @var \Magento\Framework\ObjectManager\FactoryInterface */ @@ -45,9 +50,12 @@ class FactoryDecorator implements \Magento\Framework\ObjectManager\FactoryInterf { $this->log->startCreating($requestedType); $result = $this->subject->create($requestedType, $arguments); - $loggerClassName = get_class($result) . "\\Logger"; - $wrappedResult = new $loggerClassName($result, $this->log); - $this->log->stopCreating($result); - return $wrappedResult; + if ($requestedType !== self::GENERATOR_NAME) { + $loggerClassName = get_class($result) . "\\Logger"; + $wrappedResult = new $loggerClassName($result, $this->log); + $this->log->stopCreating($result); + $result = $wrappedResult; + } + return $result; } } diff --git a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Profiler/FactoryDecoratorTest.php b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Profiler/FactoryDecoratorTest.php new file mode 100644 index 0000000000000000000000000000000000000000..1d0fa91e42286c72588946788f40491de72e925e --- /dev/null +++ b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/Profiler/FactoryDecoratorTest.php @@ -0,0 +1,75 @@ +<?php +/*** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ + +namespace Magento\Framework\ObjectManager\Test\Unit\Profiler; + +class FactoryDecoratorTest extends \PHPUnit_Framework_TestCase +{ + /** + * Name of the base class to wrap in logger + */ + const CLASS_NAME = 'Magento\Test\Di\WrappedClass'; + + /** + * Name of the wrapper class that does logging + */ + const LOGGER_NAME = 'Magento\Test\Di\WrappedClass\Logger'; + + /** + * Name of the class that generates wrappers - should not be wrapped by logger + */ + const GENERATOR_NAME = 'Magento\Framework\ObjectManager\Profiler\Code\Generator\Logger'; + + /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\ObjectManager\FactoryInterface*/ + private $objectManagerMock; + + /** @var \Magento\Framework\ObjectManager\Profiler\FactoryDecorator */ + private $model; + + public function setUp() + { + require_once __DIR__ . '/../_files/logger_classes.php'; + $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + + $this->objectManagerMock = $this->getMockBuilder('Magento\Framework\ObjectManager\FactoryInterface') + ->disableOriginalConstructor() + ->getMock(); + + // Instantiate SUT + $this->model = $objectManager->getObject( + 'Magento\Framework\ObjectManager\Profiler\FactoryDecorator', + ['subject' => $this->objectManagerMock] + ); + } + + public function testCreate() + { + $baseObjectName = self::CLASS_NAME; + $baseObject = new $baseObjectName(); + + $arguments = [1, 2, 3]; + + $this->objectManagerMock->expects($this->once()) + ->method('create') + ->with(self::CLASS_NAME, $arguments) + ->willReturn($baseObject); + + $this->assertInstanceOf(self::LOGGER_NAME, $this->model->create(self::CLASS_NAME, $arguments)); + } + + public function testCreateNeglectGenerator() + { + $arguments = [1, 2, 3]; + $loggerMock = $this->getMockBuilder(self::GENERATOR_NAME)->disableOriginalConstructor()->getMock(); + + $this->objectManagerMock->expects($this->once()) + ->method('create') + ->with(self::GENERATOR_NAME, $arguments) + ->willReturn($loggerMock); + + $this->assertSame($loggerMock, $this->model->create(self::GENERATOR_NAME, $arguments)); + } +} diff --git a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/_files/logger_classes.php b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/_files/logger_classes.php new file mode 100644 index 0000000000000000000000000000000000000000..e55641a0a272aec4eb42cf90468a14891c1db04a --- /dev/null +++ b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/_files/logger_classes.php @@ -0,0 +1,21 @@ +<?php +/*** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ + +namespace Magento\Test\Di { + /** + * Test classes used for \Magento\Framework\ObjectManager\Test\Unit\Profiler\FactoryDecoratorTest + */ + class WrappedClass + { + + } +} +namespace Magento\Test\Di\WrappedClass { + class Logger + { + + } +}