|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Palmtree\CanonicalUrlBundle\Tests\EventListener; |
|
4
|
|
|
|
|
5
|
|
|
use Palmtree\CanonicalUrlBundle\EventListener\KernelEventListener; |
|
6
|
|
|
use Palmtree\CanonicalUrlBundle\Service\CanonicalUrlGenerator; |
|
7
|
|
|
use Palmtree\CanonicalUrlBundle\Tests\AbstractTest; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
10
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
|
11
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; |
|
12
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
13
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface; |
|
14
|
|
|
use Symfony\Component\HttpKernel\Tests\TestHttpKernel; |
|
15
|
|
|
|
|
16
|
|
|
class KernelEventListenerTest extends AbstractTest |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @dataProvider configProvider |
|
20
|
|
|
*/ |
|
21
|
|
|
public function testCanonicalRedirect(array $config) |
|
22
|
|
|
{ |
|
23
|
|
|
$event = new GetResponseEvent( |
|
24
|
|
|
new TestHttpKernel(), |
|
25
|
|
|
$this->getFooRequest(false), |
|
26
|
|
|
HttpKernelInterface::MASTER_REQUEST |
|
27
|
|
|
); |
|
28
|
|
|
|
|
29
|
|
|
$response = new Response(); |
|
30
|
|
|
$event->setResponse($response); |
|
31
|
|
|
|
|
32
|
|
|
$listener = $this->getKernelEventListener($config); |
|
33
|
|
|
$listener->onKernelRequest($event); |
|
34
|
|
|
|
|
35
|
|
|
$this->assertNotEquals($response, $event->getResponse()); |
|
36
|
|
|
$this->assertTrue($event->getResponse() instanceof RedirectResponse); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @dataProvider configProvider |
|
41
|
|
|
* @param array $config |
|
42
|
|
|
*/ |
|
43
|
|
|
public function testTrailingSlashRedirect(array $config) |
|
44
|
|
|
{ |
|
45
|
|
|
$event = new GetResponseForExceptionEvent( |
|
46
|
|
|
new TestHttpKernel(), |
|
47
|
|
|
$this->getFooRequest(), |
|
48
|
|
|
HttpKernelInterface::MASTER_REQUEST, |
|
49
|
|
|
new NotFoundHttpException('') |
|
50
|
|
|
); |
|
51
|
|
|
|
|
52
|
|
|
$listener = $this->getKernelEventListener($config); |
|
53
|
|
|
|
|
54
|
|
|
$listener->onKernelException($event); |
|
55
|
|
|
|
|
56
|
|
|
/** @var RedirectResponse $response */ |
|
57
|
|
|
$response = $event->getResponse(); |
|
58
|
|
|
|
|
59
|
|
|
$this->assertTrue($response instanceof RedirectResponse); |
|
60
|
|
|
$this->assertTrue($response->getTargetUrl() === 'https://example.org/foo'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param array $config |
|
65
|
|
|
* @return KernelEventListener |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function getKernelEventListener(array $config) |
|
68
|
|
|
{ |
|
69
|
|
|
$router = $this->getRouter(); |
|
70
|
|
|
|
|
71
|
|
|
$urlGenerator = new CanonicalUrlGenerator($router, $config); |
|
72
|
|
|
$listener = new KernelEventListener($router, $urlGenerator, $config); |
|
73
|
|
|
|
|
74
|
|
|
return $listener; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|