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/AreaTest.php b/dev/tests/unit/testsuite/Magento/Framework/App/AreaTest.php new file mode 100644 index 0000000000000..bf2f452a45a18 --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/Framework/App/AreaTest.php @@ -0,0 +1,316 @@ +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(self::SCOPE_ID)); + $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->verifyLoadConfig(); + $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->verifyLoadConfig(); + $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 verifyLoadConfig() + { + $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(self::SCOPE_ID) + ->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(self::SCOPE_ID) + ->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(self::SCOPE_ID) + ->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); + } +} 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..f6448f3f96ddd --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/Framework/App/Config/BaseFactoryTest.php @@ -0,0 +1,16 @@ +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/Data/BackendModelPoolTest.php b/dev/tests/unit/testsuite/Magento/Framework/App/Config/Data/ProcessorFactoryTest.php similarity index 93% 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..807f2cf9e59ca 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 + * @expectedExceptionMessageRegExp /\w+\\WrongBackendModel is not instance of \w+\\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..3f578938d546f --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/Framework/App/Config/DataFactoryTest.php @@ -0,0 +1,16 @@ +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 5193cf725cb9d..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\Config\ValidationStateInterface | \PHPUnit_Framework_MockObject_MockObject + */ + protected $validationStateMock; /** - * @var \Magento\Framework\Filesystem\Directory\Read|\PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\App\Config\Initial\SchemaLocator | \PHPUnit_Framework_MockObject_MockObject */ - protected $rootDirectory; + 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,37 +84,67 @@ 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->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->converterMock->expects($this->once()) + ->method('convert') + ->with($this->anything()) + ->will($this->returnValue($expectedConfig)); + + $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() + { + $this->createModelAndVerifyConstructor(); + $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->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(); + } + + 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->assertEquals($expectedConfig, $this->_model->read()); + $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 new file mode 100644 index 0000000000000..36ed85447054c --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/Framework/App/Config/Initial/SchemaLocatorTest.php @@ -0,0 +1,51 @@ +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..21a24fcfafafd --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/Framework/App/Config/Storage/WriterTest.php @@ -0,0 +1,64 @@ +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 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()) + ->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..8ccad2ea3d6b6 --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/Framework/App/Config/ValueFactoryTest.php @@ -0,0 +1,27 @@ +instanceClassName = 'Magento\Framework\App\Config\ValueInterface'; + $this->factoryClassName = 'Magento\Framework\App\Config\ValueFactory'; + parent::setUp(); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testCreateWithException() + { + $this->objectManagerMock->expects($this->once()) + ->method('create') + ->will($this->returnValue('somethingElse')); + $this->factory->create(); + } +} 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..6cf82d0261205 --- /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('key')); + } + + public function testSetGetValue() + { + $this->object->setValue('key', 'value', 'default'); + $this->assertEquals('value', $this->object->getValue('key')); + } + + public function testSetUnsetGetValue() + { + $this->object->setValue('key', 'value', 'default'); + $this->object->unsValue('key'); + $this->assertEquals('default', $this->object->getValue('key')); + } + + public function testGetData() + { + $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()); + } +} 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..74105e5c2d303 --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/Framework/App/Resource/ConnectionFactoryTest.php @@ -0,0 +1,91 @@ +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->objectManagerMock->expects($this->never()) + ->method('get'); + $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(); + $connectionAdapterMock = $this->getMockBuilder('Magento\Framework\App\Resource\ConnectionAdapterInterface') + ->disableOriginalConstructor() + ->getMock(); + $adapterInstanceMock = $this->getMockBuilder('Magento\Framework\DB\Adapter\AdapterInterface') + ->disableOriginalConstructor() + ->getMock(); + $adapterInstanceMock->expects($this->once()) + ->method('setCacheAdapter') + ->with($cacheAdapterMock) + ->willReturnSelf(); + $connectionAdapterMock->expects($this->once()) + ->method('getConnection') + ->with($loggerMock) + ->will($this->returnValue($adapterInstanceMock)); + $this->objectManagerMock->expects($this->once()) + ->method('create') + ->with('Magento\Framework\App\Resource\ConnectionAdapterInterface') + ->will($this->returnValue($connectionAdapterMock)); + $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/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..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 @@ -8,71 +8,61 @@ class FileFactoryTest extends \PHPUnit_Framework_TestCase { /** - * @var \Magento\Framework\App\Response\Http\FileFactory + * @var \Magento\TestFramework\Helper\ObjectManager */ - protected $_model; + protected $objectManager; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Filesystem */ - protected $_fileSystemMock; + protected $fileSystemMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\App\Response\Http */ - protected $_responseMock; + protected $responseMock; /** - * @var \Magento\Framework\Filesystem\Directory\WriteInterface + * @var \Magento\Framework\Filesystem\Directory\WriteInterface | \PHPUnit_Framework_MockObject_MockObject */ - protected $_dirMock; + protected $dirMock; protected function setUp() { - $this->_fileSystemMock = $this->getMock( + $this->objectManager = new \Magento\TestFramework\Helper\ObjectManager($this); + $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', '__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 - ); } /** @@ -80,7 +70,7 @@ protected function setUp() */ public function testCreateIfContentDoesntHaveRequiredKeys() { - $this->_model->create('fileName', []); + $this->getModel()->create('fileName', []); } /** @@ -92,6 +82,183 @@ public function testCreateIfFileNotExist() $file = 'some_file'; $content = ['type' => 'filename', 'value' => $file]; - $this->_model->create('fileName', $content); + $this->responseMock->expects( + $this->never() + )->method( + 'setHeader' + )->will( + $this->returnSelf() + ); + $this->responseMock->expects( + $this->never() + )->method( + 'setHttpResponseCode' + )->will( + $this->returnSelf() + ); + $this->getModel()->create('fileName', $content); + } + + public function testCreateArrayContent() + { + $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->getModelMock()->create('fileName', $content); + } + + public function testCreateArrayContentRm() + { + $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->getModelMock()->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->getModel()->create('fileName', 'content')); + } + + /** + * Get model + * + * @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, + ] + ); + } + + /** + * Get model mock + * + * @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/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..c31e5e2ac680a --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/Framework/App/Router/ActionListTest.php @@ -0,0 +1,163 @@ +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() + { + $mockClassName = 'Mock_Action_Class'; + $actionClass = $this->getMockClass( + 'Magento\Framework\App\ActionInterface', + ['dispatch', 'getResponse'], + [], + $mockClassName + ); + + return [ + [ + 'Magento_Module', + 'Area', + 'Namespace', + 'Index', + ['magento\module\controller\area\namespace\index' => $mockClassName], + $actionClass + ], + [ + 'Magento_Module', + '', + 'Namespace', + 'Index', + ['magento\module\controller\namespace\index' => $mockClassName], + $actionClass + ], + [ + 'Magento_Module', + 'Area', + 'Namespace', + 'Catch', + ['magento\module\controller\area\namespace\catchaction' => $mockClassName], + $actionClass + ], + [ + 'Magento_Module', + 'Area', + 'Namespace', + 'Index', + ['magento\module\controller\area\namespace\index' => 'Not_Exist_Class'], + null + ], + [ + 'Magento_Module', + 'Area', + 'Namespace', + 'Index', + [], + null + ], + ]; + } +} 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; 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; } } diff --git a/lib/internal/Magento/Framework/App/Response/Http/FileFactory.php b/lib/internal/Magento/Framework/App/Response/Http/FileFactory.php index 4ceab8ac5a1a9..539448a1baa35 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); + $this->callExit(); } else { $this->_response->clearBody(); $this->_response->setBody($content); @@ -121,4 +121,15 @@ public function create( } return $this->_response; } + + /** + * Call exit + * + * @return void + * @SuppressWarnings(PHPMD.ExitExpression) + */ + protected function callExit() + { + exit(0); + } }