From 6a1c337c35b103f8d9681a7552247ec0490951dc Mon Sep 17 00:00:00 2001 From: Yup Date: Thu, 28 Jul 2022 08:58:52 +0300 Subject: [PATCH 1/8] Make Menu non-shared In order to have new instance for every request in Long-running process (ReactPHP, Amphp, RoadRunner) --- src/Resources/config/menu.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Resources/config/menu.php b/src/Resources/config/menu.php index 2828fecbcf..fb566618c2 100644 --- a/src/Resources/config/menu.php +++ b/src/Resources/config/menu.php @@ -33,6 +33,7 @@ ]) ->set('sonata.admin.sidebar_menu', MenuItem::class) + ->share(false) ->tag('knp_menu.menu', ['alias' => 'sonata_admin_sidebar']) ->factory([ new ReferenceConfigurator('sonata.admin.menu_builder'), From af31e54dd5e647de2461d66bd5ce8488575b03f0 Mon Sep 17 00:00:00 2001 From: Warxcell Date: Thu, 28 Jul 2022 21:45:07 +0300 Subject: [PATCH 2/8] add test case covering long-running process. --- phpunit.xml.dist | 1 + tests/App/EventListener/ConfigureMenu.php | 34 +++++++++++++++++++ tests/Functional/Controller/MenuTest.php | 40 +++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 tests/App/EventListener/ConfigureMenu.php create mode 100644 tests/Functional/Controller/MenuTest.php diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 5787b7178e..10b7297411 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -33,5 +33,6 @@ It's auto-generated by sonata-project/dev-kit package. + diff --git a/tests/App/EventListener/ConfigureMenu.php b/tests/App/EventListener/ConfigureMenu.php new file mode 100644 index 0000000000..864d88fa3e --- /dev/null +++ b/tests/App/EventListener/ConfigureMenu.php @@ -0,0 +1,34 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\AdminBundle\Tests\App\EventListener; + +use Sonata\AdminBundle\Event\ConfigureMenuEvent; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; + +final class ConfigureMenu implements EventSubscriberInterface +{ + private int $counter = 0; + + public static function getSubscribedEvents(): array + { + return [ + ConfigureMenuEvent::SIDEBAR => 'configureMenu', + ]; + } + + public function configureMenu(ConfigureMenuEvent $configureMenuEvent): void + { + $configureMenuEvent->getMenu()->addChild(sprintf('Dynamic Menu %s', ++$this->counter))->setAttribute('class', 'dynamic-menu'); + } +} diff --git a/tests/Functional/Controller/MenuTest.php b/tests/Functional/Controller/MenuTest.php new file mode 100644 index 0000000000..f648ff8763 --- /dev/null +++ b/tests/Functional/Controller/MenuTest.php @@ -0,0 +1,40 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\AdminBundle\Tests\Functional\Controller; + +use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; + +final class MenuTest extends WebTestCase +{ + public function testDynamicMenuInLongRunningProcess(): void + { + $client = static::createClient(); + $client->disableReboot(); + + for ($i = 1; $i < 5; ++$i) { + $client->request(Request::METHOD_GET, '/admin/dashboard'); + + static::assertSame(Response::HTTP_OK, $client->getResponse()->getStatusCode()); + + $crawler = $client->getCrawler(); + + $menu = $crawler->filter('.sidebar-menu .dynamic-menu a'); + + static::assertCount(1, $menu); + static::assertSame(sprintf('Dynamic Menu %s', $i), $menu->innerText()); + } + } +} From 6dbaa2ba66a286619e5033be74f3dae16573ac0d Mon Sep 17 00:00:00 2001 From: Warxcell Date: Thu, 28 Jul 2022 21:53:26 +0300 Subject: [PATCH 3/8] add test case covering long-running process. --- phpunit.xml.dist | 1 - tests/Functional/Controller/MenuTest.php | 6 ++++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 10b7297411..5787b7178e 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -33,6 +33,5 @@ It's auto-generated by sonata-project/dev-kit package. - diff --git a/tests/Functional/Controller/MenuTest.php b/tests/Functional/Controller/MenuTest.php index f648ff8763..ec7a54fc71 100644 --- a/tests/Functional/Controller/MenuTest.php +++ b/tests/Functional/Controller/MenuTest.php @@ -13,12 +13,18 @@ namespace Sonata\AdminBundle\Tests\Functional\Controller; +use Sonata\AdminBundle\Tests\App\AppKernel; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; final class MenuTest extends WebTestCase { + protected static function getKernelClass(): string + { + return AppKernel::class; + } + public function testDynamicMenuInLongRunningProcess(): void { $client = static::createClient(); From 4e5519106f1f40ba80147a116f806b5ff9f4ce8a Mon Sep 17 00:00:00 2001 From: Warxcell Date: Thu, 28 Jul 2022 21:56:17 +0300 Subject: [PATCH 4/8] add test case covering long-running process. --- tests/Functional/Controller/MenuTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/Functional/Controller/MenuTest.php b/tests/Functional/Controller/MenuTest.php index ec7a54fc71..f3b4fd919c 100644 --- a/tests/Functional/Controller/MenuTest.php +++ b/tests/Functional/Controller/MenuTest.php @@ -20,11 +20,6 @@ final class MenuTest extends WebTestCase { - protected static function getKernelClass(): string - { - return AppKernel::class; - } - public function testDynamicMenuInLongRunningProcess(): void { $client = static::createClient(); @@ -43,4 +38,9 @@ public function testDynamicMenuInLongRunningProcess(): void static::assertSame(sprintf('Dynamic Menu %s', $i), $menu->innerText()); } } + + protected static function getKernelClass(): string + { + return AppKernel::class; + } } From 9ad44a146c1be8d6812bf31f55f6da67e426f0af Mon Sep 17 00:00:00 2001 From: Warxcell Date: Thu, 28 Jul 2022 21:58:47 +0300 Subject: [PATCH 5/8] Fix test on SF4.4 --- tests/Functional/Controller/MenuTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Functional/Controller/MenuTest.php b/tests/Functional/Controller/MenuTest.php index f3b4fd919c..93332906ea 100644 --- a/tests/Functional/Controller/MenuTest.php +++ b/tests/Functional/Controller/MenuTest.php @@ -35,7 +35,7 @@ public function testDynamicMenuInLongRunningProcess(): void $menu = $crawler->filter('.sidebar-menu .dynamic-menu a'); static::assertCount(1, $menu); - static::assertSame(sprintf('Dynamic Menu %s', $i), $menu->innerText()); + static::assertSame(sprintf('Dynamic Menu %s', $i), $menu->text()); } } From bfe005441d02aad15a5989ac8fcadbcfa9e0550c Mon Sep 17 00:00:00 2001 From: Warxcell Date: Thu, 28 Jul 2022 22:00:37 +0300 Subject: [PATCH 6/8] Fix test on SF4.4 --- tests/Functional/Controller/MenuTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Functional/Controller/MenuTest.php b/tests/Functional/Controller/MenuTest.php index 93332906ea..6412a23c58 100644 --- a/tests/Functional/Controller/MenuTest.php +++ b/tests/Functional/Controller/MenuTest.php @@ -35,7 +35,7 @@ public function testDynamicMenuInLongRunningProcess(): void $menu = $crawler->filter('.sidebar-menu .dynamic-menu a'); static::assertCount(1, $menu); - static::assertSame(sprintf('Dynamic Menu %s', $i), $menu->text()); + static::assertSame(sprintf('Dynamic Menu %s', $i), trim($menu->text())); } } From 37a1a39c06729e968e67ae20c154bdaac1255669 Mon Sep 17 00:00:00 2001 From: Warxcell Date: Thu, 28 Jul 2022 23:01:06 +0300 Subject: [PATCH 7/8] add description --- tests/Functional/Controller/MenuTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Functional/Controller/MenuTest.php b/tests/Functional/Controller/MenuTest.php index 6412a23c58..7960e56c89 100644 --- a/tests/Functional/Controller/MenuTest.php +++ b/tests/Functional/Controller/MenuTest.php @@ -23,7 +23,7 @@ final class MenuTest extends WebTestCase public function testDynamicMenuInLongRunningProcess(): void { $client = static::createClient(); - $client->disableReboot(); + $client->disableReboot(); // forces requests to land at same Kernel, simulating long-running process like one used in roadrunner/reactphp/amphp for ($i = 1; $i < 5; ++$i) { $client->request(Request::METHOD_GET, '/admin/dashboard'); From 3f152c727fc728963666da065d7be5ea86a411fd Mon Sep 17 00:00:00 2001 From: Warxcell Date: Thu, 28 Jul 2022 23:43:19 +0300 Subject: [PATCH 8/8] Ignore psalm. --- src/FieldDescription/TypeGuesserChain.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/FieldDescription/TypeGuesserChain.php b/src/FieldDescription/TypeGuesserChain.php index 4cdbaeacf4..7dfa94c858 100644 --- a/src/FieldDescription/TypeGuesserChain.php +++ b/src/FieldDescription/TypeGuesserChain.php @@ -53,6 +53,8 @@ public function __construct(array $guessers) /** * @psalm-suppress ArgumentTypeCoercion @see https://github.com/vimeo/psalm/issues/5938 + * @psalm-suppress MoreSpecificReturnType @see https://github.com/vimeo/psalm/issues/8330#issuecomment-1198609261 + * @psalm-suppress LessSpecificReturnStatement @see https://github.com/vimeo/psalm/issues/8330#issuecomment-1198609261 */ public function guess(FieldDescriptionInterface $fieldDescription): ?TypeGuess {