1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\NewsBundle\Tests\Functional\Routing; |
15
|
|
|
|
16
|
|
|
use Sonata\NewsBundle\Tests\App\AppKernel; |
17
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Javier Spagnoletti <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
final class RoutingTest extends WebTestCase |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @group legacy |
26
|
|
|
* |
27
|
|
|
* @dataProvider getRoutes |
28
|
|
|
*/ |
29
|
|
|
public function testRoutes(string $name, string $path, array $methods): void |
30
|
|
|
{ |
31
|
|
|
$client = static::createClient(); |
32
|
|
|
$router = $client->getContainer()->get('router'); |
33
|
|
|
|
34
|
|
|
$route = $router->getRouteCollection()->get($name); |
35
|
|
|
|
36
|
|
|
$this->assertNotNull($route); |
37
|
|
|
$this->assertSame($path, $route->getPath()); |
38
|
|
|
$this->assertEmpty(array_diff($methods, $route->getMethods())); |
39
|
|
|
|
40
|
|
|
$matchingPath = $path; |
41
|
|
|
$matchingFormat = ''; |
42
|
|
|
if (false !== strpos($matchingPath, '.{_format}', -10)) { |
43
|
|
|
$matchingFormat = '.json'; |
44
|
|
|
$matchingPath = str_replace('.{_format}', $matchingFormat, $path); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$matcher = $router->getMatcher(); |
48
|
|
|
$requestContext = $router->getContext(); |
49
|
|
|
|
50
|
|
|
foreach ($methods as $method) { |
51
|
|
|
$requestContext->setMethod($method); |
52
|
|
|
|
53
|
|
|
// Check paths like "/api/news/posts.json". |
54
|
|
|
$match = $matcher->match($matchingPath); |
55
|
|
|
|
56
|
|
|
$this->assertSame($name, $match['_route']); |
57
|
|
|
|
58
|
|
|
if ($matchingFormat) { |
59
|
|
|
$this->assertSame(ltrim($matchingFormat, '.'), $match['_format']); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$matchingPathWithStrippedFormat = str_replace('.{_format}', '', $path); |
63
|
|
|
|
64
|
|
|
// Check paths like "/api/news/posts". |
65
|
|
|
$match = $matcher->match($matchingPathWithStrippedFormat); |
66
|
|
|
|
67
|
|
|
$this->assertSame($name, $match['_route']); |
68
|
|
|
|
69
|
|
|
if ($matchingFormat) { |
70
|
|
|
$this->assertSame(ltrim($matchingFormat, '.'), $match['_format']); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getRoutes(): iterable |
76
|
|
|
{ |
77
|
|
|
// API |
78
|
|
|
yield ['nelmio_api_doc_index', '/api/doc/{view}', ['GET']]; |
79
|
|
|
|
80
|
|
|
// API - comment |
81
|
|
|
yield ['sonata_api_news_comment_get_comment', '/api/news/comments/{id}.{_format}', ['GET']]; |
82
|
|
|
yield ['sonata_api_news_comment_delete_comment', '/api/news/comments/{id}.{_format}', ['DELETE']]; |
83
|
|
|
|
84
|
|
|
// API - post |
85
|
|
|
yield ['sonata_api_news_post_get_posts', '/api/news/posts.{_format}', ['GET']]; |
86
|
|
|
yield ['sonata_api_news_post_get_post', '/api/news/posts/{id}.{_format}', ['GET']]; |
87
|
|
|
yield ['sonata_api_news_post_post_post', '/api/news/posts.{_format}', ['POST']]; |
88
|
|
|
yield ['sonata_api_news_post_put_post', '/api/news/posts/{id}.{_format}', ['PUT']]; |
89
|
|
|
yield ['sonata_api_news_post_delete_post', '/api/news/posts/{id}.{_format}', ['DELETE']]; |
90
|
|
|
yield ['sonata_api_news_post_get_post_comments', '/api/news/posts/{id}/comments.{_format}', ['GET']]; |
91
|
|
|
yield ['sonata_api_news_post_post_post_comments', '/api/news/posts/{id}/comments.{_format}', ['POST']]; |
92
|
|
|
yield ['sonata_api_news_post_put_post_comments', '/api/news/posts/{postId}/comments/{commentId}.{_format}', ['PUT']]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
protected static function getKernelClass(): string |
96
|
|
|
{ |
97
|
|
|
return AppKernel::class; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|