diff --git a/app/code/Magento/Backend/Model/Menu.php b/app/code/Magento/Backend/Model/Menu.php index 953e540b2cfc64b5d76e1308ac6587155e947ec2..15432dd71edaf775be21b2501bcff17911ef697b 100644 --- a/app/code/Magento/Backend/Model/Menu.php +++ b/app/code/Magento/Backend/Model/Menu.php @@ -39,10 +39,12 @@ class Menu extends \ArrayObject private $serializer; /** + * Menu constructor + * * @param LoggerInterface $logger * @param string $pathInMenuStructure - * @param Factory $menuItemFactory - * @param SerializerInterface $serializer + * @param Factory|null $menuItemFactory + * @param SerializerInterface|null $serializer */ public function __construct( LoggerInterface $logger, @@ -258,17 +260,13 @@ class Menu extends \ArrayObject } /** - * Hack to unset logger instance which cannot be serialized + * Serialize menu * * @return string */ public function serialize() { - $logger = $this->_logger; - unset($this->_logger); - $result = $this->serializer->serialize($this->toArray()); - $this->_logger = $logger; - return $result; + return $this->serializer->serialize($this->toArray()); } /** diff --git a/app/code/Magento/Backend/Test/Unit/Model/Menu/Filter/IteratorTest.php b/app/code/Magento/Backend/Test/Unit/Model/Menu/Filter/IteratorTest.php index fa5573a26133938d00048b37def1c7f9b9890e36..06dee1f01fd88439ad1a1bcf1f959e19eb13e9be 100644 --- a/app/code/Magento/Backend/Test/Unit/Model/Menu/Filter/IteratorTest.php +++ b/app/code/Magento/Backend/Test/Unit/Model/Menu/Filter/IteratorTest.php @@ -12,12 +12,12 @@ class IteratorTest extends \PHPUnit_Framework_TestCase /** * @var \Magento\Backend\Model\Menu */ - protected $menuModel; + private $menuModel; /** * @var \Magento\Backend\Model\Menu\Item[] */ - protected $items = []; + private $items = []; protected function setUp() { diff --git a/app/code/Magento/Backend/Test/Unit/Model/Menu/ItemTest.php b/app/code/Magento/Backend/Test/Unit/Model/Menu/ItemTest.php index 4cd491b9b34fd86ad0107c981f65feb3d1e0ec01..f8b26ce7aae2a6e80a273de1f96acba5e3d86a6a 100644 --- a/app/code/Magento/Backend/Test/Unit/Model/Menu/ItemTest.php +++ b/app/code/Magento/Backend/Test/Unit/Model/Menu/ItemTest.php @@ -226,7 +226,7 @@ class ItemTest extends \PHPUnit_Framework_TestCase * @param array $expected * @dataProvider toArrayDataProvider */ - public function testToArray($data, $expected) + public function testToArray(array $data, array $expected) { $menuMock = $this->getMock(\Magento\Backend\Model\Menu::class, [], [], '', false); $this->_menuFactoryMock->method('create')->will($this->returnValue($menuMock)); @@ -251,8 +251,16 @@ class ItemTest extends \PHPUnit_Framework_TestCase public function toArrayDataProvider() { return [ - [ - $this->_params, + 'No submenu' => [ + [ + 'id' => 'item', + 'title' => 'Item Title', + 'action' => '/system/config', + 'resource' => 'Magento_Config::config', + 'depends_on_module' => 'Magento_Backend', + 'depends_on_config' => 'system/config/isEnabled', + 'tooltip' => 'Item tooltip', + ], [ 'parent_id' => null, 'module_name' => 'Magento_Backend', @@ -268,7 +276,7 @@ class ItemTest extends \PHPUnit_Framework_TestCase 'sub_menu' => null ] ], - [ + 'with submenu' => [ [ 'parent_id' => '1', 'module_name' => 'Magento_Module1', @@ -281,7 +289,15 @@ class ItemTest extends \PHPUnit_Framework_TestCase 'depends_on_module' => null, 'tooltip' => null, 'title' => null, - 'sub_menu' => $this->_params, + 'sub_menu' => [ + 'id' => 'item', + 'title' => 'Item Title', + 'action' => '/system/config', + 'resource' => 'Magento_Config::config', + 'depends_on_module' => 'Magento_Backend', + 'depends_on_config' => 'system/config/isEnabled', + 'tooltip' => 'Item tooltip', + ], ], [ 'parent_id' => '1', @@ -295,15 +311,31 @@ class ItemTest extends \PHPUnit_Framework_TestCase 'depends_on_module' => null, 'tooltip' => '', 'title' => null, - 'sub_menu' => $this->_params + 'sub_menu' => [ + 'id' => 'item', + 'title' => 'Item Title', + 'action' => '/system/config', + 'resource' => 'Magento_Config::config', + 'depends_on_module' => 'Magento_Backend', + 'depends_on_config' => 'system/config/isEnabled', + 'tooltip' => 'Item tooltip', + ] ] ], - [ + 'small set of data' => [ [ 'parent_id' => '1', 'module_name' => 'Magento_Module1', 'sort_index' => '50', - 'sub_menu' => $this->_params, + 'sub_menu' => [ + 'id' => 'item', + 'title' => 'Item Title', + 'action' => '/system/config', + 'resource' => 'Magento_Config::config', + 'depends_on_module' => 'Magento_Backend', + 'depends_on_config' => 'system/config/isEnabled', + 'tooltip' => 'Item tooltip', + ], ], [ 'parent_id' => '1', @@ -317,7 +349,15 @@ class ItemTest extends \PHPUnit_Framework_TestCase 'depends_on_module' => null, 'tooltip' => '', 'title' => null, - 'sub_menu' => $this->_params + 'sub_menu' => [ + 'id' => 'item', + 'title' => 'Item Title', + 'action' => '/system/config', + 'resource' => 'Magento_Config::config', + 'depends_on_module' => 'Magento_Backend', + 'depends_on_config' => 'system/config/isEnabled', + 'tooltip' => 'Item tooltip', + ] ] ] ]; @@ -326,10 +366,14 @@ class ItemTest extends \PHPUnit_Framework_TestCase /** * @param array $constructorData * @param array $populateFromData + * @param array $expected * @dataProvider populateFromArrayDataProvider */ - public function testPopulateFromArray($constructorData, $populateFromData, $expected) - { + public function testPopulateFromArray( + array $constructorData, + array $populateFromData, + array $expected + ) { $menuMock = $this->getMock(\Magento\Backend\Model\Menu::class, [], [], '', false); $this->_menuFactoryMock->method('create')->will($this->returnValue($menuMock)); $menuMock->method('toArray') @@ -354,9 +398,17 @@ class ItemTest extends \PHPUnit_Framework_TestCase public function populateFromArrayDataProvider() { return [ - [ + 'default data to constructor' => [ [], - $this->_params, + [ + 'id' => 'item', + 'title' => 'Item Title', + 'action' => '/system/config', + 'resource' => 'Magento_Config::config', + 'depends_on_module' => 'Magento_Backend', + 'depends_on_config' => 'system/config/isEnabled', + 'tooltip' => 'Item tooltip', + ], [ 'parent_id' => null, 'module_name' => 'Magento_Backend', @@ -372,8 +424,16 @@ class ItemTest extends \PHPUnit_Framework_TestCase 'sub_menu' => null ], ], - [ - $this->_params, + 'data without submenu to constructor' => [ + [ + 'id' => 'item', + 'title' => 'Item Title', + 'action' => '/system/config', + 'resource' => 'Magento_Config::config', + 'depends_on_module' => 'Magento_Backend', + 'depends_on_config' => 'system/config/isEnabled', + 'tooltip' => 'Item tooltip', + ], [ 'parent_id' => '1', 'module_name' => 'Magento_Module1', @@ -386,7 +446,15 @@ class ItemTest extends \PHPUnit_Framework_TestCase 'depends_on_module' => null, 'tooltip' => null, 'title' => null, - 'sub_menu' => $this->_params, + 'sub_menu' => [ + 'id' => 'item', + 'title' => 'Item Title', + 'action' => '/system/config', + 'resource' => 'Magento_Config::config', + 'depends_on_module' => 'Magento_Backend', + 'depends_on_config' => 'system/config/isEnabled', + 'tooltip' => 'Item tooltip', + ], ], [ 'parent_id' => '1', @@ -403,7 +471,7 @@ class ItemTest extends \PHPUnit_Framework_TestCase 'sub_menu' => null ], ], - [ + 'data with submenu to constructor' => [ [ 'parent_id' => '1', 'module_name' => 'Magento_Module1', @@ -416,13 +484,29 @@ class ItemTest extends \PHPUnit_Framework_TestCase 'depends_on_module' => null, 'tooltip' => null, 'title' => null, - 'sub_menu' => $this->_params, + 'sub_menu' => [ + 'id' => 'item', + 'title' => 'Item Title', + 'action' => '/system/config', + 'resource' => 'Magento_Config::config', + 'depends_on_module' => 'Magento_Backend', + 'depends_on_config' => 'system/config/isEnabled', + 'tooltip' => 'Item tooltip', + ], ], [ 'parent_id' => '1', 'module_name' => 'Magento_Module1', 'sort_index' => '50', - 'sub_menu' => $this->_params, + 'sub_menu' => [ + 'id' => 'item', + 'title' => 'Item Title', + 'action' => '/system/config', + 'resource' => 'Magento_Config::config', + 'depends_on_module' => 'Magento_Backend', + 'depends_on_config' => 'system/config/isEnabled', + 'tooltip' => 'Item tooltip', + ], ], [ 'parent_id' => '1', @@ -436,7 +520,15 @@ class ItemTest extends \PHPUnit_Framework_TestCase 'depends_on_module' => null, 'tooltip' => '', 'title' => null, - 'sub_menu' => $this->_params + 'sub_menu' => [ + 'id' => 'item', + 'title' => 'Item Title', + 'action' => '/system/config', + 'resource' => 'Magento_Config::config', + 'depends_on_module' => 'Magento_Backend', + 'depends_on_config' => 'system/config/isEnabled', + 'tooltip' => 'Item tooltip', + ] ], ] ]; diff --git a/app/code/Magento/Backend/Test/Unit/Model/MenuTest.php b/app/code/Magento/Backend/Test/Unit/Model/MenuTest.php index 2954afbe64755a58b1a485d73dc0f044e9e386f8..294ff9b41dce6f959591ff9bc013683f62bbfe3c 100644 --- a/app/code/Magento/Backend/Test/Unit/Model/MenuTest.php +++ b/app/code/Magento/Backend/Test/Unit/Model/MenuTest.php @@ -338,10 +338,6 @@ class MenuTest extends \PHPUnit_Framework_TestCase $this->assertEquals($expected, $actual); } - /** - * @covers \Magento\Backend\Model\Menu::toArray - * @covers \Magento\Backend\Model\Menu::serialize - */ public function testSerialize() { $serializerMock = $this->getMock(SerializerInterface::class); @@ -365,10 +361,6 @@ class MenuTest extends \PHPUnit_Framework_TestCase $this->assertEquals('serializedString', $menu->serialize()); } - /** - * @covers \Magento\Backend\Model\Menu::populateFromArray - * @covers \Magento\Backend\Model\Menu::unserialize - */ public function testUnserialize() { $serializerMock = $this->getMock(SerializerInterface::class); diff --git a/dev/tests/integration/testsuite/Magento/Backend/Model/MenuTest.php b/dev/tests/integration/testsuite/Magento/Backend/Model/MenuTest.php index f9628110851d124bb8a6b26272dfebb882a069bc..5cc7ddb911bcd00fc99b1d20858b4cec7ec59edf 100644 --- a/dev/tests/integration/testsuite/Magento/Backend/Model/MenuTest.php +++ b/dev/tests/integration/testsuite/Magento/Backend/Model/MenuTest.php @@ -9,37 +9,34 @@ namespace Magento\Backend\Model; * Test class for \Magento\Backend\Model\Auth. * * @magentoAppArea adminhtml - * @magentoCache all disabled */ class MenuTest extends \PHPUnit_Framework_TestCase { /** * @var \Magento\Backend\Model\Menu */ - protected $_model; + private $model; + + /** @var \Magento\Framework\ObjectManagerInterface */ + private $objectManager; protected function setUp() { parent::setUp(); \Magento\TestFramework\Helper\Bootstrap::getInstance() ->loadArea(\Magento\Backend\App\Area\FrontNameResolver::AREA_CODE); - $this->_model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() - ->create(\Magento\Backend\Model\Auth::class); - \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get( - \Magento\Framework\Config\ScopeInterface::class - )->setCurrentScope(\Magento\Backend\App\Area\FrontNameResolver::AREA_CODE); + $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + $this->model = $this->objectManager->create(\Magento\Backend\Model\Auth::class); + $this->objectManager->get(\Magento\Framework\Config\ScopeInterface::class) + ->setCurrentScope(\Magento\Backend\App\Area\FrontNameResolver::AREA_CODE); } public function testMenuItemManipulation() { /* @var $menu \Magento\Backend\Model\Menu */ - $menu = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create( - \Magento\Backend\Model\Menu\Config::class - )->getMenu(); + $menu = $this->objectManager->create(\Magento\Backend\Model\Menu\Config::class)->getMenu(); /* @var $itemFactory \Magento\Backend\Model\Menu\Item\Factory */ - $itemFactory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create( - \Magento\Backend\Model\Menu\Item\Factory::class - ); + $itemFactory = $this->objectManager->create(\Magento\Backend\Model\Menu\Item\Factory::class); // Add new item in top level $menu->add( @@ -84,13 +81,9 @@ class MenuTest extends \PHPUnit_Framework_TestCase public function testSerializeUnserialize() { /** @var Menu $menu */ - $menu = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get( - \Magento\Backend\Model\MenuFactory::class - )->create(); + $menu = $this->objectManager->get(\Magento\Backend\Model\MenuFactory::class)->create(); /* @var \Magento\Backend\Model\Menu\Item\Factory $itemFactory */ - $itemFactory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create( - \Magento\Backend\Model\Menu\Item\Factory::class - ); + $itemFactory = $this->objectManager->create(\Magento\Backend\Model\Menu\Item\Factory::class); // Add new item in top level $menu->add( @@ -104,7 +97,7 @@ class MenuTest extends \PHPUnit_Framework_TestCase ) ); - //Add submenu + // Add submenu $menu->add( $itemFactory->create( [ @@ -118,11 +111,9 @@ class MenuTest extends \PHPUnit_Framework_TestCase 'Magento_Backend::system3' ); $serializedString = $menu->serialize(); - /** @var Menu $menu2 */ - $menu2 = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get( - \Magento\Backend\Model\MenuFactory::class - )->create(); - $menu2->unserialize($serializedString); - $this->assertEquals($menu, $menu2); + /** @var Menu $unserializedMenu */ + $unserializedMenu = $this->objectManager->get(\Magento\Backend\Model\MenuFactory::class)->create(); + $unserializedMenu->unserialize($serializedString); + $this->assertEquals($menu, $unserializedMenu); } }