Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix menu in long-running process (RoadRunner, ReactPHP, Amphp) #7885

Merged
merged 8 commits into from Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we cant add a test, we should at least leave a comment about why we do this, imho

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise menu is created once and then re-used - and that doesn't work if there is dynamic content in the menu.

@Warxcell Is there a way to add a test about the use-case which didn't work ?

Sure. Adding it atm.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added. :)

->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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what makes the client share the same container? If yes, can you add a comment here to explain this line (ex: this ensures the container is reused across multiple request to simulate the behavior of ...)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, adding


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;
}
}