Completed
Push — master ( d66330...eb9ee0 )
by Grégoire
02:15
created

RoutingTest::getRoutes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\MediaBundle\Tests\Functional\Routing;
15
16
use Sonata\MediaBundle\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
        // define {provider} for data set #17
41
        $path = str_replace('{provider}', 'test', $path);
42
43
        $matchingPath = $path;
44
        $matchingFormat = '';
45
        if (false !== strpos($matchingPath, '.{_format}', -10)) {
46
            $matchingFormat = '.json';
47
            $matchingPath = str_replace('.{_format}', $matchingFormat, $path);
48
        }
49
50
        $matcher = $router->getMatcher();
51
        $requestContext = $router->getContext();
52
53
        foreach ($methods as $method) {
54
            $requestContext->setMethod($method);
55
56
            // Check paths like "/api/user/users.json".
57
            $match = $matcher->match($matchingPath);
58
59
            $this->assertSame($name, $match['_route']);
60
61
            if ($matchingFormat) {
62
                $this->assertSame(ltrim($matchingFormat, '.'), $match['_format']);
63
            }
64
65
            $matchingPathWithStrippedFormat = str_replace('.{_format}', '', $path);
66
67
            // Check paths like "/api/user/users".
68
            $match = $matcher->match($matchingPathWithStrippedFormat);
69
70
            $this->assertSame($name, $match['_route']);
71
72
            if ($matchingFormat) {
73
                $this->assertSame(ltrim($matchingFormat, '.'), $match['_format']);
74
            }
75
        }
76
    }
77
78
    public function getRoutes(): iterable
79
    {
80
        // API
81
        yield ['nelmio_api_doc_index', '/api/doc/{view}', ['GET']];
82
        yield ['sonata_api_media_gallery_get_galleries', '/api/media/galleries.{_format}', ['GET']];
83
        yield ['sonata_api_media_gallery_get_gallery', '/api/media/galleries/{id}.{_format}', ['GET']];
84
        yield ['sonata_api_media_gallery_get_gallery_medias', '/api/media/galleries/{id}/medias.{_format}', ['GET']];
85
        yield ['sonata_api_media_gallery_get_gallery_galleryitems', '/api/media/galleries/{id}/galleryitems.{_format}', ['GET']];
86
        yield ['sonata_api_media_gallery_post_gallery', '/api/media/galleries.{_format}', ['POST']];
87
        yield ['sonata_api_media_gallery_put_gallery', '/api/media/galleries/{id}.{_format}', ['PUT']];
88
        yield ['sonata_api_media_gallery_post_gallery_media_galleryitem', '/api/media/galleries/{galleryId}/media/{mediaId}/galleryitem.{_format}', ['POST']];
89
        yield ['sonata_api_media_gallery_put_gallery_media_galleryitem', '/api/media/galleries/{galleryId}/media/{mediaId}/galleryitem.{_format}', ['PUT']];
90
        yield ['sonata_api_media_gallery_delete_gallery_media_galleryitem', '/api/media/galleries/{galleryId}/media/{mediaId}/galleryitem.{_format}', ['DELETE']];
91
        yield ['sonata_api_media_gallery_delete_gallery', '/api/media/galleries/{id}.{_format}', ['DELETE']];
92
        yield ['sonata_api_media_media_get_media', '/api/media/media.{_format}', ['GET']];
93
        yield ['sonata_api_media_media_get_medium', '/api/media/media/{id}.{_format}', ['GET']];
94
        yield ['sonata_api_media_media_get_medium_formats', '/api/media/media/{id}/formats.{_format}', ['GET']];
95
        yield ['sonata_api_media_media_get_medium_binary', '/api/media/media/{id}/binaries/{format}.{_format}', ['GET']];
96
        yield ['sonata_api_media_media_delete_medium', '/api/media/media/{id}.{_format}', ['DELETE']];
97
        yield ['sonata_api_media_media_put_medium', '/api/media/media/{id}.{_format}', ['PUT']];
98
        yield ['sonata_api_media_media_post_provider_medium', '/api/media/media/providers/{provider}/media.{_format}', ['POST']];
99
        yield ['sonata_api_media_media_put_medium_binary_content', '/api/media/media/{id}/binary/content.{_format}', ['PUT']];
100
    }
101
102
    protected static function getKernelClass(): string
103
    {
104
        return AppKernel::class;
105
    }
106
}
107