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
|
|
|
|