|
| 1 | +<?php |
| 2 | +/*** |
| 3 | + * Copyright © 2015 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\Framework\ObjectManager\Test\Unit\Profiler; |
| 8 | + |
| 9 | +class FactoryDecoratorTest extends \PHPUnit_Framework_TestCase |
| 10 | +{ |
| 11 | + /** |
| 12 | + * Name of the base class to wrap in logger |
| 13 | + */ |
| 14 | + const CLASS_NAME = 'Magento\Test\Di\WrappedClass'; |
| 15 | + |
| 16 | + /** |
| 17 | + * Name of the wrapper class that does logging |
| 18 | + */ |
| 19 | + const LOGGER_NAME = 'Magento\Test\Di\WrappedClass\Logger'; |
| 20 | + |
| 21 | + /** |
| 22 | + * Name of the class that generates wrappers - should not be wrapped by logger |
| 23 | + */ |
| 24 | + const GENERATOR_NAME = 'Magento\Framework\ObjectManager\Profiler\Code\Generator\Logger'; |
| 25 | + |
| 26 | + /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\ObjectManager\FactoryInterface*/ |
| 27 | + private $objectManagerMock; |
| 28 | + |
| 29 | + /** @var \Magento\Framework\ObjectManager\Profiler\FactoryDecorator */ |
| 30 | + private $model; |
| 31 | + |
| 32 | + public function setUp() |
| 33 | + { |
| 34 | + require_once __DIR__ . '/../_files/logger_classes.php'; |
| 35 | + $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); |
| 36 | + |
| 37 | + $this->objectManagerMock = $this->getMockBuilder('Magento\Framework\ObjectManager\FactoryInterface') |
| 38 | + ->disableOriginalConstructor() |
| 39 | + ->getMock(); |
| 40 | + |
| 41 | + // Instantiate SUT |
| 42 | + $this->model = $objectManager->getObject( |
| 43 | + 'Magento\Framework\ObjectManager\Profiler\FactoryDecorator', |
| 44 | + ['subject' => $this->objectManagerMock] |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + public function testCreate() |
| 49 | + { |
| 50 | + $baseObjectName = self::CLASS_NAME; |
| 51 | + $baseObject = new $baseObjectName(); |
| 52 | + |
| 53 | + $arguments = [1, 2, 3]; |
| 54 | + |
| 55 | + $this->objectManagerMock->expects($this->once()) |
| 56 | + ->method('create') |
| 57 | + ->with(self::CLASS_NAME, $arguments) |
| 58 | + ->willReturn($baseObject); |
| 59 | + |
| 60 | + $this->assertInstanceOf(self::LOGGER_NAME, $this->model->create(self::CLASS_NAME, $arguments)); |
| 61 | + } |
| 62 | + |
| 63 | + public function testCreateNeglectGenerator() |
| 64 | + { |
| 65 | + $arguments = [1, 2, 3]; |
| 66 | + $loggerMock = $this->getMockBuilder(self::GENERATOR_NAME)->disableOriginalConstructor()->getMock(); |
| 67 | + |
| 68 | + $this->objectManagerMock->expects($this->once()) |
| 69 | + ->method('create') |
| 70 | + ->with(self::GENERATOR_NAME, $arguments) |
| 71 | + ->willReturn($loggerMock); |
| 72 | + |
| 73 | + $this->assertSame($loggerMock, $this->model->create(self::GENERATOR_NAME, $arguments)); |
| 74 | + } |
| 75 | +} |
0 commit comments