Passed
Push — master ( 61ebc4...c33ad8 )
by Pavel
17:39
created

RoutingTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 6
dl 0
loc 47
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testEntityRouting() 0 15 1
B matchPath() 0 26 5
1
<?php
2
3
namespace ScayTrase\Api\Cruds\Tests;
4
5
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
7
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
8
use Symfony\Component\Routing\RequestContext;
9
10
class RoutingTest extends WebTestCase
11
{
12
    use CrudsTestCaseTrait;
13
14
    public function testEntityRouting()
15
    {
16
        self::bootKernel();
17
18
        $this->matchPath('/api/entity/my-entity/create', 'POST');
19
        $this->matchPath('/api/entity/my-entity/create', 'GET', true);
20
        $this->matchPath('/api/entity/my-entity/read', 'GET');
21
        $this->matchPath('/api/entity/my-entity/read', 'POST', true);
22
        $this->matchPath('/api/entity/my-entity/update', 'POST');
23
        $this->matchPath('/api/entity/my-entity/update', 'GET', true);
24
        $this->matchPath('/api/entity/my-entity/delete', 'POST');
25
        $this->matchPath('/api/entity/my-entity/delete', 'GET', true);
26
        $this->matchPath('/api/entity/my-entity/search', 'GET');
27
        $this->matchPath('/api/entity/my-entity/search', 'POST');
28
    }
29
30
    private function matchPath($path, $method = 'GET', $catch = false)
31
    {
32
        $container = self::$kernel->getContainer();
33
        $router    = $container->get('router');
34
35
        $context = new RequestContext('', $method);
36
        $router->getMatcher()->setContext($context);
37
38
        try {
39
            $router->match($path);
40
        } catch (MethodNotAllowedException $exception) {
41
            if (!$catch) {
42
                self::fail(
43
                    sprintf(
44
                        'Method %s not allowed. Allowed methods are: %s',
45
                        $context->getMethod(),
46
                        implode(', ', $exception->getAllowedMethods())
47
                    )
48
                );
49
            }
50
        } catch (ResourceNotFoundException $exception) {
51
            if (!$catch) {
52
                self::fail(sprintf('Resource not found: %s', $path));
53
            }
54
        }
55
    }
56
}
57