From 0ad16a870963e2dfa64033c800d215ba56a153c7 Mon Sep 17 00:00:00 2001 From: Joan He Date: Tue, 20 Jan 2015 11:03:41 -0600 Subject: [PATCH 01/15] MAGETWO-32769: added unit tests to \Magento\Framework\App\Area --- .../Magento/Framework/App/AreaTest.php | 311 ++++++++++++++++++ 1 file changed, 311 insertions(+) create mode 100644 dev/tests/unit/testsuite/Magento/Framework/App/AreaTest.php diff --git a/dev/tests/unit/testsuite/Magento/Framework/App/AreaTest.php b/dev/tests/unit/testsuite/Magento/Framework/App/AreaTest.php new file mode 100644 index 0000000000000..a47ecdafe5065 --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/Framework/App/AreaTest.php @@ -0,0 +1,311 @@ +defaultRenderer = \Magento\Framework\Phrase::getRenderer(); + $this->objectManager = new \Magento\TestFramework\Helper\ObjectManager($this); + $this->loggerMock = $this->getMockBuilder('Psr\Log\LoggerInterface') + ->disableOriginalConstructor() + ->getMock(); + $this->eventManagerMock = $this->getMockBuilder('Magento\Framework\Event\ManagerInterface') + ->disableOriginalConstructor() + ->getMock(); + $this->translatorMock = $this->getMockBuilder('Magento\Framework\TranslateInterface') + ->disableOriginalConstructor() + ->getMock(); + $this->diConfigLoaderMock = $this->getMockBuilder('Magento\Framework\App\ObjectManager\ConfigLoader') + ->disableOriginalConstructor() + ->getMock(); + $this->objectManagerMock = $this->getMockBuilder('Magento\Framework\ObjectManagerInterface') + ->disableOriginalConstructor() + ->getMock(); + $this->designMock = $this->getMockBuilder('Magento\Framework\App\DesignInterface') + ->disableOriginalConstructor() + ->getMock(); + $this->scopeResolverMock = $this->getMockBuilder('Magento\Framework\App\ScopeResolverInterface') + ->disableOriginalConstructor() + ->getMock(); + $scopeMock = $this->getMockBuilder('Magento\Framework\App\ScopeInterface') + ->disableOriginalConstructor() + ->getMock(); + $scopeMock->expects($this->any()) + ->method('getId') + ->will($this->returnValue('1')); + $this->scopeResolverMock->expects($this->any()) + ->method('getScope') + ->will($this->returnValue($scopeMock)); + $this->designExceptionsMock = $this->getMockBuilder('Magento\Framework\View\DesignExceptions') + ->disableOriginalConstructor() + ->getMock(); + $this->areaCode = Area::AREA_FRONTEND; + + $this->object = $this->objectManager->getObject( + 'Magento\Framework\App\Area', + [ + 'logger' => $this->loggerMock, + 'objectManager' => $this->objectManagerMock, + 'eventManager' => $this->eventManagerMock, + 'translator' => $this->translatorMock, + 'diConfigLoader' => $this->diConfigLoaderMock, + 'design' => $this->designMock, + 'scopeResolver' => $this->scopeResolverMock, + 'designExceptions' => $this->designExceptionsMock, + 'areaCode' => $this->areaCode, + ] + ); + } + + public function tearDown() + { + \Magento\Framework\Phrase::setRenderer($this->defaultRenderer); + } + + public function testLoadConfig() + { + $this->mockLoadConfig(); + $this->object->load(Area::PART_CONFIG); + } + + public function testLoadTranslate() + { + $this->translatorMock->expects($this->once()) + ->method('loadData'); + $renderMock = $this->getMockBuilder('Magento\Framework\Phrase\RendererInterface') + ->disableOriginalConstructor() + ->getMock(); + $this->objectManagerMock->expects($this->once()) + ->method('get') + ->with('Magento\Framework\Phrase\RendererInterface') + ->will($this->returnValue($renderMock)); + $this->object->load(Area::PART_TRANSLATE); + } + + public function testLoadDesign() + { + $designMock = $this->getMockBuilder('Magento\Framework\View\DesignInterface') + ->disableOriginalConstructor() + ->getMock(); + $this->objectManagerMock->expects($this->once()) + ->method('get') + ->with('Magento\Framework\View\DesignInterface') + ->will($this->returnValue($designMock)); + $designMock->expects($this->once()) + ->method('setArea') + ->with($this->areaCode) + ->willReturnSelf(); + $designMock->expects($this->once()) + ->method('setDefaultDesignTheme'); + $this->object->load(Area::PART_DESIGN); + } + + public function testLoadUnknownPart() + { + $this->objectManagerMock->expects($this->never()) + ->method('configure'); + $this->objectManagerMock->expects($this->never()) + ->method('get'); + $this->object->load('unknown part'); + } + + public function testLoad() + { + $this->mockLoadConfig(); + $this->translatorMock->expects($this->once()) + ->method('loadData'); + $renderMock = $this->getMockBuilder('Magento\Framework\Phrase\RendererInterface') + ->disableOriginalConstructor() + ->getMock(); + $designMock = $this->getMockBuilder('Magento\Framework\View\DesignInterface') + ->disableOriginalConstructor() + ->getMock(); + $designMock->expects($this->once()) + ->method('setArea') + ->with($this->areaCode) + ->willReturnSelf(); + $designMock->expects($this->once()) + ->method('setDefaultDesignTheme'); + $this->objectManagerMock->expects($this->exactly(2)) + ->method('get') + ->will($this->returnValueMap( + [ + ['Magento\Framework\Phrase\RendererInterface', $renderMock], + ['Magento\Framework\View\DesignInterface', $designMock], + ] + )); + $this->object->load(); + } + + private function mockLoadConfig() + { + $configs = ['dummy configs']; + $this->diConfigLoaderMock->expects($this->once()) + ->method('load') + ->with($this->areaCode) + ->will($this->returnValue($configs)); + $this->objectManagerMock->expects($this->once()) + ->method('configure') + ->with($configs); + } + + public function testDetectDesign() + { + $this->designExceptionsMock->expects($this->never()) + ->method('getThemeByRequest'); + $this->designMock->expects($this->once()) + ->method('loadChange') + ->with('1') + ->willReturnSelf(); + $designMock = $this->getMockBuilder('Magento\Framework\View\DesignInterface') + ->disableOriginalConstructor() + ->getMock(); + $this->objectManagerMock->expects($this->once()) + ->method('get') + ->with('Magento\Framework\View\DesignInterface') + ->will($this->returnValue($designMock)); + $this->designMock->expects($this->once()) + ->method('changeDesign') + ->with($designMock) + ->willReturnSelf(); + $this->object->detectDesign(); + } + + /** + * @param string|bool $value + * @param int $callNum + * @param int $callNum2 + * @dataProvider detectDesignByRequestDataProvider + */ + public function testDetectDesignByRequest($value, $callNum, $callNum2) + { + $this->designExceptionsMock->expects($this->once()) + ->method('getThemeByRequest') + ->will($this->returnValue($value)); + $designMock = $this->getMockBuilder('Magento\Framework\View\DesignInterface') + ->disableOriginalConstructor() + ->getMock(); + $designMock->expects($this->exactly($callNum)) + ->method('setDesignTheme'); + $this->objectManagerMock->expects($this->once()) + ->method('get') + ->with('Magento\Framework\View\DesignInterface') + ->will($this->returnValue($designMock)); + $this->designMock->expects($this->exactly($callNum2)) + ->method('loadChange') + ->with('1') + ->willReturnSelf(); + $this->designMock->expects($this->exactly($callNum2)) + ->method('changeDesign') + ->with($designMock) + ->willReturnSelf(); + $requestMock = $this->getMockBuilder('Magento\Framework\App\Request\Http') + ->disableOriginalConstructor() + ->getMock(); + $this->object->detectDesign($requestMock); + } + + public function detectDesignByRequestDataProvider() + { + return [ + [false, 0, 1], + ['theme', 1, 0], + ]; + } + + public function testDetectDesignByRequestWithException() + { + $exception = new \Exception('exception'); + $this->designExceptionsMock->expects($this->once()) + ->method('getThemeByRequest') + ->will($this->throwException($exception)); + $designMock = $this->getMockBuilder('Magento\Framework\View\DesignInterface') + ->disableOriginalConstructor() + ->getMock(); + $designMock->expects($this->never()) + ->method('setDesignTheme'); + $this->objectManagerMock->expects($this->once()) + ->method('get') + ->with('Magento\Framework\View\DesignInterface') + ->will($this->returnValue($designMock)); + $this->designMock->expects($this->once()) + ->method('loadChange') + ->with('1') + ->willReturnSelf(); + $this->designMock->expects($this->once()) + ->method('changeDesign') + ->with($designMock) + ->willReturnSelf(); + $requestMock = $this->getMockBuilder('Magento\Framework\App\Request\Http') + ->disableOriginalConstructor() + ->getMock(); + $this->loggerMock->expects($this->once()) + ->method('critical') + ->with($exception); + $this->object->detectDesign($requestMock); + } +} From 525a5674cb549ea4723f89e32481b204fcdc9726 Mon Sep 17 00:00:00 2001 From: Joan He Date: Tue, 20 Jan 2015 13:33:30 -0600 Subject: [PATCH 02/15] MAGETWO-32853: added unit tests for classes under lib/internal/Magento/Framework/App/Config --- .../Framework/App/Config/BaseFactoryTest.php | 47 +++++++++++++++ ...lPoolTest.php => ProcessorFactoryTest.php} | 4 +- .../Framework/App/Config/DataFactoryTest.php | 47 +++++++++++++++ .../App/Config/Initial/ReaderTest.php | 41 +++++++++++++ .../App/Config/Initial/SchemaLocatorTest.php | 57 ++++++++++++++++++ .../Config/Initial/_files/invalid_config.xml | 10 ++++ .../App/Config/Storage/WriterTest.php | 54 +++++++++++++++++ .../Framework/App/Config/ValueFactoryTest.php | 58 +++++++++++++++++++ .../App/Config/Data/ProcessorFactory.php | 2 +- 9 files changed, 317 insertions(+), 3 deletions(-) create mode 100644 dev/tests/unit/testsuite/Magento/Framework/App/Config/BaseFactoryTest.php rename dev/tests/unit/testsuite/Magento/Framework/App/Config/Data/{BackendModelPoolTest.php => ProcessorFactoryTest.php} (92%) create mode 100644 dev/tests/unit/testsuite/Magento/Framework/App/Config/DataFactoryTest.php create mode 100644 dev/tests/unit/testsuite/Magento/Framework/App/Config/Initial/SchemaLocatorTest.php create mode 100644 dev/tests/unit/testsuite/Magento/Framework/App/Config/Initial/_files/invalid_config.xml create mode 100644 dev/tests/unit/testsuite/Magento/Framework/App/Config/Storage/WriterTest.php create mode 100644 dev/tests/unit/testsuite/Magento/Framework/App/Config/ValueFactoryTest.php diff --git a/dev/tests/unit/testsuite/Magento/Framework/App/Config/BaseFactoryTest.php b/dev/tests/unit/testsuite/Magento/Framework/App/Config/BaseFactoryTest.php new file mode 100644 index 0000000000000..4cb7fe796385c --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/Framework/App/Config/BaseFactoryTest.php @@ -0,0 +1,47 @@ +objectManager = new \Magento\TestFramework\Helper\ObjectManager($this); + $this->objectManagerMock = $this->getMockBuilder('Magento\Framework\ObjectManagerInterface') + ->disableOriginalConstructor() + ->getMock(); + $this->model = $this->objectManager->getObject( + 'Magento\Framework\App\Config\BaseFactory', + ['objectManager' => $this->objectManagerMock] + ); + } + + public function testCreate() + { + $instanceMock = $this->getMockBuilder('Magento\Framework\App\Config\Base') + ->disableOriginalConstructor() + ->getMock(); + $this->objectManagerMock->expects($this->once()) + ->method('create') + ->will($this->returnValue($instanceMock)); + $this->assertSame($instanceMock, $this->model->create()); + } +} diff --git a/dev/tests/unit/testsuite/Magento/Framework/App/Config/Data/BackendModelPoolTest.php b/dev/tests/unit/testsuite/Magento/Framework/App/Config/Data/ProcessorFactoryTest.php similarity index 92% rename from dev/tests/unit/testsuite/Magento/Framework/App/Config/Data/BackendModelPoolTest.php rename to dev/tests/unit/testsuite/Magento/Framework/App/Config/Data/ProcessorFactoryTest.php index 07933f8e00ba7..eacb9eac5fd75 100644 --- a/dev/tests/unit/testsuite/Magento/Framework/App/Config/Data/BackendModelPoolTest.php +++ b/dev/tests/unit/testsuite/Magento/Framework/App/Config/Data/ProcessorFactoryTest.php @@ -5,7 +5,7 @@ */ namespace Magento\Framework\App\Config\Data; -class BackendModelPoolTest extends \PHPUnit_Framework_TestCase +class ProcessorFactoryTest extends \PHPUnit_Framework_TestCase { /** * @var \Magento\Framework\App\Config\Data\ProcessorFactory @@ -27,7 +27,6 @@ protected function setUp() $this->_objectManager = $this->getMock('Magento\Framework\ObjectManagerInterface'); $this->_model = new \Magento\Framework\App\Config\Data\ProcessorFactory($this->_objectManager); $this->_processorMock = $this->getMockForAbstractClass('Magento\Framework\App\Config\Data\ProcessorInterface'); - $this->_processorMock->expects($this->any())->method('processValue')->will($this->returnArgument(0)); } /** @@ -54,6 +53,7 @@ public function testGetModelWithCorrectInterface() /** * @covers \Magento\Framework\App\Config\Data\ProcessorFactory::get * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Magento\Framework\App\Config\Data\WrongBackendModel is not instance of \Magento\Framework\App\Config\Data\ProcessorInterface */ public function testGetModelWithWrongInterface() { diff --git a/dev/tests/unit/testsuite/Magento/Framework/App/Config/DataFactoryTest.php b/dev/tests/unit/testsuite/Magento/Framework/App/Config/DataFactoryTest.php new file mode 100644 index 0000000000000..cfdec1e5916e3 --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/Framework/App/Config/DataFactoryTest.php @@ -0,0 +1,47 @@ +objectManager = new \Magento\TestFramework\Helper\ObjectManager($this); + $this->objectManagerMock = $this->getMockBuilder('Magento\Framework\ObjectManagerInterface') + ->disableOriginalConstructor() + ->getMock(); + $this->model = $this->objectManager->getObject( + 'Magento\Framework\App\Config\DataFactory', + ['objectManager' => $this->objectManagerMock] + ); + } + + public function testCreate() + { + $instanceMock = $this->getMockBuilder('Magento\Framework\App\Config\Data') + ->disableOriginalConstructor() + ->getMock(); + $this->objectManagerMock->expects($this->once()) + ->method('create') + ->will($this->returnValue($instanceMock)); + $this->assertSame($instanceMock, $this->model->create()); + } +} diff --git a/dev/tests/unit/testsuite/Magento/Framework/App/Config/Initial/ReaderTest.php b/dev/tests/unit/testsuite/Magento/Framework/App/Config/Initial/ReaderTest.php index 5193cf725cb9d..982756ac16612 100644 --- a/dev/tests/unit/testsuite/Magento/Framework/App/Config/Initial/ReaderTest.php +++ b/dev/tests/unit/testsuite/Magento/Framework/App/Config/Initial/ReaderTest.php @@ -122,4 +122,45 @@ public function testReadValidConfig() $this->assertEquals($expectedConfig, $this->_model->read()); } + + /** + * @covers \Magento\Framework\App\Config\Initial\Reader::read + * @expectedException \Magento\Framework\Exception + * @expectedExceptionMessageRegExp /Invalid XML in file \w+/ + */ + public function testReadInvalidConfig() + { + $testXmlFilesList = [ + file_get_contents($this->_filePath . 'invalid_config.xml'), + file_get_contents($this->_filePath . 'initial_config2.xml'), + ]; + $expectedConfig = ['data' => [], 'metadata' => []]; + + $this->_fileResolverMock->expects( + $this->at(0) + )->method( + 'get' + )->with( + 'config.xml', + 'global' + )->will( + $this->returnValue($testXmlFilesList) + ); + + $this->_converterMock->expects( + $this->never() + )->method( + 'convert' + )->with( + $this->anything() + )->will( + $this->returnValue($expectedConfig) + ); + + $this->rootDirectory->expects($this->any())->method('getRelativePath')->will($this->returnArgument(0)); + + $this->rootDirectory->expects($this->any())->method('readFile')->will($this->returnValue('')); + + $this->assertEquals($expectedConfig, $this->_model->read()); + } } diff --git a/dev/tests/unit/testsuite/Magento/Framework/App/Config/Initial/SchemaLocatorTest.php b/dev/tests/unit/testsuite/Magento/Framework/App/Config/Initial/SchemaLocatorTest.php new file mode 100644 index 0000000000000..f98959489915c --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/Framework/App/Config/Initial/SchemaLocatorTest.php @@ -0,0 +1,57 @@ +objectManager = new \Magento\TestFramework\Helper\ObjectManager($this); + $this->_moduleReaderMock = $this->getMock('Magento\Framework\Module\Dir\Reader', [], [], '', false); + $this->_moduleReaderMock->expects( + $this->once() + )->method( + 'getModuleDir' + )->with( + 'etc', + 'moduleName' + )->will( + $this->returnValue('schema_dir') + ); + $this->_model = $this->objectManager->getObject( + 'Magento\Framework\App\Config\Initial\SchemaLocator', + [ + 'moduleReader' => $this->_moduleReaderMock, + 'moduleName' => 'moduleName', + ] + ); + } + + public function testGetSchema() + { + $this->assertEquals('schema_dir/config.xsd', $this->_model->getSchema()); + } + + public function testGetPerFileSchema() + { + $this->assertEquals('schema_dir/config.xsd', $this->_model->getPerFileSchema()); + } +} diff --git a/dev/tests/unit/testsuite/Magento/Framework/App/Config/Initial/_files/invalid_config.xml b/dev/tests/unit/testsuite/Magento/Framework/App/Config/Initial/_files/invalid_config.xml new file mode 100644 index 0000000000000..07a89760bb4c4 --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/Framework/App/Config/Initial/_files/invalid_config.xml @@ -0,0 +1,10 @@ + + + + + diff --git a/dev/tests/unit/testsuite/Magento/Framework/App/Config/Storage/WriterTest.php b/dev/tests/unit/testsuite/Magento/Framework/App/Config/Storage/WriterTest.php new file mode 100644 index 0000000000000..0b0805cb87f34 --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/Framework/App/Config/Storage/WriterTest.php @@ -0,0 +1,54 @@ +objectManager = new \Magento\TestFramework\Helper\ObjectManager($this); + $this->resource = $this->getMockBuilder('Magento\Framework\App\Config\Resource\ConfigInterface') + ->disableOriginalConstructor() + ->getMock(); + $this->model = $this->objectManager->getObject( + 'Magento\Framework\App\Config\Storage\Writer', + ['resource' => $this->resource] + ); + } + + public function testDelete() + { + $this->resource->expects($this->once()) + ->method('deleteConfig') + ->with('path', ScopeInterface::SCOPE_DEFAULT, 0); + $this->model->delete('path'); + } + + public function testSave() + { + $this->resource->expects($this->once()) + ->method('saveConfig') + ->with('path', 'value', ScopeInterface::SCOPE_DEFAULT, 0); + $this->model->save('path', 'value'); + } +} diff --git a/dev/tests/unit/testsuite/Magento/Framework/App/Config/ValueFactoryTest.php b/dev/tests/unit/testsuite/Magento/Framework/App/Config/ValueFactoryTest.php new file mode 100644 index 0000000000000..5501373422048 --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/Framework/App/Config/ValueFactoryTest.php @@ -0,0 +1,58 @@ +objectManager = new \Magento\TestFramework\Helper\ObjectManager($this); + $this->objectManagerMock = $this->getMockBuilder('Magento\Framework\ObjectManagerInterface') + ->disableOriginalConstructor() + ->getMock(); + $this->model = $this->objectManager->getObject( + 'Magento\Framework\App\Config\ValueFactory', + ['objectManager' => $this->objectManagerMock] + ); + } + + public function testCreate() + { + $instanceMock = $this->getMockBuilder('Magento\Framework\App\Config\ValueInterface') + ->disableOriginalConstructor() + ->getMock(); + $this->objectManagerMock->expects($this->once()) + ->method('create') + ->will($this->returnValue($instanceMock)); + $this->assertSame($instanceMock, $this->model->create()); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testCreateWithException() + { + $this->objectManagerMock->expects($this->once()) + ->method('create') + ->will($this->returnValue('somethingElse')); + $this->model->create(); + } +} diff --git a/lib/internal/Magento/Framework/App/Config/Data/ProcessorFactory.php b/lib/internal/Magento/Framework/App/Config/Data/ProcessorFactory.php index e34ec4519cd3b..8f473e2a37b2b 100644 --- a/lib/internal/Magento/Framework/App/Config/Data/ProcessorFactory.php +++ b/lib/internal/Magento/Framework/App/Config/Data/ProcessorFactory.php @@ -40,7 +40,7 @@ public function get($model) $instance = $this->_objectManager->create($model); if (!$instance instanceof ProcessorInterface) { throw new \InvalidArgumentException( - $model . ' does not instance of \Magento\Framework\App\Config\Data\ProcessorInterface' + $model . ' is not instance of \Magento\Framework\App\Config\Data\ProcessorInterface' ); } $this->_pool[$model] = $instance; From cd320bb086a4283652894fd6279767dd35431863 Mon Sep 17 00:00:00 2001 From: Joan He Date: Tue, 20 Jan 2015 15:53:45 -0600 Subject: [PATCH 03/15] MAGETWO-32861: added unit tests for classes under lib/internal/Magento/Framework/App/Http --- .../Framework/App/Http/ContextTest.php | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 dev/tests/unit/testsuite/Magento/Framework/App/Http/ContextTest.php diff --git a/dev/tests/unit/testsuite/Magento/Framework/App/Http/ContextTest.php b/dev/tests/unit/testsuite/Magento/Framework/App/Http/ContextTest.php new file mode 100644 index 0000000000000..a01afee94ee68 --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/Framework/App/Http/ContextTest.php @@ -0,0 +1,53 @@ +objectManager = new \Magento\TestFramework\Helper\ObjectManager($this); + $this->object = $this->objectManager->getObject('Magento\Framework\App\Http\Context'); + } + + public function testGetValue() + { + $this->assertNull($this->object->getValue('value')); + } + + public function testSetGetValue() + { + $this->object->setValue('value', 'value', 'default'); + $this->assertEquals('value', $this->object->getValue('value')); + } + + public function testSetUnsetGetValue() + { + $this->object->setValue('value', 'value', 'default'); + $this->object->unsValue('value'); + $this->assertEquals('default', $this->object->getValue('value')); + } + + public function testGetData() + { + $this->object->setValue('value1', 'value1', 'default1'); + $this->object->setValue('value2', 'value2', 'default2'); + $this->object->setValue('value3', 'value3', 'value3'); + $this->object->unsValue('value1'); + $this->assertEquals(['value2' => 'value2'], $this->object->getData()); + } +} From 53a92b3746eb05582e405d4350acab3cfdf1b05c Mon Sep 17 00:00:00 2001 From: Joan He Date: Wed, 21 Jan 2015 09:55:03 -0600 Subject: [PATCH 04/15] MAGETWO-32876: added unit tests for lib/internal/Magento/Framework/App/Resource/ConnectionFactory.php --- .../App/Resource/ConnectionFactoryTest.php | 86 +++++++++++++++++++ .../App/Resource/ConnectionFactory.php | 8 +- 2 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 dev/tests/unit/testsuite/Magento/Framework/App/Resource/ConnectionFactoryTest.php diff --git a/dev/tests/unit/testsuite/Magento/Framework/App/Resource/ConnectionFactoryTest.php b/dev/tests/unit/testsuite/Magento/Framework/App/Resource/ConnectionFactoryTest.php new file mode 100644 index 0000000000000..42d0600c9c4a4 --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/Framework/App/Resource/ConnectionFactoryTest.php @@ -0,0 +1,86 @@ +objectManager = new \Magento\TestFramework\Helper\ObjectManager($this); + $this->objectManagerMock = $this->getMockBuilder('Magento\Framework\ObjectManagerInterface') + ->disableOriginalConstructor() + ->getMock(); + $this->model = $this->objectManager->getObject( + 'Magento\Framework\App\Resource\ConnectionFactory', + ['objectManager' => $this->objectManagerMock] + ); + } + + public function testCreateNull() + { + $this->assertNull($this->model->create([])); + $this->assertNull($this->model->create(['something'])); + $this->assertNull($this->model->create(['active' => null])); + } + + public function testCreate() + { + $cacheAdapterMock = $this->getMockBuilder('Magento\Framework\Cache\FrontendInterface') + ->disableOriginalConstructor() + ->getMock(); + $loggerMock = $this->getMockBuilder('Magento\Framework\DB\LoggerInterface') + ->disableOriginalConstructor() + ->getMock(); + $adapterInstanceMock = $this->getMockBuilder('Magento\Framework\App\Resource\ConnectionAdapterInterface') + ->disableOriginalConstructor() + ->setMethods(['setCacheAdapter', 'getConnection']) + ->getMock(); + $adapterInstanceMock->expects($this->once()) + ->method('setCacheAdapter') + ->with($cacheAdapterMock) + ->willReturnSelf(); + $adapterInstanceMock->expects($this->once()) + ->method('getConnection') + ->with($loggerMock) + ->willReturnSelf(); + $this->objectManagerMock->expects($this->once()) + ->method('create') + ->will($this->returnValue($adapterInstanceMock)); + $poolMock = $this->getMockBuilder('Magento\Framework\App\Cache\Type\FrontendPool') + ->disableOriginalConstructor() + ->getMock(); + $poolMock->expects($this->once()) + ->method('get') + ->with(DdlCache::TYPE_IDENTIFIER) + ->will($this->returnValue($cacheAdapterMock)); + $this->objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap( + [ + ['Magento\Framework\DB\LoggerInterface', $loggerMock], + ['Magento\Framework\App\Cache\Type\FrontendPool', $poolMock], + ] + )); + $this->assertSame($adapterInstanceMock, $this->model->create(['active' => true])); + } +} diff --git a/lib/internal/Magento/Framework/App/Resource/ConnectionFactory.php b/lib/internal/Magento/Framework/App/Resource/ConnectionFactory.php index 5c370d6cd5a30..655ae1f45d8a4 100644 --- a/lib/internal/Magento/Framework/App/Resource/ConnectionFactory.php +++ b/lib/internal/Magento/Framework/App/Resource/ConnectionFactory.php @@ -22,9 +22,11 @@ class ConnectionFactory extends ModelConnectionFactory public function create(array $connectionConfig) { $connection = parent::create($connectionConfig); - /** @var \Magento\Framework\App\Cache\Type\FrontendPool $pool */ - $pool = $this->objectManager->get('Magento\Framework\App\Cache\Type\FrontendPool'); - $connection->setCacheAdapter($pool->get(DdlCache::TYPE_IDENTIFIER)); + if ($connection) { + /** @var \Magento\Framework\App\Cache\Type\FrontendPool $pool */ + $pool = $this->objectManager->get('Magento\Framework\App\Cache\Type\FrontendPool'); + $connection->setCacheAdapter($pool->get(DdlCache::TYPE_IDENTIFIER)); + } return $connection; } } From 1ea1f0d1814ee288080cc2af151547c6ebef4705 Mon Sep 17 00:00:00 2001 From: Joan He Date: Wed, 21 Jan 2015 15:05:19 -0600 Subject: [PATCH 05/15] MAGETWO-32901: Added unit tests to lib/internal/Magento/Framework/App/Response/Http/FileFactory.php --- .../App/Response/Http/FileFactoryTest.php | 185 ++++++++++++++++-- .../App/Response/Http/FileFactory.php | 23 ++- .../Response/Http/TestingPhpExitException.php | 15 ++ 3 files changed, 205 insertions(+), 18 deletions(-) create mode 100644 lib/internal/Magento/Framework/App/Response/Http/TestingPhpExitException.php diff --git a/dev/tests/unit/testsuite/Magento/Framework/App/Response/Http/FileFactoryTest.php b/dev/tests/unit/testsuite/Magento/Framework/App/Response/Http/FileFactoryTest.php index b6989570318e3..a03f0747f02c5 100644 --- a/dev/tests/unit/testsuite/Magento/Framework/App/Response/Http/FileFactoryTest.php +++ b/dev/tests/unit/testsuite/Magento/Framework/App/Response/Http/FileFactoryTest.php @@ -7,28 +7,34 @@ class FileFactoryTest extends \PHPUnit_Framework_TestCase { + /** + * @var \Magento\TestFramework\Helper\ObjectManager + */ + protected $objectManager; + /** * @var \Magento\Framework\App\Response\Http\FileFactory */ protected $_model; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Filesystem */ protected $_fileSystemMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\App\Response\Http */ protected $_responseMock; /** - * @var \Magento\Framework\Filesystem\Directory\WriteInterface + * @var \Magento\Framework\Filesystem\Directory\WriteInterface | \PHPUnit_Framework_MockObject_MockObject */ protected $_dirMock; protected function setUp() { + $this->objectManager = new \Magento\TestFramework\Helper\ObjectManager($this); $this->_fileSystemMock = $this->getMock( 'Magento\Framework\Filesystem', ['getDirectoryWrite'], @@ -57,21 +63,17 @@ protected function setUp() ); $this->_responseMock = $this->getMock( 'Magento\Framework\App\Response\Http', - ['setHeader', 'sendHeaders', '__wakeup'], + ['setHeader', 'sendHeaders', 'setHttpResponseCode', 'clearBody', 'setBody', '__wakeup'], [], '', false ); - $this->_responseMock->expects( - $this->any() - )->method( - 'setHeader' - )->will( - $this->returnValue($this->_responseMock) - ); - $this->_model = new \Magento\Framework\App\Response\Http\FileFactory( - $this->_responseMock, - $this->_fileSystemMock + $this->_model = $this->objectManager->getObject( + 'Magento\Framework\App\Response\Http\FileFactory', + [ + 'response' => $this->_responseMock, + 'filesystem' => $this->_fileSystemMock, + ] ); } @@ -92,6 +94,161 @@ public function testCreateIfFileNotExist() $file = 'some_file'; $content = ['type' => 'filename', 'value' => $file]; + $this->_responseMock->expects( + $this->never() + )->method( + 'setHeader' + )->will( + $this->returnSelf() + ); + $this->_responseMock->expects( + $this->never() + )->method( + 'setHttpResponseCode' + )->will( + $this->returnSelf() + ); $this->_model->create('fileName', $content); } + + /** + * @expectedException \Magento\Framework\App\Response\Http\TestingPhpExitException + */ + public function testCreateArrayContent() + { + if (!defined('UNIT_TESTING')) { + define('UNIT_TESTING', 1); + } + $file = 'some_file'; + $content = ['type' => 'filename', 'value' => $file]; + + $this->_dirMock->expects($this->once()) + ->method('isFile') + ->will($this->returnValue(true)); + $this->_dirMock->expects($this->once()) + ->method('stat') + ->will($this->returnValue(['size' => 100])); + $this->_responseMock->expects($this->exactly(6)) + ->method('setHeader') + ->will($this->returnSelf()); + $this->_responseMock->expects($this->once()) + ->method('setHttpResponseCode') + ->with(200) + ->will($this->returnSelf()); + $this->_responseMock->expects($this->once()) + ->method('sendHeaders') + ->will($this->returnSelf()); + + $streamMock = $this->getMockBuilder('Magento\Framework\Filesystem\File\WriteInterface') + ->disableOriginalConstructor()->getMock(); + $this->_dirMock->expects($this->once()) + ->method('openFile') + ->will($this->returnValue($streamMock)); + $this->_dirMock->expects($this->never()) + ->method('delete') + ->will($this->returnValue($streamMock)); + $streamMock->expects($this->at(1)) + ->method('eof') + ->will($this->returnValue(false)); + $streamMock->expects($this->at(2)) + ->method('eof') + ->will($this->returnValue(true)); + $streamMock->expects($this->once()) + ->method('read'); + $streamMock->expects($this->once()) + ->method('close'); + $this->_model->create('fileName', $content); + } + + /** + * @expectedException \Magento\Framework\App\Response\Http\TestingPhpExitException + */ + public function testCreateArrayContentRm() + { + if (!defined('UNIT_TESTING')) { + define('UNIT_TESTING', 1); + } + $file = 'some_file'; + $content = ['type' => 'filename', 'value' => $file, 'rm' => 1]; + + $this->_dirMock->expects($this->once()) + ->method('isFile') + ->will($this->returnValue(true)); + $this->_dirMock->expects($this->once()) + ->method('stat') + ->will($this->returnValue(['size' => 100])); + $this->_responseMock->expects($this->exactly(6)) + ->method('setHeader') + ->will($this->returnSelf()); + $this->_responseMock->expects($this->once()) + ->method('setHttpResponseCode') + ->with(200) + ->will($this->returnSelf()); + $this->_responseMock->expects($this->once()) + ->method('sendHeaders') + ->will($this->returnSelf()); + + $streamMock = $this->getMockBuilder('Magento\Framework\Filesystem\File\WriteInterface') + ->disableOriginalConstructor()->getMock(); + $this->_dirMock->expects($this->once()) + ->method('openFile') + ->will($this->returnValue($streamMock)); + $this->_dirMock->expects($this->once()) + ->method('delete') + ->will($this->returnValue($streamMock)); + $streamMock->expects($this->at(1)) + ->method('eof') + ->will($this->returnValue(false)); + $streamMock->expects($this->at(2)) + ->method('eof') + ->will($this->returnValue(true)); + $streamMock->expects($this->once()) + ->method('read'); + $streamMock->expects($this->once()) + ->method('close'); + $this->_model->create('fileName', $content); + } + + public function testCreateStringContent() + { + $this->_dirMock->expects($this->never()) + ->method('isFile') + ->will($this->returnValue(true)); + $this->_dirMock->expects($this->never()) + ->method('stat') + ->will($this->returnValue(['size' => 100])); + $this->_responseMock->expects($this->exactly(6)) + ->method('setHeader') + ->will($this->returnSelf()); + $this->_responseMock->expects($this->once()) + ->method('setHttpResponseCode') + ->with(200) + ->will($this->returnSelf()); + $this->_responseMock->expects($this->once()) + ->method('clearBody') + ->will($this->returnSelf()); + $this->_responseMock->expects($this->once()) + ->method('setBody') + ->will($this->returnSelf()); + $this->_responseMock->expects($this->never()) + ->method('sendHeaders') + ->will($this->returnSelf()); + + $streamMock = $this->getMockBuilder('Magento\Framework\Filesystem\File\WriteInterface') + ->disableOriginalConstructor()->getMock(); + $this->_dirMock->expects($this->never()) + ->method('openFile') + ->will($this->returnValue($streamMock)); + $this->_dirMock->expects($this->never()) + ->method('delete') + ->will($this->returnValue($streamMock)); + $streamMock->expects($this->never()) + ->method('eof') + ->will($this->returnValue(false)); + $streamMock->expects($this->never()) + ->method('read'); + $streamMock->expects($this->never()) + ->method('close'); + $this->assertSame($this->_responseMock, $this->_model->create('fileName', 'content')); + } } diff --git a/lib/internal/Magento/Framework/App/Response/Http/FileFactory.php b/lib/internal/Magento/Framework/App/Response/Http/FileFactory.php index 4ceab8ac5a1a9..4e5a1c77d0d74 100644 --- a/lib/internal/Magento/Framework/App/Response/Http/FileFactory.php +++ b/lib/internal/Magento/Framework/App/Response/Http/FileFactory.php @@ -66,6 +66,9 @@ public function create( if ($content['type'] == 'filename') { $isFile = true; $file = $content['value']; + if (!$dir->isFile($file)) { + throw new \Exception(__('File not found')); + } $contentLength = $dir->stat($file)['size']; } } @@ -100,9 +103,6 @@ public function create( if (!is_null($content)) { if ($isFile) { - if (!$dir->isFile($file)) { - throw new \Exception(__('File not found')); - } $this->_response->sendHeaders(); $stream = $dir->openFile($file, 'r'); while (!$stream->eof()) { @@ -113,7 +113,7 @@ public function create( if (!empty($content['rm'])) { $dir->delete($file); } - exit(0); + self::exitphp(0); } else { $this->_response->clearBody(); $this->_response->setBody($content); @@ -121,4 +121,19 @@ public function create( } return $this->_response; } + + /** + * Call exit + * + * @param $code + * @throws TestingPhpExitException + */ + static function exitphp($code) + { + if (defined('UNIT_TESTING')) { + throw new \Magento\Framework\App\Response\Http\TestingPhpExitException(); + } else { + exit($code); + } + } } diff --git a/lib/internal/Magento/Framework/App/Response/Http/TestingPhpExitException.php b/lib/internal/Magento/Framework/App/Response/Http/TestingPhpExitException.php new file mode 100644 index 0000000000000..3f486c4afae6b --- /dev/null +++ b/lib/internal/Magento/Framework/App/Response/Http/TestingPhpExitException.php @@ -0,0 +1,15 @@ + Date: Wed, 21 Jan 2015 15:17:00 -0600 Subject: [PATCH 06/15] MAGETWO-32876: added unit tests for lib/internal/Magento/Framework/App/Resource/ConnectionFactory.php --- .../App/Resource/ConnectionFactoryTest.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/dev/tests/unit/testsuite/Magento/Framework/App/Resource/ConnectionFactoryTest.php b/dev/tests/unit/testsuite/Magento/Framework/App/Resource/ConnectionFactoryTest.php index 42d0600c9c4a4..74105e5c2d303 100644 --- a/dev/tests/unit/testsuite/Magento/Framework/App/Resource/ConnectionFactoryTest.php +++ b/dev/tests/unit/testsuite/Magento/Framework/App/Resource/ConnectionFactoryTest.php @@ -38,6 +38,8 @@ protected function setUp() public function testCreateNull() { + $this->objectManagerMock->expects($this->never()) + ->method('get'); $this->assertNull($this->model->create([])); $this->assertNull($this->model->create(['something'])); $this->assertNull($this->model->create(['active' => null])); @@ -51,21 +53,24 @@ public function testCreate() $loggerMock = $this->getMockBuilder('Magento\Framework\DB\LoggerInterface') ->disableOriginalConstructor() ->getMock(); - $adapterInstanceMock = $this->getMockBuilder('Magento\Framework\App\Resource\ConnectionAdapterInterface') + $connectionAdapterMock = $this->getMockBuilder('Magento\Framework\App\Resource\ConnectionAdapterInterface') + ->disableOriginalConstructor() + ->getMock(); + $adapterInstanceMock = $this->getMockBuilder('Magento\Framework\DB\Adapter\AdapterInterface') ->disableOriginalConstructor() - ->setMethods(['setCacheAdapter', 'getConnection']) ->getMock(); $adapterInstanceMock->expects($this->once()) ->method('setCacheAdapter') ->with($cacheAdapterMock) ->willReturnSelf(); - $adapterInstanceMock->expects($this->once()) + $connectionAdapterMock->expects($this->once()) ->method('getConnection') ->with($loggerMock) - ->willReturnSelf(); + ->will($this->returnValue($adapterInstanceMock)); $this->objectManagerMock->expects($this->once()) ->method('create') - ->will($this->returnValue($adapterInstanceMock)); + ->with('Magento\Framework\App\Resource\ConnectionAdapterInterface') + ->will($this->returnValue($connectionAdapterMock)); $poolMock = $this->getMockBuilder('Magento\Framework\App\Cache\Type\FrontendPool') ->disableOriginalConstructor() ->getMock(); From bc0e1c9361713fbc64ec060139b971216c6ea364 Mon Sep 17 00:00:00 2001 From: Joan He Date: Wed, 21 Jan 2015 15:21:22 -0600 Subject: [PATCH 07/15] MAGETWO-32861: added unit tests for classes under lib/internal/Magento/Framework/App/Http --- .../Framework/App/Http/ContextTest.php | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/dev/tests/unit/testsuite/Magento/Framework/App/Http/ContextTest.php b/dev/tests/unit/testsuite/Magento/Framework/App/Http/ContextTest.php index a01afee94ee68..6cf82d0261205 100644 --- a/dev/tests/unit/testsuite/Magento/Framework/App/Http/ContextTest.php +++ b/dev/tests/unit/testsuite/Magento/Framework/App/Http/ContextTest.php @@ -26,28 +26,28 @@ public function setUp() public function testGetValue() { - $this->assertNull($this->object->getValue('value')); + $this->assertNull($this->object->getValue('key')); } public function testSetGetValue() { - $this->object->setValue('value', 'value', 'default'); - $this->assertEquals('value', $this->object->getValue('value')); + $this->object->setValue('key', 'value', 'default'); + $this->assertEquals('value', $this->object->getValue('key')); } public function testSetUnsetGetValue() { - $this->object->setValue('value', 'value', 'default'); - $this->object->unsValue('value'); - $this->assertEquals('default', $this->object->getValue('value')); + $this->object->setValue('key', 'value', 'default'); + $this->object->unsValue('key'); + $this->assertEquals('default', $this->object->getValue('key')); } public function testGetData() { - $this->object->setValue('value1', 'value1', 'default1'); - $this->object->setValue('value2', 'value2', 'default2'); - $this->object->setValue('value3', 'value3', 'value3'); - $this->object->unsValue('value1'); - $this->assertEquals(['value2' => 'value2'], $this->object->getData()); + $this->object->setValue('key1', 'value1', 'default1'); + $this->object->setValue('key2', 'value2', 'default2'); + $this->object->setValue('key3', 'value3', 'value3'); + $this->object->unsValue('key1'); + $this->assertEquals(['key2' => 'value2'], $this->object->getData()); } } From b6e2de863cd03448856115b55295a70dbb136d54 Mon Sep 17 00:00:00 2001 From: Joan He Date: Wed, 21 Jan 2015 15:26:46 -0600 Subject: [PATCH 08/15] MAGETWO-32769: added unit tests to \Magento\Framework\App\Area --- dev/tests/unit/testsuite/Magento/Framework/App/AreaTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dev/tests/unit/testsuite/Magento/Framework/App/AreaTest.php b/dev/tests/unit/testsuite/Magento/Framework/App/AreaTest.php index a47ecdafe5065..fa344005cf9fe 100644 --- a/dev/tests/unit/testsuite/Magento/Framework/App/AreaTest.php +++ b/dev/tests/unit/testsuite/Magento/Framework/App/AreaTest.php @@ -128,7 +128,7 @@ public function tearDown() public function testLoadConfig() { - $this->mockLoadConfig(); + $this->verifyLoadConfig(); $this->object->load(Area::PART_CONFIG); } @@ -175,7 +175,7 @@ public function testLoadUnknownPart() public function testLoad() { - $this->mockLoadConfig(); + $this->verifyLoadConfig(); $this->translatorMock->expects($this->once()) ->method('loadData'); $renderMock = $this->getMockBuilder('Magento\Framework\Phrase\RendererInterface') @@ -201,7 +201,7 @@ public function testLoad() $this->object->load(); } - private function mockLoadConfig() + private function verifyLoadConfig() { $configs = ['dummy configs']; $this->diConfigLoaderMock->expects($this->once()) From 91d51b07dda1348a6fb6946b80a71960aa88cbbb Mon Sep 17 00:00:00 2001 From: Joan He Date: Thu, 22 Jan 2015 08:27:41 -0600 Subject: [PATCH 09/15] MAGETWO-32769: added unit tests to \Magento\Framework\App\Area --- .../unit/testsuite/Magento/Framework/App/AreaTest.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/dev/tests/unit/testsuite/Magento/Framework/App/AreaTest.php b/dev/tests/unit/testsuite/Magento/Framework/App/AreaTest.php index fa344005cf9fe..864f5e6c07cbf 100644 --- a/dev/tests/unit/testsuite/Magento/Framework/App/AreaTest.php +++ b/dev/tests/unit/testsuite/Magento/Framework/App/AreaTest.php @@ -8,6 +8,8 @@ class AreaTest extends \PHPUnit_Framework_TestCase { + const SCOPE_ID = '1'; + /** * @var \Magento\TestFramework\Helper\ObjectManager */ @@ -96,7 +98,7 @@ public function setUp() ->getMock(); $scopeMock->expects($this->any()) ->method('getId') - ->will($this->returnValue('1')); + ->will($this->returnValue(self::SCOPE_ID)); $this->scopeResolverMock->expects($this->any()) ->method('getScope') ->will($this->returnValue($scopeMock)); @@ -219,7 +221,7 @@ public function testDetectDesign() ->method('getThemeByRequest'); $this->designMock->expects($this->once()) ->method('loadChange') - ->with('1') + ->with(self::SCOPE_ID) ->willReturnSelf(); $designMock = $this->getMockBuilder('Magento\Framework\View\DesignInterface') ->disableOriginalConstructor() @@ -257,7 +259,7 @@ public function testDetectDesignByRequest($value, $callNum, $callNum2) ->will($this->returnValue($designMock)); $this->designMock->expects($this->exactly($callNum2)) ->method('loadChange') - ->with('1') + ->with(self::SCOPE_ID) ->willReturnSelf(); $this->designMock->expects($this->exactly($callNum2)) ->method('changeDesign') @@ -294,7 +296,7 @@ public function testDetectDesignByRequestWithException() ->will($this->returnValue($designMock)); $this->designMock->expects($this->once()) ->method('loadChange') - ->with('1') + ->with(self::SCOPE_ID) ->willReturnSelf(); $this->designMock->expects($this->once()) ->method('changeDesign') From a4761ce77d69977d9bad282d34bb27d1dec86f72 Mon Sep 17 00:00:00 2001 From: Joan He Date: Thu, 22 Jan 2015 10:47:44 -0600 Subject: [PATCH 10/15] MAGETWO-32913: added unit tests for classes under lib/internal/Magento/Framework/App/Router --- .../App/Router/ActionList/ReaderTest.php | 69 ++++++++ .../Framework/App/Router/ActionListTest.php | 162 ++++++++++++++++++ 2 files changed, 231 insertions(+) create mode 100644 dev/tests/unit/testsuite/Magento/Framework/App/Router/ActionList/ReaderTest.php create mode 100644 dev/tests/unit/testsuite/Magento/Framework/App/Router/ActionListTest.php diff --git a/dev/tests/unit/testsuite/Magento/Framework/App/Router/ActionList/ReaderTest.php b/dev/tests/unit/testsuite/Magento/Framework/App/Router/ActionList/ReaderTest.php new file mode 100644 index 0000000000000..917ddc23414d1 --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/Framework/App/Router/ActionList/ReaderTest.php @@ -0,0 +1,69 @@ +objectManager = new \Magento\TestFramework\Helper\ObjectManager($this); + $this->moduleReaderMock = $this->getMockBuilder('Magento\Framework\Module\Dir\Reader') + ->disableOriginalConstructor() + ->getMock(); + $this->actionListReader = $this->objectManager->getObject( + 'Magento\Framework\App\Router\ActionList\Reader', + ['moduleReader' => $this->moduleReaderMock] + ); + } + + /** + * @param array $actionFiles + * @param array $expected + * @dataProvider readDataProvider + */ + public function testRead($actionFiles, $expected) + { + $this->moduleReaderMock->expects($this->once()) + ->method('getActionFiles') + ->willReturn($actionFiles); + $this->assertEquals($expected, $this->actionListReader->read()); + } + + public function readDataProvider() + { + return [ + [[], []], + [ + [ + 'Magento/Backend/Controller/Adminhtml/Cache.php', + 'Magento/Backend/Controller/Adminhtml/Index.php' + ], + [ + 'magento\backend\controller\adminhtml\cache' => 'Magento\Backend\Controller\Adminhtml\Cache', + 'magento\backend\controller\adminhtml\index' => 'Magento\Backend\Controller\Adminhtml\Index' + + ] + ] + ]; + } +} diff --git a/dev/tests/unit/testsuite/Magento/Framework/App/Router/ActionListTest.php b/dev/tests/unit/testsuite/Magento/Framework/App/Router/ActionListTest.php new file mode 100644 index 0000000000000..aacf7f3944dca --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/Framework/App/Router/ActionListTest.php @@ -0,0 +1,162 @@ +objectManager = new \Magento\TestFramework\Helper\ObjectManager($this); + $this->cacheMock = $this->getMockBuilder('Magento\Framework\Config\CacheInterface') + ->disableOriginalConstructor() + ->getMock(); + $this->actionReaderMock = $this->getMockBuilder('Magento\Framework\App\Router\ActionList\Reader') + ->disableOriginalConstructor() + ->getMock(); + } + + public function testConstructorCachedData() + { + $this->cacheMock->expects($this->once()) + ->method('load') + ->will($this->returnValue(serialize('data'))); + $this->cacheMock->expects($this->never()) + ->method('save'); + $this->actionReaderMock->expects($this->never()) + ->method('read'); + $this->actionList = $this->objectManager->getObject( + 'Magento\Framework\App\Router\ActionList', + [ + 'cache' => $this->cacheMock, + 'actionReader' => $this->actionReaderMock, + ] + ); + } + + public function testConstructorNoCachedData() + { + $this->cacheMock->expects($this->once()) + ->method('load') + ->will($this->returnValue(false)); + $this->cacheMock->expects($this->once()) + ->method('save'); + $this->actionReaderMock->expects($this->once()) + ->method('read') + ->will($this->returnValue('data')); + $this->actionList = $this->objectManager->getObject( + 'Magento\Framework\App\Router\ActionList', + [ + 'cache' => $this->cacheMock, + 'actionReader' => $this->actionReaderMock, + ] + ); + } + + /** + * @param string $module + * @param string $area + * @param string $namespace + * @param string $action + * @param array $data + * @param string|null $expected + * @dataProvider getDataProvider + */ + public function testGet($module, $area, $namespace, $action, $data, $expected) + { + + $this->cacheMock->expects($this->once()) + ->method('load') + ->will($this->returnValue(false)); + $this->cacheMock->expects($this->once()) + ->method('save'); + $this->actionReaderMock->expects($this->once()) + ->method('read') + ->will($this->returnValue($data)); + $this->actionList = $this->objectManager->getObject( + 'Magento\Framework\App\Router\ActionList', + [ + 'cache' => $this->cacheMock, + 'actionReader' => $this->actionReaderMock, + ] + ); + $this->assertEquals($expected, $this->actionList->get($module, $area, $namespace, $action)); + } + + public function getDataProvider() + { + $actionClass = $this->getMockClass( + 'Magento\Framework\App\ActionInterface', + ['dispatch', 'getResponse'], + [], + 'Magento_Module_Controller_Area_Namespace_Index' + ); + + return [ + [ + 'Magento_Module', + 'Area', + 'Namespace', + 'Index', + ['magento\module\controller\area\namespace\index' => 'Magento_Module_Controller_Area_Namespace_Index'], + $actionClass + ], + [ + 'Magento_Module', + '', + 'Namespace', + 'Index', + ['magento\module\controller\namespace\index' => 'Magento_Module_Controller_Area_Namespace_Index'], + $actionClass + ], + [ + 'Magento_Module', + 'Area', + 'Namespace', + 'Catch', + ['magento\module\controller\area\namespace\catchaction' => 'Magento_Module_Controller_Area_Namespace_Index'], + $actionClass + ], + [ + 'Magento_Module', + 'Area', + 'Namespace', + 'Index', + ['magento\module\controller\area\namespace\index' => 'Magento_Module_Controller_Area_Namespace_Index2'], + null + ], + [ + 'Magento_Module', + 'Area', + 'Namespace', + 'Index', + [], + null + ], + ]; + } +} From bfd6bac9e29bd5c80f7f54e98676cbb5c20bde12 Mon Sep 17 00:00:00 2001 From: Joan He Date: Thu, 22 Jan 2015 15:20:17 -0600 Subject: [PATCH 11/15] MAGETWO-32853: added unit tests for classes under lib/internal/Magento/Framework/App/Config --- .../Magento/Test/AbstractFactoryTestCase.php | 63 +++++++ .../Framework/App/Config/BaseFactoryTest.php | 39 +--- .../Framework/App/Config/DataFactoryTest.php | 39 +--- .../App/Config/Initial/ReaderTest.php | 166 ++++++++---------- .../App/Config/Initial/SchemaLocatorTest.php | 14 +- .../App/Config/Storage/WriterTest.php | 10 ++ .../Framework/App/Config/ValueFactoryTest.php | 41 +---- 7 files changed, 165 insertions(+), 207 deletions(-) create mode 100644 dev/tests/unit/framework/Magento/Test/AbstractFactoryTestCase.php diff --git a/dev/tests/unit/framework/Magento/Test/AbstractFactoryTestCase.php b/dev/tests/unit/framework/Magento/Test/AbstractFactoryTestCase.php new file mode 100644 index 0000000000000..3727868d8ab70 --- /dev/null +++ b/dev/tests/unit/framework/Magento/Test/AbstractFactoryTestCase.php @@ -0,0 +1,63 @@ +objectManager = new \Magento\TestFramework\Helper\ObjectManager($this); + $this->objectManagerMock = $this->getMockBuilder('Magento\Framework\ObjectManagerInterface') + ->disableOriginalConstructor() + ->getMock(); + $this->factory = $this->objectManager->getObject( + $this->factoryClassName, + ['objectManager' => $this->objectManagerMock] + ); + } + + public function testCreate() + { + $instanceMock = $this->getMockBuilder($this->instanceClassName) + ->disableOriginalConstructor() + ->getMock(); + $this->objectManagerMock->expects($this->once()) + ->method('create') + ->will($this->returnValue($instanceMock)); + $this->assertSame($instanceMock, $this->factory->create()); + } +} diff --git a/dev/tests/unit/testsuite/Magento/Framework/App/Config/BaseFactoryTest.php b/dev/tests/unit/testsuite/Magento/Framework/App/Config/BaseFactoryTest.php index 4cb7fe796385c..f6448f3f96ddd 100644 --- a/dev/tests/unit/testsuite/Magento/Framework/App/Config/BaseFactoryTest.php +++ b/dev/tests/unit/testsuite/Magento/Framework/App/Config/BaseFactoryTest.php @@ -5,43 +5,12 @@ */ namespace Magento\Framework\App\Config; -class BaseFactoryTest extends \PHPUnit_Framework_TestCase +class BaseFactoryTest extends \Magento\Test\AbstractFactoryTestCase { - /** - * @var \Magento\TestFramework\Helper\ObjectManager - */ - protected $objectManager; - - /** - * @var \Magento\Framework\App\Config\BaseFactory - */ - protected $model; - - /** - * @var \Magento\Framework\ObjectManagerInterface | \PHPUnit_Framework_MockObject_MockObject - */ - protected $objectManagerMock; - protected function setUp() { - $this->objectManager = new \Magento\TestFramework\Helper\ObjectManager($this); - $this->objectManagerMock = $this->getMockBuilder('Magento\Framework\ObjectManagerInterface') - ->disableOriginalConstructor() - ->getMock(); - $this->model = $this->objectManager->getObject( - 'Magento\Framework\App\Config\BaseFactory', - ['objectManager' => $this->objectManagerMock] - ); - } - - public function testCreate() - { - $instanceMock = $this->getMockBuilder('Magento\Framework\App\Config\Base') - ->disableOriginalConstructor() - ->getMock(); - $this->objectManagerMock->expects($this->once()) - ->method('create') - ->will($this->returnValue($instanceMock)); - $this->assertSame($instanceMock, $this->model->create()); + $this->instanceClassName = 'Magento\Framework\App\Config\Base'; + $this->factoryClassName = 'Magento\Framework\App\Config\BaseFactory'; + parent::setUp(); } } diff --git a/dev/tests/unit/testsuite/Magento/Framework/App/Config/DataFactoryTest.php b/dev/tests/unit/testsuite/Magento/Framework/App/Config/DataFactoryTest.php index cfdec1e5916e3..3f578938d546f 100644 --- a/dev/tests/unit/testsuite/Magento/Framework/App/Config/DataFactoryTest.php +++ b/dev/tests/unit/testsuite/Magento/Framework/App/Config/DataFactoryTest.php @@ -5,43 +5,12 @@ */ namespace Magento\Framework\App\Config; -class DataFactoryTest extends \PHPUnit_Framework_TestCase +class DataFactoryTest extends \Magento\Test\AbstractFactoryTestCase { - /** - * @var \Magento\TestFramework\Helper\ObjectManager - */ - protected $objectManager; - - /** - * @var \Magento\Framework\App\Config\DataFactory - */ - protected $model; - - /** - * @var \Magento\Framework\ObjectManagerInterface | \PHPUnit_Framework_MockObject_MockObject - */ - protected $objectManagerMock; - protected function setUp() { - $this->objectManager = new \Magento\TestFramework\Helper\ObjectManager($this); - $this->objectManagerMock = $this->getMockBuilder('Magento\Framework\ObjectManagerInterface') - ->disableOriginalConstructor() - ->getMock(); - $this->model = $this->objectManager->getObject( - 'Magento\Framework\App\Config\DataFactory', - ['objectManager' => $this->objectManagerMock] - ); - } - - public function testCreate() - { - $instanceMock = $this->getMockBuilder('Magento\Framework\App\Config\Data') - ->disableOriginalConstructor() - ->getMock(); - $this->objectManagerMock->expects($this->once()) - ->method('create') - ->will($this->returnValue($instanceMock)); - $this->assertSame($instanceMock, $this->model->create()); + $this->instanceClassName = 'Magento\Framework\App\Config\Data'; + $this->factoryClassName = 'Magento\Framework\App\Config\DataFactory'; + parent::setUp(); } } diff --git a/dev/tests/unit/testsuite/Magento/Framework/App/Config/Initial/ReaderTest.php b/dev/tests/unit/testsuite/Magento/Framework/App/Config/Initial/ReaderTest.php index 982756ac16612..7fcf4042a15d6 100644 --- a/dev/tests/unit/testsuite/Magento/Framework/App/Config/Initial/ReaderTest.php +++ b/dev/tests/unit/testsuite/Magento/Framework/App/Config/Initial/ReaderTest.php @@ -9,60 +9,60 @@ class ReaderTest extends \PHPUnit_Framework_TestCase { + /** + * @var \Magento\TestFramework\Helper\ObjectManager + */ + protected $objectManager; + /** * @var \Magento\Framework\App\Config\Initial\Reader */ - protected $_model; + protected $model; /** - * @var \Magento\Framework\Config\FileResolverInterface|\PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\Config\FileResolverInterface | \PHPUnit_Framework_MockObject_MockObject */ - protected $_fileResolverMock; + protected $fileResolverMock; /** - * @var \Magento\Framework\App\Config\Initial\Converter|\PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\App\Config\Initial\Converter | \PHPUnit_Framework_MockObject_MockObject */ - protected $_converterMock; + protected $converterMock; /** * @var string */ - protected $_filePath; + protected $filePath; /** - * @var \Magento\Framework\Filesystem\Directory\Read|\PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\Config\ValidationStateInterface | \PHPUnit_Framework_MockObject_MockObject */ - protected $rootDirectory; + protected $validationStateMock; + + /** + * @var \Magento\Framework\App\Config\Initial\SchemaLocator | \PHPUnit_Framework_MockObject_MockObject + */ + protected $schemaLocatorMock; protected function setUp() { - $this->_filePath = __DIR__ . '/_files/'; - $this->_fileResolverMock = $this->getMock('Magento\Framework\Config\FileResolverInterface'); - $this->_converterMock = $this->getMock('Magento\Framework\App\Config\Initial\Converter'); - $schemaLocatorMock = $this->getMock( + $this->objectManager = new \Magento\TestFramework\Helper\ObjectManager($this); + $this->filePath = __DIR__ . '/_files/'; + $this->fileResolverMock = $this->getMock('Magento\Framework\Config\FileResolverInterface'); + $this->converterMock = $this->getMock('Magento\Framework\App\Config\Initial\Converter'); + $this->schemaLocatorMock = $this->getMock( 'Magento\Framework\App\Config\Initial\SchemaLocator', [], [], '', false ); - $validationStateMock = $this->getMock('Magento\Framework\Config\ValidationStateInterface'); - $validationStateMock->expects($this->once())->method('isValidated')->will($this->returnValue(true)); - $schemaFile = $this->_filePath . 'config.xsd'; - $schemaLocatorMock->expects($this->once())->method('getSchema')->will($this->returnValue($schemaFile)); - $this->rootDirectory = $this->getMock( - 'Magento\Framework\Filesystem\Directory\Read', - ['readFile', 'getRelativePath'], - [], - '', - false - ); - $this->_model = new \Magento\Framework\App\Config\Initial\Reader( - $this->_fileResolverMock, - $this->_converterMock, - $schemaLocatorMock, - $validationStateMock - ); + $this->validationStateMock = $this->getMock('Magento\Framework\Config\ValidationStateInterface'); + } + + public function testConstructor() + { + $this->createModelAndVerifyConstructor(); } /** @@ -70,18 +70,13 @@ protected function setUp() */ public function testReadNoFiles() { - $this->_fileResolverMock->expects( - $this->at(0) - )->method( - 'get' - )->with( - 'config.xml', - 'global' - )->will( - $this->returnValue([]) - ); + $this->createModelAndVerifyConstructor(); + $this->fileResolverMock->expects($this->at(0)) + ->method('get') + ->with('config.xml', 'global') + ->will($this->returnValue([])); - $this->assertEquals([], $this->_model->read()); + $this->assertEquals([], $this->model->read()); } /** @@ -89,38 +84,24 @@ public function testReadNoFiles() */ public function testReadValidConfig() { + $this->createModelAndVerifyConstructor(); $testXmlFilesList = [ - file_get_contents($this->_filePath . 'initial_config1.xml'), - file_get_contents($this->_filePath . 'initial_config2.xml'), + file_get_contents($this->filePath . 'initial_config1.xml'), + file_get_contents($this->filePath . 'initial_config2.xml'), ]; $expectedConfig = ['data' => [], 'metadata' => []]; - $this->_fileResolverMock->expects( - $this->at(0) - )->method( - 'get' - )->with( - 'config.xml', - 'global' - )->will( - $this->returnValue($testXmlFilesList) - ); - - $this->_converterMock->expects( - $this->once() - )->method( - 'convert' - )->with( - $this->anything() - )->will( - $this->returnValue($expectedConfig) - ); - - $this->rootDirectory->expects($this->any())->method('getRelativePath')->will($this->returnArgument(0)); + $this->fileResolverMock->expects($this->at(0)) + ->method('get') + ->with('config.xml', 'global') + ->will($this->returnValue($testXmlFilesList)); - $this->rootDirectory->expects($this->any())->method('readFile')->will($this->returnValue('')); + $this->converterMock->expects($this->once()) + ->method('convert') + ->with($this->anything()) + ->will($this->returnValue($expectedConfig)); - $this->assertEquals($expectedConfig, $this->_model->read()); + $this->assertEquals($expectedConfig, $this->model->read()); } /** @@ -130,37 +111,40 @@ public function testReadValidConfig() */ public function testReadInvalidConfig() { + $this->createModelAndVerifyConstructor(); $testXmlFilesList = [ - file_get_contents($this->_filePath . 'invalid_config.xml'), - file_get_contents($this->_filePath . 'initial_config2.xml'), + file_get_contents($this->filePath . 'invalid_config.xml'), + file_get_contents($this->filePath . 'initial_config2.xml'), ]; $expectedConfig = ['data' => [], 'metadata' => []]; - $this->_fileResolverMock->expects( - $this->at(0) - )->method( - 'get' - )->with( - 'config.xml', - 'global' - )->will( - $this->returnValue($testXmlFilesList) - ); - - $this->_converterMock->expects( - $this->never() - )->method( - 'convert' - )->with( - $this->anything() - )->will( - $this->returnValue($expectedConfig) - ); + $this->fileResolverMock->expects($this->at(0)) + ->method('get') + ->with('config.xml', 'global') + ->will($this->returnValue($testXmlFilesList)); - $this->rootDirectory->expects($this->any())->method('getRelativePath')->will($this->returnArgument(0)); + $this->converterMock->expects($this->never()) + ->method('convert') + ->with($this->anything()) + ->will($this->returnValue($expectedConfig)); - $this->rootDirectory->expects($this->any())->method('readFile')->will($this->returnValue('')); + $this->model->read(); + } - $this->assertEquals($expectedConfig, $this->_model->read()); + private function createModelAndVerifyConstructor() + { + $this->validationStateMock->expects($this->once())->method('isValidated')->will($this->returnValue(true)); + $schemaFile = $this->filePath . 'config.xsd'; + $this->schemaLocatorMock->expects($this->once())->method('getSchema')->will($this->returnValue($schemaFile)); + + $this->model = $this->objectManager->getObject( + 'Magento\Framework\App\Config\Initial\Reader', + [ + 'fileResolver' => $this->fileResolverMock, + 'converter' => $this->converterMock, + 'schemaLocator' => $this->schemaLocatorMock, + 'validationState' => $this->validationStateMock + ] + ); } } diff --git a/dev/tests/unit/testsuite/Magento/Framework/App/Config/Initial/SchemaLocatorTest.php b/dev/tests/unit/testsuite/Magento/Framework/App/Config/Initial/SchemaLocatorTest.php index f98959489915c..36ed85447054c 100644 --- a/dev/tests/unit/testsuite/Magento/Framework/App/Config/Initial/SchemaLocatorTest.php +++ b/dev/tests/unit/testsuite/Magento/Framework/App/Config/Initial/SchemaLocatorTest.php @@ -26,16 +26,10 @@ protected function setUp() { $this->objectManager = new \Magento\TestFramework\Helper\ObjectManager($this); $this->_moduleReaderMock = $this->getMock('Magento\Framework\Module\Dir\Reader', [], [], '', false); - $this->_moduleReaderMock->expects( - $this->once() - )->method( - 'getModuleDir' - )->with( - 'etc', - 'moduleName' - )->will( - $this->returnValue('schema_dir') - ); + $this->_moduleReaderMock->expects($this->once()) + ->method('getModuleDir') + ->with('etc', 'moduleName') + ->will($this->returnValue('schema_dir')); $this->_model = $this->objectManager->getObject( 'Magento\Framework\App\Config\Initial\SchemaLocator', [ diff --git a/dev/tests/unit/testsuite/Magento/Framework/App/Config/Storage/WriterTest.php b/dev/tests/unit/testsuite/Magento/Framework/App/Config/Storage/WriterTest.php index 0b0805cb87f34..21a24fcfafafd 100644 --- a/dev/tests/unit/testsuite/Magento/Framework/App/Config/Storage/WriterTest.php +++ b/dev/tests/unit/testsuite/Magento/Framework/App/Config/Storage/WriterTest.php @@ -44,6 +44,16 @@ public function testDelete() $this->model->delete('path'); } + public function testDeleteOptions() + { + $scope = 'scope'; + $scopeId = '1'; + $this->resource->expects($this->once()) + ->method('deleteConfig') + ->with('path', $scope, $scopeId); + $this->model->delete('path', $scope, $scopeId); + } + public function testSave() { $this->resource->expects($this->once()) diff --git a/dev/tests/unit/testsuite/Magento/Framework/App/Config/ValueFactoryTest.php b/dev/tests/unit/testsuite/Magento/Framework/App/Config/ValueFactoryTest.php index 5501373422048..8ccad2ea3d6b6 100644 --- a/dev/tests/unit/testsuite/Magento/Framework/App/Config/ValueFactoryTest.php +++ b/dev/tests/unit/testsuite/Magento/Framework/App/Config/ValueFactoryTest.php @@ -5,44 +5,13 @@ */ namespace Magento\Framework\App\Config; -class ValueFactoryTest extends \PHPUnit_Framework_TestCase +class ValueFactoryTest extends \Magento\Test\AbstractFactoryTestCase { - /** - * @var \Magento\TestFramework\Helper\ObjectManager - */ - protected $objectManager; - - /** - * @var \Magento\Framework\App\Config\ValueFactory - */ - protected $model; - - /** - * @var \Magento\Framework\ObjectManagerInterface | \PHPUnit_Framework_MockObject_MockObject - */ - protected $objectManagerMock; - protected function setUp() { - $this->objectManager = new \Magento\TestFramework\Helper\ObjectManager($this); - $this->objectManagerMock = $this->getMockBuilder('Magento\Framework\ObjectManagerInterface') - ->disableOriginalConstructor() - ->getMock(); - $this->model = $this->objectManager->getObject( - 'Magento\Framework\App\Config\ValueFactory', - ['objectManager' => $this->objectManagerMock] - ); - } - - public function testCreate() - { - $instanceMock = $this->getMockBuilder('Magento\Framework\App\Config\ValueInterface') - ->disableOriginalConstructor() - ->getMock(); - $this->objectManagerMock->expects($this->once()) - ->method('create') - ->will($this->returnValue($instanceMock)); - $this->assertSame($instanceMock, $this->model->create()); + $this->instanceClassName = 'Magento\Framework\App\Config\ValueInterface'; + $this->factoryClassName = 'Magento\Framework\App\Config\ValueFactory'; + parent::setUp(); } /** @@ -53,6 +22,6 @@ public function testCreateWithException() $this->objectManagerMock->expects($this->once()) ->method('create') ->will($this->returnValue('somethingElse')); - $this->model->create(); + $this->factory->create(); } } From 0e60b08eeea61d5f8a3cd3129d9b787f02743836 Mon Sep 17 00:00:00 2001 From: Joan He Date: Thu, 22 Jan 2015 15:52:43 -0600 Subject: [PATCH 12/15] MAGETWO-32901: Added unit tests to lib/internal/Magento/Framework/App/Response/Http/FileFactory.php --- .../App/Response/Http/FileFactoryTest.php | 132 +++++++++--------- .../App/Response/Http/FileFactory.php | 13 +- .../Response/Http/TestingPhpExitException.php | 15 -- 3 files changed, 72 insertions(+), 88 deletions(-) delete mode 100644 lib/internal/Magento/Framework/App/Response/Http/TestingPhpExitException.php diff --git a/dev/tests/unit/testsuite/Magento/Framework/App/Response/Http/FileFactoryTest.php b/dev/tests/unit/testsuite/Magento/Framework/App/Response/Http/FileFactoryTest.php index a03f0747f02c5..f3ebe63993dd0 100644 --- a/dev/tests/unit/testsuite/Magento/Framework/App/Response/Http/FileFactoryTest.php +++ b/dev/tests/unit/testsuite/Magento/Framework/App/Response/Http/FileFactoryTest.php @@ -12,69 +12,57 @@ class FileFactoryTest extends \PHPUnit_Framework_TestCase */ protected $objectManager; - /** - * @var \Magento\Framework\App\Response\Http\FileFactory - */ - protected $_model; - /** * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Filesystem */ - protected $_fileSystemMock; + protected $fileSystemMock; /** * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\App\Response\Http */ - protected $_responseMock; + protected $responseMock; /** * @var \Magento\Framework\Filesystem\Directory\WriteInterface | \PHPUnit_Framework_MockObject_MockObject */ - protected $_dirMock; + protected $dirMock; protected function setUp() { $this->objectManager = new \Magento\TestFramework\Helper\ObjectManager($this); - $this->_fileSystemMock = $this->getMock( + $this->fileSystemMock = $this->getMock( 'Magento\Framework\Filesystem', ['getDirectoryWrite'], [], '', false ); - $this->_dirMock = $this->getMockBuilder( + $this->dirMock = $this->getMockBuilder( '\Magento\Framework\Filesystem\Directory\Write' )->disableOriginalConstructor()->getMock(); - $this->_fileSystemMock->expects( + $this->fileSystemMock->expects( $this->any() )->method( 'getDirectoryWrite' )->withAnyParameters()->will( - $this->returnValue($this->_dirMock) + $this->returnValue($this->dirMock) ); - $this->_fileSystemMock->expects( + $this->fileSystemMock->expects( $this->any() )->method( 'isFile' )->withAnyParameters()->will( $this->returnValue(0) ); - $this->_responseMock = $this->getMock( + $this->responseMock = $this->getMock( 'Magento\Framework\App\Response\Http', ['setHeader', 'sendHeaders', 'setHttpResponseCode', 'clearBody', 'setBody', '__wakeup'], [], '', false ); - $this->_model = $this->objectManager->getObject( - 'Magento\Framework\App\Response\Http\FileFactory', - [ - 'response' => $this->_responseMock, - 'filesystem' => $this->_fileSystemMock, - ] - ); } /** @@ -82,7 +70,7 @@ protected function setUp() */ public function testCreateIfContentDoesntHaveRequiredKeys() { - $this->_model->create('fileName', []); + $this->getModel()->create('fileName', []); } /** @@ -94,57 +82,51 @@ public function testCreateIfFileNotExist() $file = 'some_file'; $content = ['type' => 'filename', 'value' => $file]; - $this->_responseMock->expects( + $this->responseMock->expects( $this->never() )->method( 'setHeader' )->will( $this->returnSelf() ); - $this->_responseMock->expects( + $this->responseMock->expects( $this->never() )->method( 'setHttpResponseCode' )->will( $this->returnSelf() ); - $this->_model->create('fileName', $content); + $this->getModel()->create('fileName', $content); } - /** - * @expectedException \Magento\Framework\App\Response\Http\TestingPhpExitException - */ public function testCreateArrayContent() { - if (!defined('UNIT_TESTING')) { - define('UNIT_TESTING', 1); - } $file = 'some_file'; $content = ['type' => 'filename', 'value' => $file]; - $this->_dirMock->expects($this->once()) + $this->dirMock->expects($this->once()) ->method('isFile') ->will($this->returnValue(true)); - $this->_dirMock->expects($this->once()) + $this->dirMock->expects($this->once()) ->method('stat') ->will($this->returnValue(['size' => 100])); - $this->_responseMock->expects($this->exactly(6)) + $this->responseMock->expects($this->exactly(6)) ->method('setHeader') ->will($this->returnSelf()); - $this->_responseMock->expects($this->once()) + $this->responseMock->expects($this->once()) ->method('setHttpResponseCode') ->with(200) ->will($this->returnSelf()); - $this->_responseMock->expects($this->once()) + $this->responseMock->expects($this->once()) ->method('sendHeaders') ->will($this->returnSelf()); $streamMock = $this->getMockBuilder('Magento\Framework\Filesystem\File\WriteInterface') ->disableOriginalConstructor()->getMock(); - $this->_dirMock->expects($this->once()) + $this->dirMock->expects($this->once()) ->method('openFile') ->will($this->returnValue($streamMock)); - $this->_dirMock->expects($this->never()) + $this->dirMock->expects($this->never()) ->method('delete') ->will($this->returnValue($streamMock)); $streamMock->expects($this->at(1)) @@ -157,43 +139,37 @@ public function testCreateArrayContent() ->method('read'); $streamMock->expects($this->once()) ->method('close'); - $this->_model->create('fileName', $content); + $this->getModelMock()->create('fileName', $content); } - /** - * @expectedException \Magento\Framework\App\Response\Http\TestingPhpExitException - */ public function testCreateArrayContentRm() { - if (!defined('UNIT_TESTING')) { - define('UNIT_TESTING', 1); - } $file = 'some_file'; $content = ['type' => 'filename', 'value' => $file, 'rm' => 1]; - $this->_dirMock->expects($this->once()) + $this->dirMock->expects($this->once()) ->method('isFile') ->will($this->returnValue(true)); - $this->_dirMock->expects($this->once()) + $this->dirMock->expects($this->once()) ->method('stat') ->will($this->returnValue(['size' => 100])); - $this->_responseMock->expects($this->exactly(6)) + $this->responseMock->expects($this->exactly(6)) ->method('setHeader') ->will($this->returnSelf()); - $this->_responseMock->expects($this->once()) + $this->responseMock->expects($this->once()) ->method('setHttpResponseCode') ->with(200) ->will($this->returnSelf()); - $this->_responseMock->expects($this->once()) + $this->responseMock->expects($this->once()) ->method('sendHeaders') ->will($this->returnSelf()); $streamMock = $this->getMockBuilder('Magento\Framework\Filesystem\File\WriteInterface') ->disableOriginalConstructor()->getMock(); - $this->_dirMock->expects($this->once()) + $this->dirMock->expects($this->once()) ->method('openFile') ->will($this->returnValue($streamMock)); - $this->_dirMock->expects($this->once()) + $this->dirMock->expects($this->once()) ->method('delete') ->will($this->returnValue($streamMock)); $streamMock->expects($this->at(1)) @@ -206,40 +182,40 @@ public function testCreateArrayContentRm() ->method('read'); $streamMock->expects($this->once()) ->method('close'); - $this->_model->create('fileName', $content); + $this->getModelMock()->create('fileName', $content); } public function testCreateStringContent() { - $this->_dirMock->expects($this->never()) + $this->dirMock->expects($this->never()) ->method('isFile') ->will($this->returnValue(true)); - $this->_dirMock->expects($this->never()) + $this->dirMock->expects($this->never()) ->method('stat') ->will($this->returnValue(['size' => 100])); - $this->_responseMock->expects($this->exactly(6)) + $this->responseMock->expects($this->exactly(6)) ->method('setHeader') ->will($this->returnSelf()); - $this->_responseMock->expects($this->once()) + $this->responseMock->expects($this->once()) ->method('setHttpResponseCode') ->with(200) ->will($this->returnSelf()); - $this->_responseMock->expects($this->once()) + $this->responseMock->expects($this->once()) ->method('clearBody') ->will($this->returnSelf()); - $this->_responseMock->expects($this->once()) + $this->responseMock->expects($this->once()) ->method('setBody') ->will($this->returnSelf()); - $this->_responseMock->expects($this->never()) + $this->responseMock->expects($this->never()) ->method('sendHeaders') ->will($this->returnSelf()); $streamMock = $this->getMockBuilder('Magento\Framework\Filesystem\File\WriteInterface') ->disableOriginalConstructor()->getMock(); - $this->_dirMock->expects($this->never()) + $this->dirMock->expects($this->never()) ->method('openFile') ->will($this->returnValue($streamMock)); - $this->_dirMock->expects($this->never()) + $this->dirMock->expects($this->never()) ->method('delete') ->will($this->returnValue($streamMock)); $streamMock->expects($this->never()) @@ -249,6 +225,36 @@ public function testCreateStringContent() ->method('read'); $streamMock->expects($this->never()) ->method('close'); - $this->assertSame($this->_responseMock, $this->_model->create('fileName', 'content')); + $this->assertSame($this->responseMock, $this->getModel()->create('fileName', 'content')); + } + + /** + * @return \Magento\Framework\App\Response\Http\FileFactory + */ + private function getModel() + { + return $this->objectManager->getObject( + 'Magento\Framework\App\Response\Http\FileFactory', + [ + 'response' => $this->responseMock, + 'filesystem' => $this->fileSystemMock, + ] + ); + } + + /** + * @return \Magento\Framework\App\Response\Http\FileFactory | \PHPUnit_Framework_MockObject_MockBuilder + */ + private function getModelMock() + { + $modelMock = $this->getMock( + 'Magento\Framework\App\Response\Http\FileFactory', + ['callExit'], + [ + 'response' => $this->responseMock, + 'filesystem' => $this->fileSystemMock, + ] + ); + return $modelMock; } } diff --git a/lib/internal/Magento/Framework/App/Response/Http/FileFactory.php b/lib/internal/Magento/Framework/App/Response/Http/FileFactory.php index 4e5a1c77d0d74..3f90403f8d0af 100644 --- a/lib/internal/Magento/Framework/App/Response/Http/FileFactory.php +++ b/lib/internal/Magento/Framework/App/Response/Http/FileFactory.php @@ -113,7 +113,7 @@ public function create( if (!empty($content['rm'])) { $dir->delete($file); } - self::exitphp(0); + $this->callExit(); } else { $this->_response->clearBody(); $this->_response->setBody($content); @@ -124,16 +124,9 @@ public function create( /** * Call exit - * - * @param $code - * @throws TestingPhpExitException */ - static function exitphp($code) + protected function callExit() { - if (defined('UNIT_TESTING')) { - throw new \Magento\Framework\App\Response\Http\TestingPhpExitException(); - } else { - exit($code); - } + exit(0); } } diff --git a/lib/internal/Magento/Framework/App/Response/Http/TestingPhpExitException.php b/lib/internal/Magento/Framework/App/Response/Http/TestingPhpExitException.php deleted file mode 100644 index 3f486c4afae6b..0000000000000 --- a/lib/internal/Magento/Framework/App/Response/Http/TestingPhpExitException.php +++ /dev/null @@ -1,15 +0,0 @@ - Date: Thu, 22 Jan 2015 16:19:34 -0600 Subject: [PATCH 13/15] MAGETWO-32901: Added unit tests to lib/internal/Magento/Framework/App/Response/Http/FileFactory.php --- .../Magento/Framework/App/Response/Http/FileFactoryTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/dev/tests/unit/testsuite/Magento/Framework/App/Response/Http/FileFactoryTest.php b/dev/tests/unit/testsuite/Magento/Framework/App/Response/Http/FileFactoryTest.php index f3ebe63993dd0..d037063ed3a7b 100644 --- a/dev/tests/unit/testsuite/Magento/Framework/App/Response/Http/FileFactoryTest.php +++ b/dev/tests/unit/testsuite/Magento/Framework/App/Response/Http/FileFactoryTest.php @@ -229,6 +229,8 @@ public function testCreateStringContent() } /** + * Get model + * * @return \Magento\Framework\App\Response\Http\FileFactory */ private function getModel() @@ -243,6 +245,8 @@ private function getModel() } /** + * Get model mock + * * @return \Magento\Framework\App\Response\Http\FileFactory | \PHPUnit_Framework_MockObject_MockBuilder */ private function getModelMock() From 4f8d4ede3eed178af2e29778625a82057327aa5d Mon Sep 17 00:00:00 2001 From: Joan He Date: Fri, 23 Jan 2015 11:08:44 -0600 Subject: [PATCH 14/15] MAGETWO-32913: added unit tests for classes under lib/internal/Magento/Framework/App/Router --- .../Magento/Framework/App/Router/ActionListTest.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/dev/tests/unit/testsuite/Magento/Framework/App/Router/ActionListTest.php b/dev/tests/unit/testsuite/Magento/Framework/App/Router/ActionListTest.php index aacf7f3944dca..c31e5e2ac680a 100644 --- a/dev/tests/unit/testsuite/Magento/Framework/App/Router/ActionListTest.php +++ b/dev/tests/unit/testsuite/Magento/Framework/App/Router/ActionListTest.php @@ -109,11 +109,12 @@ public function testGet($module, $area, $namespace, $action, $data, $expected) public function getDataProvider() { + $mockClassName = 'Mock_Action_Class'; $actionClass = $this->getMockClass( 'Magento\Framework\App\ActionInterface', ['dispatch', 'getResponse'], [], - 'Magento_Module_Controller_Area_Namespace_Index' + $mockClassName ); return [ @@ -122,7 +123,7 @@ public function getDataProvider() 'Area', 'Namespace', 'Index', - ['magento\module\controller\area\namespace\index' => 'Magento_Module_Controller_Area_Namespace_Index'], + ['magento\module\controller\area\namespace\index' => $mockClassName], $actionClass ], [ @@ -130,7 +131,7 @@ public function getDataProvider() '', 'Namespace', 'Index', - ['magento\module\controller\namespace\index' => 'Magento_Module_Controller_Area_Namespace_Index'], + ['magento\module\controller\namespace\index' => $mockClassName], $actionClass ], [ @@ -138,7 +139,7 @@ public function getDataProvider() 'Area', 'Namespace', 'Catch', - ['magento\module\controller\area\namespace\catchaction' => 'Magento_Module_Controller_Area_Namespace_Index'], + ['magento\module\controller\area\namespace\catchaction' => $mockClassName], $actionClass ], [ @@ -146,7 +147,7 @@ public function getDataProvider() 'Area', 'Namespace', 'Index', - ['magento\module\controller\area\namespace\index' => 'Magento_Module_Controller_Area_Namespace_Index2'], + ['magento\module\controller\area\namespace\index' => 'Not_Exist_Class'], null ], [ From 0c97598113d04d7015d636f18ec091025f6afc4d Mon Sep 17 00:00:00 2001 From: Joan He Date: Mon, 26 Jan 2015 15:41:47 -0600 Subject: [PATCH 15/15] MAGETWO-31938: added unit tests --- dev/tests/unit/testsuite/Magento/Framework/App/AreaTest.php | 3 +++ .../Magento/Framework/App/Config/Data/ProcessorFactoryTest.php | 2 +- .../Magento/Framework/App/Response/Http/FileFactory.php | 3 +++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/dev/tests/unit/testsuite/Magento/Framework/App/AreaTest.php b/dev/tests/unit/testsuite/Magento/Framework/App/AreaTest.php index 864f5e6c07cbf..bf2f452a45a18 100644 --- a/dev/tests/unit/testsuite/Magento/Framework/App/AreaTest.php +++ b/dev/tests/unit/testsuite/Magento/Framework/App/AreaTest.php @@ -6,6 +6,9 @@ namespace Magento\Framework\App; +/** + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + */ class AreaTest extends \PHPUnit_Framework_TestCase { const SCOPE_ID = '1'; diff --git a/dev/tests/unit/testsuite/Magento/Framework/App/Config/Data/ProcessorFactoryTest.php b/dev/tests/unit/testsuite/Magento/Framework/App/Config/Data/ProcessorFactoryTest.php index eacb9eac5fd75..807f2cf9e59ca 100644 --- a/dev/tests/unit/testsuite/Magento/Framework/App/Config/Data/ProcessorFactoryTest.php +++ b/dev/tests/unit/testsuite/Magento/Framework/App/Config/Data/ProcessorFactoryTest.php @@ -53,7 +53,7 @@ public function testGetModelWithCorrectInterface() /** * @covers \Magento\Framework\App\Config\Data\ProcessorFactory::get * @expectedException \InvalidArgumentException - * @expectedExceptionMessage Magento\Framework\App\Config\Data\WrongBackendModel is not instance of \Magento\Framework\App\Config\Data\ProcessorInterface + * @expectedExceptionMessageRegExp /\w+\\WrongBackendModel is not instance of \w+\\ProcessorInterface/ */ public function testGetModelWithWrongInterface() { diff --git a/lib/internal/Magento/Framework/App/Response/Http/FileFactory.php b/lib/internal/Magento/Framework/App/Response/Http/FileFactory.php index 3f90403f8d0af..539448a1baa35 100644 --- a/lib/internal/Magento/Framework/App/Response/Http/FileFactory.php +++ b/lib/internal/Magento/Framework/App/Response/Http/FileFactory.php @@ -124,6 +124,9 @@ public function create( /** * Call exit + * + * @return void + * @SuppressWarnings(PHPMD.ExitExpression) */ protected function callExit() {