Skip to content

Commit

Permalink
Fix menu in long-running process (RoadRunner, ReactPHP, Amphp) (#7885)
Browse files Browse the repository at this point in the history
* Make Menu non-shared

In order to have new instance for every request in Long-running process (ReactPHP, Amphp, RoadRunner)

* add test case covering long-running process.

* add test case covering long-running process.

* add test case covering long-running process.

* Fix test on SF4.4

* Fix test on SF4.4

* add description

* Ignore psalm.
  • Loading branch information
Warxcell committed Jul 28, 2022
1 parent 0210c1e commit 2b3c098
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/FieldDescription/TypeGuesserChain.php
Expand Up @@ -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
{
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/menu.php
Expand Up @@ -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'),
Expand Down
34 changes: 34 additions & 0 deletions tests/App/EventListener/ConfigureMenu.php
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* 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');
}
}
46 changes: 46 additions & 0 deletions tests/Functional/Controller/MenuTest.php
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* 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 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
{
public function testDynamicMenuInLongRunningProcess(): void
{
$client = static::createClient();
$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');

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), trim($menu->text()));
}
}

protected static function getKernelClass(): string
{
return AppKernel::class;
}
}

0 comments on commit 2b3c098

Please sign in to comment.