From 078c9c05c53d6e05fd8d692682d14e8a9aef9e42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20B=C3=BCttner?= Date: Wed, 12 Aug 2020 16:53:55 +0200 Subject: [PATCH] introducing library to handle version+name (#9) --- src/Implementations/Composer.php | 26 ----------- src/Implementations/Controller.php | 44 ++++--------------- src/Implementations/DefaultResponse.php | 2 +- .../NoValueConversionMerger.php | 22 ++-------- src/Implementations/PhalconPath2PathArray.php | 2 +- src/Implementations/Reflector.php | 6 +-- src/Interfaces/Controller.php | 8 +--- src/Interfaces/Path2PathConverter.php | 4 +- .../PathTargetAnnotationResolver.php | 4 +- src/Interfaces/RecursiveMerger.php | 4 +- src/ServiceProvider.php | 18 +++----- test/ComposerTest.php | 25 ----------- test/ControllerTest.php | 34 +++++--------- 13 files changed, 42 insertions(+), 157 deletions(-) delete mode 100644 src/Implementations/Composer.php delete mode 100644 test/ComposerTest.php diff --git a/src/Implementations/Composer.php b/src/Implementations/Composer.php deleted file mode 100644 index 2e59c01..0000000 --- a/src/Implementations/Composer.php +++ /dev/null @@ -1,26 +0,0 @@ - "3.0.1", - "info"=> [ - "title"=> "unknown", - "version"=> "1.0.0" - ] - ]; - /** * Generates an overview over routes registered * @return-200 application/json {"type":"object"} @@ -38,17 +23,16 @@ public function index(): ResponseInterface foreach ($this->router->getRoutes() as $route) { $paths[] = $converter->convert($route); } - $merger = $this->di->get(RecursiveMerger::class); return $this ->getCorsEnabledResponse() ->setJsonContent( - $merger->merge( - self::$body, - [ - 'paths' => $merger->mergeAll(...$paths), - 'info' => (new Composer())(dirname(__DIR__, 5).'/composer.json') + [ + 'paths' => $this->di->get(RMI::class)->mergeAll(...$paths), + 'info' => [ + "title"=> Versions::ROOT_PACKAGE_NAME, + "version"=> Versions::getVersion(Versions::ROOT_PACKAGE_NAME) ] - ) + ] ); } @@ -96,14 +80,4 @@ private function getCorsEnabledResponse(): ResponseInterface ) ->setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS'); } - - /** - * @param string $root - * @return ControllerInterface - */ - public function setRoot(string $root): ControllerInterface - { - $this->root = $root; - return $this; - } } diff --git a/src/Implementations/DefaultResponse.php b/src/Implementations/DefaultResponse.php index 2d26b94..a43829f 100644 --- a/src/Implementations/DefaultResponse.php +++ b/src/Implementations/DefaultResponse.php @@ -1,4 +1,4 @@ -checkArray(array_shift($sets), 1); + $initial = array_shift($sets); foreach ($sets as $pos => $set) { - $initial = $this->merge($initial, $this->checkArray($set, $pos+2)); + $initial = $this->merge($initial, $set); } return $initial; } - - /** - * @param mixed $set - * @param int $num - * @throws InvalidArgumentException - * @return array - */ - private function checkArray($set, int $num): array - { - if (!is_array($set)) { - throw new InvalidArgumentException("Set #$num is not an array, but a(n) " . gettype($set) . '.'); - } - return $set; - } } diff --git a/src/Implementations/PhalconPath2PathArray.php b/src/Implementations/PhalconPath2PathArray.php index 151afd4..8debc89 100644 --- a/src/Implementations/PhalconPath2PathArray.php +++ b/src/Implementations/PhalconPath2PathArray.php @@ -1,4 +1,4 @@ -cache[$class])) { - $this->cache[$class]['____class'] = new ReflectionClass($class); + $this->cache[$class]['#'] = new ReflectionClass($class); } if (!isset($this->cache[$class][$method])) { $this->cache[$class][$method] = DefaultResponse::add( - $this->getReflect($this->cache[$class]['____class'], $method) + $this->getReflect($this->cache[$class]['#'], $method) ); } } catch (Exception $e) { diff --git a/src/Interfaces/Controller.php b/src/Interfaces/Controller.php index 6e9e81a..6b14da1 100644 --- a/src/Interfaces/Controller.php +++ b/src/Interfaces/Controller.php @@ -1,4 +1,4 @@ -apiRoot = ($apiRoot{0}==='/' ? '' : '/') . $apiRoot; - } - /** * Registers controller at api-root * @param DiInterface $serviceContainer @@ -33,7 +25,7 @@ public function __construct(string $apiRoot = '/') */ public function register(DiInterface $serviceContainer) { - $this->registerServices($serviceContainer, $this->apiRoot); + $this->registerServices($serviceContainer); $this->registerRoutes($serviceContainer->get('router')); } @@ -42,10 +34,10 @@ public function register(DiInterface $serviceContainer) * @param string $root * @return void */ - private function registerServices(DiInterface $serviceContainer, string $root) + private function registerServices(DiInterface $serviceContainer) { - $serviceContainer->set(Controller::class, function () use ($root) { - return (new ControllerImplementation())->setRoot($root); + $serviceContainer->set(Controller::class, function () { + return (new ControllerImplementation()); }); $serviceContainer->set(Path2PathConverter::class, function () use (&$serviceContainer) { return new PhalconPath2PathArray( diff --git a/test/ComposerTest.php b/test/ComposerTest.php deleted file mode 100644 index af3ecde..0000000 --- a/test/ComposerTest.php +++ /dev/null @@ -1,25 +0,0 @@ - "idrinth/phalcon-routes2openapi", - "description" => "Generates an JSON represantation of the routes " - . "registered via phalcon in an OpenAPI-compatible way." - ], - (new Composer())(dirname(__DIR__).'/composer.json') - ); - } -} diff --git a/test/ControllerTest.php b/test/ControllerTest.php index 69d62ae..0f2f00d 100644 --- a/test/ControllerTest.php +++ b/test/ControllerTest.php @@ -36,7 +36,6 @@ public function provideIndex(): array return [ [ $this->buildRouterMock(), - '/', [ "openapi" => "3.0.1", "info" => [ @@ -52,7 +51,6 @@ public function provideIndex(): array ], [ $this->buildRouterMock(), - '/hi/', [ "openapi" => "3.0.1", "info" => [ @@ -76,11 +74,9 @@ public function provideOptions(): array { return [ [ - '/', '', ], [ - '/', 'http://abc.de', ], ]; @@ -88,14 +84,12 @@ public function provideOptions(): array /** * @param DiInterface $serviceContainer - * @param string $root * @return Controller */ - private function getBasePreparedInstance(DiInterface $serviceContainer, string $root): Controller + private function getBasePreparedInstance(DiInterface $serviceContainer): Controller { $instance = new Controller(); $instance->request = $this->getMockBuilder(RequestInterface::class)->getMock(); - $instance->setRoot($root); $instance->setDI($serviceContainer); $instance->response = $this->getMockBuilder(ResponseInterface::class)->getMock(); $instance->response->expects(static::exactly(2)) @@ -106,11 +100,10 @@ private function getBasePreparedInstance(DiInterface $serviceContainer, string $ /** * @param RouterInterface $router - * @param string $root * @param array $result * @return Controller */ - private function getPreparedInstance(RouterInterface $router, string $root, array $result): Controller + private function getPreparedInstance(RouterInterface $router, array $result): Controller { $serviceContainer = $this->getMockBuilder(DiInterface::class)->getMock(); $p2p = $this->getMockBuilder(Path2PathConverter::class)->getMock(); @@ -157,7 +150,7 @@ private function getPreparedInstance(RouterInterface $router, string $root, arra [RecursiveMerger::class] ) ->willReturnOnConsecutiveCalls($p2p, $merger); - $instance = $this->getBasePreparedInstance($serviceContainer, $root); + $instance = $this->getBasePreparedInstance($serviceContainer); $instance->router = $router; $instance->response->expects(static::once()) ->method('setJsonContent') @@ -170,13 +163,12 @@ private function getPreparedInstance(RouterInterface $router, string $root, arra * @test * @dataProvider provideIndex * @param RouterInterface $router - * @param string $root * @param array $result * @return void */ - public function testIndex(RouterInterface $router, string $root, array $result) + public function testIndex(RouterInterface $router, array $result) { - $instance = $this->getPreparedInstance($router, $root, $result); + $instance = $this->getPreparedInstance($router, $result); static::assertSame($instance->response, $instance->index()); } @@ -184,13 +176,12 @@ public function testIndex(RouterInterface $router, string $root, array $result) * @test * @dataProvider provideIndex * @param RouterInterface $router - * @param string $root * @param array $result * @return void */ - public function testIndexAction(RouterInterface $router, string $root, array $result) + public function testIndexAction(RouterInterface $router, array $result) { - $instance = $this->getPreparedInstance($router, $root, $result); + $instance = $this->getPreparedInstance($router, $result); static::assertSame($instance->response, $instance->indexAction()); } @@ -220,16 +211,16 @@ private function prepareInstanceForOptions(Controller $instance, string $origin) ) ->willReturnSelf(); } + /** * @test * @dataProvider provideOptions - * @param string $root * @param string $origin * @return void */ - public function testOptions(string $root, string $origin) + public function testOptions(string $origin) { - $instance = $this->getBasePreparedInstance($this->getMockBuilder(DiInterface::class)->getMock(), $root); + $instance = $this->getBasePreparedInstance($this->getMockBuilder(DiInterface::class)->getMock()); $this->prepareInstanceForOptions($instance, $origin); static::assertSame($instance->response, $instance->options()); } @@ -237,13 +228,12 @@ public function testOptions(string $root, string $origin) /** * @test * @dataProvider provideOptions - * @param string $root * @param string $origin * @return void */ - public function testOptionsAction(string $root, string $origin) + public function testOptionsAction(string $origin) { - $instance = $this->getBasePreparedInstance($this->getMockBuilder(DiInterface::class)->getMock(), $root); + $instance = $this->getBasePreparedInstance($this->getMockBuilder(DiInterface::class)->getMock()); $this->prepareInstanceForOptions($instance, $origin); static::assertSame($instance->response, $instance->optionsAction()); }