cacheInstanceMock = $this->createMock(Config::class); $menuFactoryMock = $this->createPartialMock(MenuFactory::class, ['create']); $this->configReaderMock = $this->createMock(Reader::class); $this->logger = $this->getMockForAbstractClass(LoggerInterface::class); $this->menuMock = $this->createMock(Menu::class); $this->menuBuilderMock = $this->createMock(Builder::class); $menuFactoryMock->expects($this->any())->method('create')->willReturn($this->menuMock); $this->configReaderMock->expects($this->any())->method('read')->willReturn([]); $appState = $this->createPartialMock(State::class, ['getAreaCode']); $appState->expects( $this->any() )->method( 'getAreaCode' )->willReturn( FrontNameResolver::AREA_CODE ); $this->model = (new ObjectManager($this))->getObject( \Magento\Backend\Model\Menu\Config::class, [ 'menuBuilder' => $this->menuBuilderMock, 'menuFactory' => $menuFactoryMock, 'configReader' => $this->configReaderMock, 'configCacheType' => $this->cacheInstanceMock, 'logger' => $this->logger, 'appState' => $appState ] ); } /** * @return void */ public function testGetMenuWithCachedObjectReturnsUnserializedObject(): void { $this->cacheInstanceMock->expects( $this->once() )->method( 'load' )->with( \Magento\Backend\Model\Menu\Config::CACHE_MENU_OBJECT )->willReturn( 'menu_cache' ); $this->menuMock->expects($this->once())->method('unserialize')->with('menu_cache'); $this->assertEquals($this->menuMock, $this->model->getMenu()); } /** * @return void */ public function testGetMenuWithNotCachedObjectBuildsObject(): void { $this->cacheInstanceMock ->method('load') ->with(\Magento\Backend\Model\Menu\Config::CACHE_MENU_OBJECT) ->willReturn(false); $this->configReaderMock->expects($this->once())->method('read')->willReturn([]); $this->menuBuilderMock->expects( $this->exactly(1) )->method( 'getResult' )->willReturn( $this->menuMock ); $this->assertEquals($this->menuMock, $this->model->getMenu()); } /** * @param string $expectedException * * @return void * @dataProvider getMenuExceptionLoggedDataProvider */ public function testGetMenuExceptionLogged(string $expectedException): void { $this->expectException($expectedException); $this->menuBuilderMock->expects( $this->exactly(1) )->method( 'getResult' )->willThrowException( new $expectedException() ); $this->model->getMenu(); } /** * @return array */ public function getMenuExceptionLoggedDataProvider(): array { return [ 'InvalidArgumentException' => ['InvalidArgumentException'], 'BadMethodCallException' => ['BadMethodCallException'], 'OutOfRangeException' => ['OutOfRangeException'] ]; } /** * @return void */ public function testGetMenuGenericExceptionIsNotLogged(): void { $this->logger->expects($this->never())->method('critical'); $this->menuBuilderMock->expects( $this->exactly(1) )->method( 'getResult' )->willThrowException( new \Exception() ); try { $this->model->getMenu(); } catch (\Exception $e) { return; } $this->fail("Generic \Exception was not thrown"); } }