Skip to content

Commit be08905

Browse files
committed
Тесты.
1 parent ba6ba2f commit be08905

File tree

4 files changed

+206
-15
lines changed

4 files changed

+206
-15
lines changed

Services/Utils/RouteCheckerExist.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,15 @@ public function check() : void
3737
$allRoutes = $this->routeCollection->all();
3838
foreach ($allRoutes as $nameRoute => $route) {
3939
$controller = $route->getDefault('_controller');
40+
41+
$method = '__invoke';
4042
if (is_string($controller)) {
4143
if (strpos($controller, '::') !== false) {
4244
$callback = explode('::', $controller, 2);
43-
$class = $callback[0];
45+
$class = (string)$callback[0];
46+
$method = (string)$callback[1];
4447
} else {
45-
// _invoke_
48+
// __invoke
4649
$class = $controller;
4750
}
4851

@@ -55,6 +58,17 @@ public function check() : void
5558
)
5659
);
5760
}
61+
62+
if (!method_exists($class, $method)) {
63+
throw new LogicException(
64+
sprintf(
65+
'Class %s declaring as controller for route %s dont have method %s.',
66+
$class,
67+
$nameRoute,
68+
$method
69+
)
70+
);
71+
}
5872
}
5973
}
6074
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?php
2+
3+
namespace Prokl\WpSymfonyRouterBundle\Tests\Cases;
4+
5+
use LogicException;
6+
use Prokl\TestingTools\Base\BaseTestCase;
7+
use Prokl\WpSymfonyRouterBundle\Services\Utils\RouteCheckerExist;
8+
use Symfony\Component\Routing\Route;
9+
use Symfony\Component\Routing\RouteCollection;
10+
11+
/**
12+
* Class RouteCheckerExistTest
13+
* @package Cases
14+
*
15+
* @since 26.05.2021
16+
*/
17+
class RouteCheckerExistTest extends BaseTestCase
18+
{
19+
/**
20+
* @var RouteCheckerExist $obTestObject
21+
*/
22+
protected $obTestObject;
23+
24+
/**
25+
* @return void
26+
*
27+
* @internal Если все OK, то не будет выброшено исключение.
28+
*/
29+
public function testCheck() : void
30+
{
31+
$this->obTestObject = new RouteCheckerExist(
32+
$this->getRouteCollection(
33+
'Prokl\WpSymfonyRouterBundle\Tests\Fixture\ExampleSimpleController::action'
34+
)
35+
);
36+
37+
$this->obTestObject->check();
38+
39+
$this->assertTrue(true);
40+
}
41+
42+
/**
43+
* Invalid action.
44+
*
45+
* @return void
46+
*
47+
* @internal Если все OK, то не будет выброшено исключение.
48+
*/
49+
public function testCheckInvalidAction() : void
50+
{
51+
$this->expectException(LogicException::class);
52+
$this->expectExceptionMessage(
53+
'Class Prokl\WpSymfonyRouterBundle\Tests\Fixture\ExampleSimpleController declaring as controller for route test_route dont have method fake.'
54+
);
55+
56+
$this->obTestObject = new RouteCheckerExist(
57+
$this->getRouteCollection(
58+
'Prokl\WpSymfonyRouterBundle\Tests\Fixture\ExampleSimpleController::fake'
59+
)
60+
);
61+
62+
$this->obTestObject->check();
63+
}
64+
65+
/**
66+
* Несуществующий контроллер.
67+
*
68+
* @return void
69+
*
70+
* @internal Если все OK, то не будет выброшено исключение.
71+
*/
72+
public function testCheckInvalidController() : void
73+
{
74+
$this->obTestObject = new RouteCheckerExist(
75+
$this->getRouteCollection(
76+
'Prokl\WpSymfonyRouterBundle\Tests\Fixture\Unexists::action'
77+
)
78+
);
79+
80+
$this->expectException(LogicException::class);
81+
$this->expectExceptionMessage(
82+
'Class Prokl\WpSymfonyRouterBundle\Tests\Fixture\Unexists declaring as controller for route test_route not exists.'
83+
);
84+
$this->obTestObject->check();
85+
}
86+
87+
/**
88+
* Invokable контроллер.
89+
*
90+
* @return void
91+
*
92+
* @internal Если все OK, то не будет выброшено исключение.
93+
*/
94+
public function testCheckInvokable() : void
95+
{
96+
97+
$this->obTestObject = new RouteCheckerExist(
98+
$this->getRouteCollection(
99+
'Prokl\WpSymfonyRouterBundle\Tests\Fixture\InvokableController'
100+
)
101+
);
102+
103+
$this->obTestObject->check();
104+
105+
$this->assertTrue(true);
106+
}
107+
108+
/**
109+
* Invokable контроллер. Без метода __invoke.
110+
*
111+
* @return void
112+
*
113+
* @internal Если все OK, то не будет выброшено исключение.
114+
*/
115+
public function testCheckInvokableInvalid() : void
116+
{
117+
$this->obTestObject = new RouteCheckerExist(
118+
$this->getRouteCollection(
119+
'Prokl\WpSymfonyRouterBundle\Tests\Fixture\ExampleSimpleController'
120+
)
121+
);
122+
123+
$this->expectException(LogicException::class);
124+
$this->expectExceptionMessage(
125+
'Class Prokl\WpSymfonyRouterBundle\Tests\Fixture\ExampleSimpleController declaring as controller for route test_route dont have method __invoke.'
126+
);
127+
128+
$this->obTestObject->check();
129+
}
130+
131+
/**
132+
* RouteCollection.
133+
*
134+
* @param string $controllerString Контроллер + action.
135+
*
136+
* @return RouteCollection
137+
*/
138+
private function getRouteCollection(
139+
string $controllerString
140+
) : RouteCollection {
141+
$collection = new RouteCollection();
142+
$collection->add(
143+
'test_route',
144+
new Route(
145+
'/test/route',
146+
[
147+
'_controller' => $controllerString
148+
]
149+
)
150+
);
151+
152+
return $collection;
153+
}
154+
}

