Skip to content

Commit 00159c0

Browse files
authored
Merge pull request #3 from chimeraphp/allow-mapping-request-handlers
Implement annotation for endpoints with no behaviour
2 parents 2900317 + fdbae71 commit 00159c0

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed

src/Routing/SimpleEndpoint.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Chimera\Mapping\Routing;
5+
6+
use Chimera\Mapping\Validator;
7+
8+
/**
9+
* @Annotation
10+
* @Target({"CLASS"})
11+
*/
12+
final class SimpleEndpoint extends Endpoint
13+
{
14+
/**
15+
* {@inheritdoc}
16+
*/
17+
protected function validateAdditionalData(Validator $validator): void
18+
{
19+
}
20+
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
protected function defaultMethods(): array
25+
{
26+
return ['GET'];
27+
}
28+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Chimera\Mapping\Tests\Functional\Routing;
5+
6+
use Chimera\Mapping\Routing\SimpleEndpoint;
7+
use Chimera\Mapping\Tests\Functional\TestCase;
8+
use Doctrine\Common\Annotations\AnnotationException;
9+
use function assert;
10+
11+
final class SimpleEndpointTest extends TestCase
12+
{
13+
/**
14+
* @test
15+
*
16+
* @covers \Chimera\Mapping\Routing\SimpleEndpoint
17+
* @covers \Chimera\Mapping\Routing\SimpleEndpoint
18+
* @covers \Chimera\Mapping\Reader
19+
*/
20+
public function defaultValueShouldBeConfiguredProperly(): void
21+
{
22+
$annotation = $this->readAnnotation(FetchBookRequestHandler::class, SimpleEndpoint::class);
23+
assert($annotation instanceof SimpleEndpoint || $annotation === null);
24+
25+
self::assertInstanceOf(SimpleEndpoint::class, $annotation);
26+
self::assertSame('/books/{id}', $annotation->path);
27+
self::assertSame(['GET'], $annotation->methods);
28+
self::assertSame('books.fetch', $annotation->name);
29+
self::assertNull($annotation->app);
30+
}
31+
32+
/**
33+
* @test
34+
*
35+
* @covers \Chimera\Mapping\Routing\SimpleEndpoint
36+
* @covers \Chimera\Mapping\Routing\SimpleEndpoint
37+
* @covers \Chimera\Mapping\Reader
38+
*/
39+
public function propertiesShouldBeConfiguredProperly(): void
40+
{
41+
$annotation = $this->readAnnotation(FindBooksRequestHandler::class, SimpleEndpoint::class);
42+
assert($annotation instanceof SimpleEndpoint || $annotation === null);
43+
44+
self::assertInstanceOf(SimpleEndpoint::class, $annotation);
45+
self::assertSame('/books', $annotation->path);
46+
self::assertSame(['GET'], $annotation->methods);
47+
self::assertSame('books.find', $annotation->name);
48+
self::assertSame('my-app', $annotation->app);
49+
}
50+
51+
/**
52+
* @test
53+
*
54+
* @covers \Chimera\Mapping\Routing\SimpleEndpoint
55+
* @covers \Chimera\Mapping\Routing\SimpleEndpoint
56+
* @covers \Chimera\Mapping\Reader
57+
*/
58+
public function exceptionShouldBeRaisedWhenRequiredPropertiesAreMissing(): void
59+
{
60+
$this->expectException(AnnotationException::class);
61+
$this->readAnnotation(FindAuthorsRequestHandler::class, SimpleEndpoint::class);
62+
}
63+
}
64+
65+
/**
66+
* @SimpleEndpoint("/books/{id}", name="books.fetch")
67+
*/
68+
final class FetchBookRequestHandler
69+
{
70+
}
71+
72+
/**
73+
* @SimpleEndpoint(path="/books", name="books.find", app="my-app")
74+
*/
75+
final class FindBooksRequestHandler
76+
{
77+
}
78+
79+
/**
80+
* @SimpleEndpoint
81+
*/
82+
final class FindAuthorsRequestHandler
83+
{
84+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Chimera\Mapping\Tests\Unit\Routing;
5+
6+
use Chimera\Mapping\Routing\SimpleEndpoint;
7+
use PHPUnit\Framework\TestCase;
8+
9+
/**
10+
* @coversDefaultClass \Chimera\Mapping\Routing\SimpleEndpoint
11+
*/
12+
final class SimpleEndpointTest extends TestCase
13+
{
14+
private const ENDPOINT_DATA = ['path' => '/tests', 'name' => 'test'];
15+
16+
/**
17+
* @test
18+
*
19+
* @covers ::__construct()
20+
* @covers ::validateAdditionalData()
21+
* @covers ::defaultMethods()
22+
* @covers \Chimera\Mapping\Validator
23+
* @covers \Chimera\Mapping\Routing\Endpoint
24+
*/
25+
public function validateShouldNotRaiseExceptionsWhenStateIsValid(): void
26+
{
27+
$annotation = new SimpleEndpoint(self::ENDPOINT_DATA);
28+
$annotation->validate('class A');
29+
30+
self::assertSame(['GET'], $annotation->methods);
31+
}
32+
}

0 commit comments

Comments
 (0)