From 3823b63f71d38ae3327498810c040c0159328b13 Mon Sep 17 00:00:00 2001
From: Dale Sikkema <dsikkema@ebay.com>
Date: Wed, 29 Apr 2015 16:09:07 -0500
Subject: [PATCH] MAGETWO-35991: Profiling option #2 is broken

---
 .htaccess                                     |  1 +
 .../Profiler/FactoryDecorator.php             | 16 +++-
 .../Unit/Profiler/FactoryDecoratorTest.php    | 75 +++++++++++++++++++
 .../Test/Unit/_files/logger_classes.php       | 21 ++++++
 4 files changed, 109 insertions(+), 4 deletions(-)
 create mode 100644 lib/internal/Magento/Framework/ObjectManager/Test/Unit/Profiler/FactoryDecoratorTest.php
 create mode 100644 lib/internal/Magento/Framework/ObjectManager/Test/Unit/_files/logger_classes.php

diff --git a/.htaccess b/.htaccess
index 404488eb7ff..8df703b4689 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 ad3864500bb..89537732728 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 00000000000..1d0fa91e422
--- /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 00000000000..e55641a0a27
--- /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
+    {
+
+    }
+}
-- 
GitLab