|
| 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 | +} |
0 commit comments