mirror of
				https://github.com/kevinveenbirkenbach/infinito.git
				synced 2025-10-31 09:19:08 +00:00 
			
		
		
		
	Optimized UserMenuSubscriber
This commit is contained in:
		| @@ -11,6 +11,8 @@ use Symfony\Component\HttpFoundation\Request; | ||||
| use FOS\RestBundle\Request\ParameterBag; | ||||
|  | ||||
| /** | ||||
|  * This class is just a result of refactoring. Feel free to replace it. | ||||
|  * | ||||
|  * @author kevinfrantz | ||||
|  */ | ||||
| abstract class AbstractEntityMenuSubscriber implements EventSubscriberInterface | ||||
| @@ -35,9 +37,9 @@ abstract class AbstractEntityMenuSubscriber implements EventSubscriberInterface | ||||
|      */ | ||||
|     protected function generateShowDropdown(ItemInterface $menu, Event $event, string $route): void | ||||
|     { | ||||
|         $dropdown = $menu->addChild($this->trans('show'), [ | ||||
|         $dropdown = $menu->addChild($this->trans('format'), [ | ||||
|             'attributes' => [ | ||||
|                 'icon' => 'fas fa-eye', | ||||
|                 'icon' => 'fas fa-file', | ||||
|                 'dropdown' => 'true', | ||||
|             ], | ||||
|         ]); | ||||
| @@ -71,16 +73,6 @@ abstract class AbstractEntityMenuSubscriber implements EventSubscriberInterface | ||||
|         return $this->translator->trans($id, $parameter); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @param Event $event | ||||
|      * | ||||
|      * @return int|string | ||||
|      */ | ||||
|     protected function getRequestIdentity(Event $event) | ||||
|     { | ||||
|         return $this->getRequestAttributs($event)->get('identity'); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @param Event $event | ||||
|      * | ||||
|   | ||||
| @@ -42,14 +42,14 @@ class UserMenuSubscriber extends AbstractEntityMenuSubscriber implements EventSu | ||||
|     public function onUserMenuConfigure(MenuEvent $event): void | ||||
|     { | ||||
|         $menu = $event->getItem(); | ||||
|         $menu->addChild($this->translator->trans('start'), [ | ||||
|         $menu->addChild($this->trans('start'), [ | ||||
|             'route' => 'homepage', | ||||
|             'attributes' => [ | ||||
|                 'icon' => 'fab fa-font-awesome-flag', | ||||
|             ], | ||||
|         ]); | ||||
|  | ||||
|         $menu->addChild($this->translator->trans('imprint'), [ | ||||
|         $menu->addChild($this->trans('imprint'), [ | ||||
|             'uri' => '/api/rest/source/'.strtolower(ImpressumFixtureSource::SLUG).'.html', | ||||
|             'attributes' => [ | ||||
|                 'icon' => 'fas fa-address-card', | ||||
| @@ -76,7 +76,7 @@ class UserMenuSubscriber extends AbstractEntityMenuSubscriber implements EventSu | ||||
|     { | ||||
|         $token = $this->getToken(); | ||||
|  | ||||
|         return ($token) ? $token->getUsername() : 'user'; | ||||
|         return ($token) ? $token->getUsername() : $this->trans('user'); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
| @@ -101,14 +101,14 @@ class UserMenuSubscriber extends AbstractEntityMenuSubscriber implements EventSu | ||||
|             ], | ||||
|         ]); | ||||
|         if ($this->getRoles()) { | ||||
|             $dropdown->addChild($this->translator->trans('logout'), [ | ||||
|             $dropdown->addChild($this->trans('logout'), [ | ||||
|                 'route' => 'logout', | ||||
|                 'attributes' => [ | ||||
|                     'icon' => 'fas fa-sign-out-alt', | ||||
|                     'divider_append' => true, | ||||
|                 ], | ||||
|             ]); | ||||
|             $dropdown->addChild($this->translator->trans('edit profile'), [ | ||||
|             $dropdown->addChild($this->trans('edit profile'), [ | ||||
|                 'route' => 'fos_user_profile_edit', | ||||
|                 'attributes' => [ | ||||
|                     'icon' => 'fas fa-user-edit', | ||||
| @@ -116,20 +116,20 @@ class UserMenuSubscriber extends AbstractEntityMenuSubscriber implements EventSu | ||||
|                 ], | ||||
|             ]); | ||||
|         } else { | ||||
|             $dropdown->addChild($this->translator->trans('login'), [ | ||||
|             $dropdown->addChild($this->trans('login'), [ | ||||
|                 'route' => 'fos_user_security_login', | ||||
|                 'attributes' => [ | ||||
|                     'divider_append' => true, | ||||
|                     'icon' => 'fas fa-sign-in-alt', | ||||
|                 ], | ||||
|             ]); | ||||
|             $dropdown->addChild('register', [ | ||||
|                 'route' => 'fos_user_registration_register', | ||||
|                 'attributes' => [ | ||||
|                     'icon' => 'fas fa-file-signature', | ||||
|                 ], | ||||
|             ]); | ||||
|         } | ||||
|         $dropdown->addChild('register', [ | ||||
|             'route' => 'fos_user_registration_register', | ||||
|             'attributes' => [ | ||||
|                 'icon' => 'fas fa-file-signature', | ||||
|             ], | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|   | ||||
| @@ -0,0 +1,88 @@ | ||||
| <?php | ||||
|  | ||||
| namespace Tests\Integration\Entity\Subscriber; | ||||
|  | ||||
| use Symfony\Component\Translation\Translator; | ||||
| use Infinito\Event\Menu\MenuEvent; | ||||
| use Symfony\Component\HttpFoundation\RequestStack; | ||||
| use Symfony\Component\HttpFoundation\Request; | ||||
| use Knp\Menu\MenuItem; | ||||
| use Knp\Menu\MenuFactory; | ||||
| use Infinito\Subscriber\UserMenuSubscriber; | ||||
| use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||||
| use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | ||||
| use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||||
| use Symfony\Component\Security\Core\Role\Role; | ||||
|  | ||||
| /** | ||||
|  * @author kevinfrantz | ||||
|  */ | ||||
| class UserMenuSubscriberIntegrationTest extends KernelTestCase | ||||
| { | ||||
|     /** | ||||
|      * @var UserMenuSubscriber | ||||
|      */ | ||||
|     private $subscriber; | ||||
|  | ||||
|     /** | ||||
|      * @var TokenStorageInterface | ||||
|      */ | ||||
|     private $tokenStorge; | ||||
|  | ||||
|     public function setUp(): void | ||||
|     { | ||||
|         self::bootKernel(); | ||||
|         $translator = new Translator('en'); | ||||
|         $this->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)); | ||||
|     } | ||||
| } | ||||
| @@ -1,44 +0,0 @@ | ||||
| <?php | ||||
|  | ||||
| namespace Tests\Unit\Entity\Subscriber; | ||||
|  | ||||
| use Symfony\Component\Translation\Translator; | ||||
| use Infinito\Event\Menu\MenuEvent; | ||||
| use Symfony\Component\HttpFoundation\RequestStack; | ||||
| use Symfony\Component\HttpFoundation\Request; | ||||
| use Knp\Menu\MenuItem; | ||||
| use Knp\Menu\MenuFactory; | ||||
| use Infinito\Subscriber\UserMenuSubscriber; | ||||
| use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||||
| use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | ||||
|  | ||||
| /** | ||||
|  * @author kevinfrantz | ||||
|  */ | ||||
| class UserMenuSubscriberTest extends KernelTestCase | ||||
| { | ||||
|     /** | ||||
|      * @var UserMenuSubscriber | ||||
|      */ | ||||
|     public $subscriber; | ||||
|  | ||||
|     public function setUp(): void | ||||
|     { | ||||
|         self::bootKernel(); | ||||
|         $translator = new Translator('en'); | ||||
|         $tokenStorage = self::$container->get(TokenStorageInterface::class); | ||||
|         $this->subscriber = new UserMenuSubscriber($tokenStorage, $translator); | ||||
|     } | ||||
|  | ||||
|     public function testOnUserMenuConfigure(): void | ||||
|     { | ||||
|         $factory = new MenuFactory(); | ||||
|         $item = new MenuItem('test', $factory); | ||||
|         $request = new Request(); | ||||
|         $request->attributes->set('id', 123); | ||||
|         $requests = new RequestStack(); | ||||
|         $requests->push($request); | ||||
|         $event = new MenuEvent($factory, $item, $requests); | ||||
|         $this->assertNull($this->subscriber->onUserMenuConfigure($event)); | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user