tokenStorage = self::$container->get(TokenStorageInterface::class); $this->subscriber = new UserMenuSubscriber($this->tokenStorage, $translator); } public function testOnUserMenuConfigure(): void { $factory = new MenuFactory(); $item = new MenuItem('test', $factory); $request = new Request(); $requests = new RequestStack(); $requests->push($request); $event = new MenuEvent($factory, $item, $requests); $this->assertNull($this->subscriber->onUserMenuConfigure($event)); } public function testAuthentificatedUserFields(): void { $token = $this->createMock(TokenInterface::class); $token->method('getRoles')->willReturn([new Role('test_role')]); $token->method('getUsername')->willReturn('test_user'); $this->tokenStorage->setToken($token); $factory = new MenuFactory(); $item = new MenuItem('test', $factory); $request = new Request(); $requests = new RequestStack(); $requests->push($request); $menuEvent = new MenuEvent($factory, $item, $requests); $this->subscriber->onUserMenuConfigure($menuEvent); $children = $menuEvent->getItem()->getChildren()['test_user']->getChildren(); $authentificatedItems = ['logout', 'edit profile']; foreach ($authentificatedItems as $key) { $this->assertInstanceOf(MenuItem::class, $children[$key]); } $this->assertEquals(count($children), count($authentificatedItems)); } public function testUnauthentificatedUserFields(): void { $factory = new MenuFactory(); $item = new MenuItem('test', $factory); $request = new Request(); $requests = new RequestStack(); $requests->push($request); $menuEvent = new MenuEvent($factory, $item, $requests); $this->subscriber->onUserMenuConfigure($menuEvent); $children = $menuEvent->getItem()->getChildren()['user']->getChildren(); $unauthentificatedItems = ['login', 'register']; foreach ($unauthentificatedItems as $key) { $this->assertInstanceOf(MenuItem::class, $children[$key]); } $this->assertEquals(count($children), count($unauthentificatedItems)); } public function testGetSubscribedEvents(): void { $subscribedEvents = $this->subscriber->getSubscribedEvents(); $reflectionClass = new \ReflectionClass($this->subscriber); foreach ($subscribedEvents as $method) { $this->assertTrue($reflectionClass->hasMethod($method)); } } }