Tests/Cases/RoutingTest.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
namespace Prokl\WpSymfonyRouterBundle\Tests\Cases;
44

55
use Faker\Factory;
6-
use Faker\Generator;
76
use Prokl\WpSymfonyRouterBundle\Services\Utils\RouteChecker;
8-
use PHPUnit\Framework\TestCase;
97
use Prokl\WpSymfonyRouterBundle\Tests\Fixture\ExampleSimpleController;
8+
use Prokl\WpSymfonyRouterBundle\Tests\Tools\ContainerAwareBaseTestCase;
109
use Symfony\Component\HttpKernel\Controller\ControllerReference;
1110
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1211
use Symfony\Component\Routing\RequestContext;
@@ -21,7 +20,7 @@
2120
* @since 01.12.2020
2221
* @since 24.12.2020 Актуализация.
2322
*/
24-
class RoutingTest extends TestCase
23+
class RoutingTest extends ContainerAwareBaseTestCase
2524
{
2625
private const TEST_ROUTE = '/api/testing/';
2726

@@ -30,28 +29,22 @@ class RoutingTest extends TestCase
3029
*/
3130
protected $obTestObject;
3231

33-
private $containerSymfony;
34-
3532
/**
36-
* @var Generator | null $faker
33+
* @inheritDoc
3734
*/
38-
protected $faker;
39-
4035
protected function setUp(): void
4136
{
42-
$this->containerSymfony = container();
43-
4437
parent::setUp();
4538

4639
$this->faker = Factory::create();
4740

4841
$this->obTestObject = new RouteChecker(
4942
$this->getRouteCollection(),
50-
$this->containerSymfony->get('global.request'),
43+
$this->container->get('global.request'),
5144
new RequestContext(
5245
'',
5346
'GET',
54-
$this->containerSymfony->getParameter('local.http.host')
47+
$this->container->getParameter('local.http.host')
5548
)
5649
);
5750
}
@@ -232,7 +225,7 @@ private function getRouteCollection() : RouteCollection
232225
['_controller' => ExampleSimpleController::class, 'id' => $this->faker->numberBetween(100, 200)]
233226
);
234227

235-
$route->setHost($this->containerSymfony->getParameter('kernel.http.host'));
228+
$route->setHost($this->container->getParameter('kernel.http.host'));
236229

237230
$routeCollection = new RouteCollection();
238231

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Prokl\WpSymfonyRouterBundle\Tests\Fixture;
4+
5+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
6+
use Symfony\Component\HttpFoundation\Request;
7+
use Symfony\Component\HttpFoundation\Response;
8+
9+
/**
10+
* Class InvokableController
11+
* @package Prokl\WpSymfonyRouterBundle\Tests\Fixture
12+
*
13+
* @since 21.10.2020
14+
*/
15+
class InvokableController extends AbstractController
16+
{
17+
public function __invoke(Request $request, int $id): Response
18+
{
19+
return new Response(
20+
'OK'
21+
);
22+
}
23+
24+
public function action2(Request $request): Response
25+
{
26+
return new Response(
27+
'OK'
28+
);
29+
}
30+
}

0 commit comments

Comments
 (0)