Skip to content

Commit

Permalink
Merge pull request #3 from chimeraphp/allow-mapping-request-handlers
Browse files Browse the repository at this point in the history
Implement annotation for endpoints with no behaviour
  • Loading branch information
lcobucci authored Feb 27, 2019
2 parents 2900317 + fdbae71 commit 00159c0
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/Routing/SimpleEndpoint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);

namespace Chimera\Mapping\Routing;

use Chimera\Mapping\Validator;

/**
* @Annotation
* @Target({"CLASS"})
*/
final class SimpleEndpoint extends Endpoint
{
/**
* {@inheritdoc}
*/
protected function validateAdditionalData(Validator $validator): void
{
}

/**
* {@inheritdoc}
*/
protected function defaultMethods(): array
{
return ['GET'];
}
}
84 changes: 84 additions & 0 deletions tests/Functional/Routing/SimpleEndpointTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
declare(strict_types=1);

namespace Chimera\Mapping\Tests\Functional\Routing;

use Chimera\Mapping\Routing\SimpleEndpoint;
use Chimera\Mapping\Tests\Functional\TestCase;
use Doctrine\Common\Annotations\AnnotationException;
use function assert;

final class SimpleEndpointTest extends TestCase
{
/**
* @test
*
* @covers \Chimera\Mapping\Routing\SimpleEndpoint
* @covers \Chimera\Mapping\Routing\SimpleEndpoint
* @covers \Chimera\Mapping\Reader
*/
public function defaultValueShouldBeConfiguredProperly(): void
{
$annotation = $this->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
{
}
32 changes: 32 additions & 0 deletions tests/Unit/Routing/SimpleEndpointTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);

namespace Chimera\Mapping\Tests\Unit\Routing;

use Chimera\Mapping\Routing\SimpleEndpoint;
use PHPUnit\Framework\TestCase;

/**
* @coversDefaultClass \Chimera\Mapping\Routing\SimpleEndpoint
*/
final class SimpleEndpointTest extends TestCase
{
private const ENDPOINT_DATA = ['path' => '/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);
}
}

0 comments on commit 00159c0

Please sign in to comment.