From fdbae71f47aec5cd22a4fe89bfb03a074eb3f9ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Cobucci?= Date: Wed, 27 Feb 2019 11:47:19 +0100 Subject: [PATCH] Implement annotation for endpoints with no behaviour Classes with this annotations are going to be mapped directly as handlers of the HTTP application, without intermediate component. --- src/Routing/SimpleEndpoint.php | 28 +++++++ .../Functional/Routing/SimpleEndpointTest.php | 84 +++++++++++++++++++ tests/Unit/Routing/SimpleEndpointTest.php | 32 +++++++ 3 files changed, 144 insertions(+) create mode 100644 src/Routing/SimpleEndpoint.php create mode 100644 tests/Functional/Routing/SimpleEndpointTest.php create mode 100644 tests/Unit/Routing/SimpleEndpointTest.php diff --git a/src/Routing/SimpleEndpoint.php b/src/Routing/SimpleEndpoint.php new file mode 100644 index 0000000..089bb13 --- /dev/null +++ b/src/Routing/SimpleEndpoint.php @@ -0,0 +1,28 @@ +readAnnotation(FetchBookRequestHandler::class, SimpleEndpoint::class); + assert($annotation instanceof SimpleEndpoint || $annotation === null); + + self::assertInstanceOf(SimpleEndpoint::class, $annotation); + self::assertSame('/books/{id}', $annotation->path); + self::assertSame(['GET'], $annotation->methods); + self::assertSame('books.fetch', $annotation->name); + self::assertNull($annotation->app); + } + + /** + * @test + * + * @covers \Chimera\Mapping\Routing\SimpleEndpoint + * @covers \Chimera\Mapping\Routing\SimpleEndpoint + * @covers \Chimera\Mapping\Reader + */ + public function propertiesShouldBeConfiguredProperly(): void + { + $annotation = $this->readAnnotation(FindBooksRequestHandler::class, SimpleEndpoint::class); + assert($annotation instanceof SimpleEndpoint || $annotation === null); + + self::assertInstanceOf(SimpleEndpoint::class, $annotation); + self::assertSame('/books', $annotation->path); + self::assertSame(['GET'], $annotation->methods); + self::assertSame('books.find', $annotation->name); + self::assertSame('my-app', $annotation->app); + } + + /** + * @test + * + * @covers \Chimera\Mapping\Routing\SimpleEndpoint + * @covers \Chimera\Mapping\Routing\SimpleEndpoint + * @covers \Chimera\Mapping\Reader + */ + public function exceptionShouldBeRaisedWhenRequiredPropertiesAreMissing(): void + { + $this->expectException(AnnotationException::class); + $this->readAnnotation(FindAuthorsRequestHandler::class, SimpleEndpoint::class); + } +} + +/** + * @SimpleEndpoint("/books/{id}", name="books.fetch") +*/ +final class FetchBookRequestHandler +{ +} + +/** + * @SimpleEndpoint(path="/books", name="books.find", app="my-app") + */ +final class FindBooksRequestHandler +{ +} + +/** + * @SimpleEndpoint + */ +final class FindAuthorsRequestHandler +{ +} diff --git a/tests/Unit/Routing/SimpleEndpointTest.php b/tests/Unit/Routing/SimpleEndpointTest.php new file mode 100644 index 0000000..88c908a --- /dev/null +++ b/tests/Unit/Routing/SimpleEndpointTest.php @@ -0,0 +1,32 @@ + '/tests', 'name' => 'test']; + + /** + * @test + * + * @covers ::__construct() + * @covers ::validateAdditionalData() + * @covers ::defaultMethods() + * @covers \Chimera\Mapping\Validator + * @covers \Chimera\Mapping\Routing\Endpoint + */ + public function validateShouldNotRaiseExceptionsWhenStateIsValid(): void + { + $annotation = new SimpleEndpoint(self::ENDPOINT_DATA); + $annotation->validate('class A'); + + self::assertSame(['GET'], $annotation->methods); + } +}