feat: Ajoute EventListener MainframeAttribute
Ajoute l'écouteur d'attribut Mainframe et son test unitaire. L'écouteur gère l'ajout de l'en-tête X-Robots-Tag.
This commit is contained in:
208
tests/EventListener/MainframeAttributeListenerTest.php
Normal file
208
tests/EventListener/MainframeAttributeListenerTest.php
Normal file
@@ -0,0 +1,208 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\App\EventListener;
|
||||
|
||||
use App\Attribute\Mainframe;
|
||||
use App\EventListener\MainframeAttributeListener;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Event\ControllerEvent;
|
||||
use Symfony\Component\HttpKernel\Event\ResponseEvent;
|
||||
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||
|
||||
/**
|
||||
* Test class for the MainframeAttributeListener.
|
||||
* This test class has been updated to reflect the removal of logger calls
|
||||
* from the MainframeAttributeListener.
|
||||
*/
|
||||
class MainframeAttributeListenerTest extends TestCase
|
||||
{
|
||||
private MainframeAttributeListener $listener;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
// No logger mock needed as logger has been removed from the listener
|
||||
$this->listener = new MainframeAttributeListener();
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to create a ControllerEvent.
|
||||
*
|
||||
* @param object $controllerObject The controller instance.
|
||||
* @param string $methodName The method name to be called.
|
||||
* @param array $requestAttributes Additional request attributes.
|
||||
* @return ControllerEvent
|
||||
*/
|
||||
private function createControllerEvent(object $controllerObject, string $methodName, array $requestAttributes = []): ControllerEvent
|
||||
{
|
||||
$request = new Request([], [], $requestAttributes);
|
||||
$kernel = $this->createMock(HttpKernelInterface::class);
|
||||
return new ControllerEvent($kernel, [$controllerObject, $methodName], $request, HttpKernelInterface::MAIN_REQUEST);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to create a ResponseEvent.
|
||||
*
|
||||
* @param Request $request The request object.
|
||||
* @param Response $response The response object.
|
||||
* @return ResponseEvent
|
||||
*/
|
||||
private function createResponseEvent(Request $request, Response $response): ResponseEvent
|
||||
{
|
||||
$kernel = $this->createMock(HttpKernelInterface::class);
|
||||
return new ResponseEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that the X-Robots-Tag: noindex header is added when index is false on the method.
|
||||
*/
|
||||
public function testNoIndexHeaderAddedWhenMethodIndexIsFalse(): void
|
||||
{
|
||||
// Dummy controller with Mainframe attribute on method
|
||||
$controller = new class {
|
||||
#[Mainframe(index: false, sitemap: true)]
|
||||
public function testAction(): Response { return new Response(); }
|
||||
};
|
||||
|
||||
// Simulate ControllerEvent
|
||||
$controllerEvent = $this->createControllerEvent($controller, 'testAction');
|
||||
$this->listener->onKernelController($controllerEvent);
|
||||
|
||||
// Assert that _mainframe_noindex attribute is set to true
|
||||
$this->assertTrue($controllerEvent->getRequest()->attributes->get('_mainframe_noindex'));
|
||||
|
||||
// Simulate ResponseEvent
|
||||
$response = new Response();
|
||||
$responseEvent = $this->createResponseEvent($controllerEvent->getRequest(), $response);
|
||||
$this->listener->onKernelResponse($responseEvent);
|
||||
|
||||
// Assert that the X-Robots-Tag header is present and correct
|
||||
$this->assertTrue($response->headers->has('X-Robots-Tag'));
|
||||
$this->assertEquals('noindex', $response->headers->get('X-Robots-Tag'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that the X-Robots-Tag: noindex header is added when index is false on the class
|
||||
* and not overridden by the method.
|
||||
*/
|
||||
public function testNoIndexHeaderAddedWhenClassIndexIsFalse(): void
|
||||
{
|
||||
// Dummy controller with Mainframe attribute on class, no attribute on method
|
||||
$controller = new #[Mainframe(index: false, sitemap: true)] class {
|
||||
public function testAction(): Response { return new Response(); }
|
||||
};
|
||||
|
||||
// Simulate ControllerEvent
|
||||
$controllerEvent = $this->createControllerEvent($controller, 'testAction');
|
||||
$this->listener->onKernelController($controllerEvent);
|
||||
|
||||
// Assert that _mainframe_noindex attribute is set to true
|
||||
$this->assertTrue($controllerEvent->getRequest()->attributes->get('_mainframe_noindex'));
|
||||
|
||||
// Simulate ResponseEvent
|
||||
$response = new Response();
|
||||
$responseEvent = $this->createResponseEvent($controllerEvent->getRequest(), $response);
|
||||
$this->listener->onKernelResponse($responseEvent);
|
||||
|
||||
// Assert that the X-Robots-Tag header is present and correct
|
||||
$this->assertTrue($response->headers->has('X-Robots-Tag'));
|
||||
$this->assertEquals('noindex', $response->headers->get('X-Robots-Tag'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that no X-Robots-Tag: noindex header is added when index is true on the method.
|
||||
*/
|
||||
public function testNoIndexHeaderNotAddedWhenMethodIndexIsTrue(): void
|
||||
{
|
||||
// Dummy controller with Mainframe attribute on method (index: true)
|
||||
$controller = new class {
|
||||
#[Mainframe(index: true, sitemap: true)]
|
||||
public function testAction(): Response { return new Response(); }
|
||||
};
|
||||
|
||||
// Simulate ControllerEvent
|
||||
$controllerEvent = $this->createControllerEvent($controller, 'testAction');
|
||||
$this->listener->onKernelController($controllerEvent);
|
||||
|
||||
// Assert that _mainframe_noindex attribute is set to false
|
||||
$this->assertFalse($controllerEvent->getRequest()->attributes->get('_mainframe_noindex'));
|
||||
|
||||
// Simulate ResponseEvent
|
||||
$response = new Response();
|
||||
$responseEvent = $this->createResponseEvent($controllerEvent->getRequest(), $response);
|
||||
$this->listener->onKernelResponse($responseEvent);
|
||||
|
||||
// Assert that the X-Robots-Tag header is NOT present
|
||||
$this->assertFalse($response->headers->has('X-Robots-Tag'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that no X-Robots-Tag: noindex header is added when index is true on the class.
|
||||
*/
|
||||
public function testNoIndexHeaderNotAddedWhenClassIndexIsTrue(): void
|
||||
{
|
||||
// Dummy controller with Mainframe attribute on class (index: true), no attribute on method
|
||||
$controller = new #[Mainframe(index: true, sitemap: true)] class {
|
||||
public function testAction(): Response { return new Response(); }
|
||||
};
|
||||
|
||||
// Simulate ControllerEvent
|
||||
$controllerEvent = $this->createControllerEvent($controller, 'testAction');
|
||||
$this->listener->onKernelController($controllerEvent);
|
||||
|
||||
// Assert that _mainframe_noindex attribute is set to false
|
||||
$this->assertFalse($controllerEvent->getRequest()->attributes->get('_mainframe_noindex'));
|
||||
|
||||
// Simulate ResponseEvent
|
||||
$response = new Response();
|
||||
$responseEvent = $this->createResponseEvent($controllerEvent->getRequest(), $response);
|
||||
$this->listener->onKernelResponse($responseEvent);
|
||||
|
||||
// Assert that the X-Robots-Tag header is NOT present
|
||||
$this->assertFalse($response->headers->has('X-Robots-Tag'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that no X-Robots-Tag: noindex header is added when no attribute is present.
|
||||
*/
|
||||
public function testNoIndexHeaderNotAddedWhenNoAttributePresent(): void
|
||||
{
|
||||
// Dummy controller with no Mainframe attribute
|
||||
$controller = new class {
|
||||
public function testAction(): Response { return new Response(); }
|
||||
};
|
||||
|
||||
// Simulate ControllerEvent
|
||||
$controllerEvent = $this->createControllerEvent($controller, 'testAction');
|
||||
$this->listener->onKernelController($controllerEvent);
|
||||
|
||||
// Assert that _mainframe_noindex attribute is set to false (default)
|
||||
$this->assertFalse($controllerEvent->getRequest()->attributes->get('_mainframe_noindex'));
|
||||
|
||||
// Simulate ResponseEvent
|
||||
$response = new Response();
|
||||
$responseEvent = $this->createResponseEvent($controllerEvent->getRequest(), $response);
|
||||
$this->listener->onKernelResponse($responseEvent);
|
||||
|
||||
// Assert that the X-Robots-Tag header is NOT present
|
||||
$this->assertFalse($response->headers->has('X-Robots-Tag'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that a closure controller is skipped.
|
||||
*/
|
||||
public function testClosureControllerIsSkipped(): void
|
||||
{
|
||||
$kernel = $this->createMock(HttpKernelInterface::class);
|
||||
$request = new Request();
|
||||
$controller = function() { return new Response(); }; // A closure controller
|
||||
|
||||
$controllerEvent = new ControllerEvent($kernel, $controller, $request, HttpKernelInterface::MAIN_REQUEST);
|
||||
|
||||
$this->listener->onKernelController($controllerEvent);
|
||||
|
||||
// Ensure no _mainframe_noindex attribute is set
|
||||
$this->assertFalse($request->attributes->has('_mainframe_noindex'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user