Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/Core/Schema/Serialization/ArraySerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private function serializeEndpoint(Controller $controller, Method $method): arra
*/
private function serializeInit(Controller $controller, Method $method): array
{
// Build full mask (Group @Path(s) + Controller @Path + Endpoint @Path)
// Build full mask (Group #[Path](s) + Controller #[Path] + Endpoint #[Path])
// without duplicated slashes (//)
// and without trailing slash at the end
// but with slash at the beginning
Expand All @@ -71,8 +71,8 @@ private function serializeInit(Controller $controller, Method $method): array
$mask = Helpers::slashless($mask);
$mask = '/' . trim($mask, '/');

// Build full id (@GroupId(s) + @ControllerId + @Id)
// If @Id is empty, then fullid is also empty
// Build full id (Group #[Id](s) + Controller #[Id] + #[Id])
// If #[Id] is empty, then full id is also empty
if ($method->getId() === null || $method->getId() === '') {
$id = null;
} else {
Expand Down Expand Up @@ -142,15 +142,15 @@ private function serializePattern(array &$endpoint, Controller $controller, Meth
return $pattern;
});

// Check if @RequestParameter(in=path) is also defined in mask
// Check if #[RequestParameter(in=path)] is also defined in mask
foreach ($pathParameters as $parameter) {
foreach ($maskParameters as $maskParameter) {
if ($maskParameter['name'] === $parameter->getName()) {
continue 2;
}
}

throw new InvalidStateException(sprintf('@RequestParameter(name="%s", in=path) is not defined in mask (@Path annotations)', $parameter->getName()));
throw new InvalidStateException(sprintf('#[RequestParameter(name="%s", in=path)] is not defined in mask (#[Path] annotations)', $parameter->getName()));
}

// Fulfill endpoint parameters (in path)
Expand Down
8 changes: 4 additions & 4 deletions src/Core/Schema/Validation/ControllerPathValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected function validateSlashes(SchemaBuilder $builder): void

if ($path === '') {
throw new InvalidSchemaException(
sprintf('@Path in "%s" must be set.', $controller->getClass())
sprintf('#[Path] in "%s" must be set.', $controller->getClass())
);
}

Expand All @@ -35,14 +35,14 @@ protected function validateSlashes(SchemaBuilder $builder): void
// MUST: Starts with slash (/)
if (!str_starts_with($path, '/')) {
throw new InvalidSchemaException(
sprintf('@Path "%s" in "%s" must starts with "/" (slash).', $path, $controller->getClass())
sprintf('#[Path] "%s" in "%s" must starts with "/" (slash).', $path, $controller->getClass())
);
}

// MUST NOT: Ends with slash (/)
if (str_ends_with($path, '/')) {
throw new InvalidSchemaException(
sprintf('@Path "%s" in "%s" must not ends with "/" (slash).', $path, $controller->getClass())
sprintf('#[Path] "%s" in "%s" must not ends with "/" (slash).', $path, $controller->getClass())
);
}
}
Expand All @@ -65,7 +65,7 @@ protected function validateRegex(SchemaBuilder $builder): void
if ($match !== null) {
throw new InvalidSchemaException(
sprintf(
'@Path "%s" in "%s" contains illegal characters "%s". Allowed characters are only [a-zA-Z0-9-_/].',
'#[Path] "%s" in "%s" contains illegal characters "%s". Allowed characters are only [a-zA-Z0-9-_/].',
$path,
$controller->getClass(),
$match[1]
Expand Down
10 changes: 5 additions & 5 deletions src/Core/Schema/Validation/GroupPathValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ protected function validateSlashes(SchemaBuilder $builder): void
if ($groupPath === '/') {
// INVALID: nonsense
throw new InvalidSchemaException(
sprintf('@Path "%s" in "%s" cannot be only "/", it is nonsense.', $groupPath, $controller->getClass())
sprintf('#[Path] "%s" in "%s" cannot be only "/", it is nonsense.', $groupPath, $controller->getClass())
);
}

// MUST: Starts with slash (/)
if (!str_starts_with($groupPath, '/')) {
throw new InvalidSchemaException(
sprintf('@Path "%s" in "%s" must starts with "/" (slash).', $groupPath, $controller->getClass())
sprintf('#[Path] "%s" in "%s" must starts with "/" (slash).', $groupPath, $controller->getClass())
);
}

// MUST NOT: Ends with slash (/)
if (str_ends_with($groupPath, '/')) {
throw new InvalidSchemaException(
sprintf('@Path "%s" in "%s" must not ends with "/" (slash).', $groupPath, $controller->getClass())
sprintf('#[Path] "%s" in "%s" must not ends with "/" (slash).', $groupPath, $controller->getClass())
);
}
}
Expand All @@ -63,7 +63,7 @@ protected function validateRegex(SchemaBuilder $builder): void
if ($match !== null) {
throw new InvalidSchemaException(
sprintf(
'@Path "%s" in "%s" contains illegal characters "%s". Allowed characters are only [a-zA-Z0-9-_/{}].',
'#[Path] "%s" in "%s" contains illegal characters "%s". Allowed characters are only [a-zA-Z0-9-_/{}].',
$path,
$controller->getClass(),
$match[1]
Expand All @@ -86,7 +86,7 @@ protected function validateRegex(SchemaBuilder $builder): void
if ($match !== null) {
throw (new InvalidSchemaException(
sprintf(
'@Path "%s" in "%s" contains illegal characters "%s" in parameter. Allowed characters in parameter are only {[a-z-A-Z0-9-_]+}',
'#[Path] "%s" in "%s" contains illegal characters "%s" in parameter. Allowed characters in parameter are only {[a-z-A-Z0-9-_]+}',
$path,
$controller->getClass(),
$match[1]
Expand Down
10 changes: 5 additions & 5 deletions src/Core/Schema/Validation/IdValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected function validateDuplicities(SchemaBuilder $builder): void

foreach ($controllers as $controller) {
foreach ($controller->getMethods() as $method) {
// Skip if @Id is not set
// Skip if #[Id] is not set
if ($method->getId() === null || $method->getId() === '') {
continue;
}
Expand All @@ -33,11 +33,11 @@ protected function validateDuplicities(SchemaBuilder $builder): void
[$method->getId()]
));

// If this @GroupId(s).@ControllerId.@Id exists, throw an exception
// If this Group #[Id](s).Controller #[Id].#[Id] exists, throw an exception
if (isset($ids[$fullid])) {
throw new InvalidSchemaException(
sprintf(
'Duplicate @Id "%s" in "%s::%s()" and "%s::%s()"',
'Duplicate #[Id] "%s" in "%s::%s()" and "%s::%s()"',
$fullid,
$controller->getClass(),
$method->getName(),
Expand All @@ -58,7 +58,7 @@ protected function validateRegex(SchemaBuilder $builder): void

foreach ($controllers as $controller) {
foreach ($controller->getMethods() as $method) {
// Skip if @Id is not set
// Skip if #[Id] is not set
if ($method->getId() === null || $method->getId() === '') {
continue;
}
Expand All @@ -75,7 +75,7 @@ protected function validateRegex(SchemaBuilder $builder): void
if ($match !== null) {
throw new InvalidSchemaException(
sprintf(
'@Id "%s" in "%s::%s()" contains illegal characters "%s". Allowed characters are only [a-zA-Z0-9_].',
'#[Id] "%s" in "%s::%s()" contains illegal characters "%s". Allowed characters are only [a-zA-Z0-9_].',
$id,
$controller->getClass(),
$method->getName(),
Expand Down
10 changes: 5 additions & 5 deletions src/Core/Schema/Validation/PathValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected function validateRequirements(SchemaBuilder $builder): void
if ($method->getPath() === '') {
throw (new InvalidSchemaException(
sprintf(
'"%s::%s()" has empty @Path.',
'"%s::%s()" has empty #[Path].',
$controller->getClass(),
$method->getName()
)
Expand All @@ -49,7 +49,7 @@ protected function validateSlashes(SchemaBuilder $builder): void
if (!str_starts_with($path, '/')) {
throw (new InvalidSchemaException(
sprintf(
'@Path "%s" in "%s::%s()" must starts with "/" (slash).',
'#[Path] "%s" in "%s::%s()" must starts with "/" (slash).',
$path,
$controller->getClass(),
$method->getName()
Expand All @@ -63,7 +63,7 @@ protected function validateSlashes(SchemaBuilder $builder): void
if (str_ends_with($path, '/') && strlen($path) > 1) {
throw (new InvalidSchemaException(
sprintf(
'@Path "%s" in "%s::%s()" must not ends with "/" (slash).',
'#[Path] "%s" in "%s::%s()" must not ends with "/" (slash).',
$path,
$controller->getClass(),
$method->getName()
Expand Down Expand Up @@ -95,7 +95,7 @@ protected function validateRegex(SchemaBuilder $builder): void
if ($match !== null) {
throw (new InvalidSchemaException(
sprintf(
'@Path "%s" in "%s::%s()" contains illegal characters "%s". Allowed characters are only [a-zA-Z0-9-_/{}].',
'#[Path] "%s" in "%s::%s()" contains illegal characters "%s". Allowed characters are only [a-zA-Z0-9-_/{}].',
$path,
$controller->getClass(),
$method->getName(),
Expand All @@ -121,7 +121,7 @@ protected function validateRegex(SchemaBuilder $builder): void
if ($match !== null) {
throw (new InvalidSchemaException(
sprintf(
'@Path "%s" in "%s::%s()" contains illegal characters "%s" in parameter. Allowed characters in parameter are only {[a-z-A-Z0-9-_]+}',
'#[Path] "%s" in "%s::%s()" contains illegal characters "%s" in parameter. Allowed characters in parameter are only {[a-z-A-Z0-9-_]+}',
$path,
$controller->getClass(),
$method->getName(),
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Schema/Validation/RequestParameterValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ protected function validateMaskParametersAreInPath(SchemaBuilder $builder): void
}

throw new InvalidSchemaException(sprintf(
'Mask parameter "%s" is not defined as @RequestParameter(in=path) in "%s"',
'Mask parameter "%s" is not defined as #[RequestParameter(in=path)] in "%s"',
$maskParameter['name'],
$controller->getClass()
));
Expand Down
3 changes: 3 additions & 0 deletions tests/Cases/Core/Annotation/Controller/Method.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ require_once __DIR__ . '/../../../../bootstrap.php';

// Ok
Toolkit::test(function (): void {
$method = new Method('GET');
Assert::equal(['GET'], $method->getMethods());

$method = new Method(['GET']);
Assert::equal(['GET'], $method->getMethods());

Expand Down
8 changes: 8 additions & 0 deletions tests/Cases/Core/Annotation/Controller/RequestParameter.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ Toolkit::test(function (): void {
Assert::null($requestParameter->getDescription());
});

// Exception - empty name
Toolkit::test(function (): void {
Assert::exception(function (): void {
new RequestParameter('', EndpointParameter::TYPE_STRING, EndpointParameter::IN_PATH);
}, InvalidArgumentException::class, 'Empty #[RequestParameter] name given');
});


// Exception - no type
Toolkit::test(function (): void {
Assert::exception(function (): void {
Expand Down
36 changes: 36 additions & 0 deletions tests/Cases/Core/Annotation/Controller/Response.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php declare(strict_types = 1);

require_once __DIR__ . '/../../../../bootstrap.php';

use Apitte\Core\Annotation\Controller\Response;
use Contributte\Tester\Toolkit;
use Tester\Assert;

// OK
Toolkit::test(function (): void {
$response = new Response(
description: 'name',
code: '200',
entity: 'EntityClass'
);

Assert::same('name', $response->getDescription());
Assert::same('200', $response->getCode());
Assert::same('EntityClass', $response->getEntity());
});

// OK - default values
Toolkit::test(function (): void {
$response = new Response('name');

Assert::same('name', $response->getDescription());
Assert::same('default', $response->getCode());
Assert::null($response->getEntity());
});

// Exception - empty description
Toolkit::test(function (): void {
Assert::exception(function (): void {
new Response('');
}, InvalidArgumentException::class, 'Empty #[Response] description given');
});
6 changes: 3 additions & 3 deletions tests/Cases/Core/DI/ApiExtension.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use Nette\DI\Compiler;
use Nette\DI\Container;
use Nette\DI\ContainerLoader;
use Tester\Assert;
use Tests\Fixtures\Controllers\AnnotationFoobarController;
use Tests\Fixtures\Controllers\AttributeFoobarController;
use Tests\Fixtures\Psr\DummyLogger;

require_once __DIR__ . '/../../../bootstrap.php';
Expand Down Expand Up @@ -52,7 +52,7 @@ Toolkit::test(function (): void {
'debugMode' => true,
],
'services' => [
AnnotationFoobarController::class,
AttributeFoobarController::class,
],
]);
}, 2);
Expand All @@ -67,7 +67,7 @@ Toolkit::test(function (): void {
Assert::equal('/api/v1/foobar/baz1', $schema->getEndpoints()[0]->getMask());
Assert::equal('#^/api/v1/foobar/baz1$#', $schema->getEndpoints()[0]->getPattern());
Assert::equal([], $schema->getEndpoints()[0]->getParameters());
Assert::equal(AnnotationFoobarController::class, $schema->getEndpoints()[0]->getHandler()->getClass());
Assert::equal(AttributeFoobarController::class, $schema->getEndpoints()[0]->getHandler()->getClass());
Assert::equal('baz1', $schema->getEndpoints()[0]->getHandler()->getMethod());
});

Expand Down
7 changes: 3 additions & 4 deletions tests/Cases/Core/DI/Loader/AttributeLoader.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use Contributte\Tester\Toolkit;
use Nette\DI\ContainerBuilder;
use Nette\DI\Definitions\ServiceDefinition;
use Tester\Assert;
use Tests\Fixtures\Controllers\AnnotationFoobarController;
use Tests\Fixtures\Controllers\ApiV1Controller;
use Tests\Fixtures\Controllers\AttributeFoobarController;

Expand All @@ -23,7 +22,7 @@ Toolkit::test(function (): void {
->andReturnUsing(function (): array {
$controllers = [];
$controllers[] = $c1 = new ServiceDefinition();
$c1->setType(AnnotationFoobarController::class);
$c1->setType(AttributeFoobarController::class);

return $controllers;
});
Expand All @@ -39,8 +38,6 @@ Toolkit::test(function (): void {
// Parse attributes
Toolkit::test(function (): void {
$builder = new ContainerBuilder();
$builder->addDefinition('annotation_controller')
->setType(AnnotationFoobarController::class);

$builder->addDefinition('attribute_controller')
->setType(AttributeFoobarController::class);
Expand All @@ -62,6 +59,7 @@ function testController(Controller $controller): void
{
Assert::equal('/foobar', $controller->getPath());
Assert::equal('foobar', $controller->getId());
Assert::equal(['Foobar' => null], $controller->getTags());

Assert::count(4, $controller->getMethods());

Expand All @@ -73,6 +71,7 @@ function testController(Controller $controller): void
Assert::equal('baz2', $controller->getMethods()['baz2']->getName());
Assert::equal('/baz2', $controller->getMethods()['baz2']->getPath());
Assert::equal(['GET', 'POST'], $controller->getMethods()['baz2']->getHttpMethods());
Assert::equal(['Baz' => null], $controller->getMethods()['baz2']->getTags());

Assert::equal(
[
Expand Down
3 changes: 0 additions & 3 deletions tests/Cases/Core/DI/Loader/AttributeMultiLoader.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ use Apitte\Core\Schema\SchemaBuilder;
use Contributte\Tester\Toolkit;
use Nette\DI\ContainerBuilder;
use Tester\Assert;
use Tests\Fixtures\Controllers\AnnotationMultiController;
use Tests\Fixtures\Controllers\AttributeMultiController;

// Parse attributes
Toolkit::test(function (): void {
$builder = new ContainerBuilder();
$builder->addDefinition('annotation_multi_controller')
->setType(AnnotationMultiController::class);

$builder->addDefinition('attribute_multi_controller')
->setType(AttributeMultiController::class);
Expand Down
Loading