Skip to content

Commit

Permalink
Added tests, repair some types
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Jul 16, 2018
1 parent b8a2124 commit c7da2db
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 31 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@
"codesniffer src tests"
],
"tester": [
"tester -s -p php --colors 1 -C tests/cases"
"tester -s -p phpdbg --colors 1 -C tests/cases"
],
"coverage": [
"tester -s -p php --colors 1 -C -d extension=xdebug.so --coverage ./coverage.xml --coverage-src ./src tests/cases"
"tester -s -p phpdbg --colors 1 -C -d extension=xdebug.so --coverage ./coverage.xml --coverage-src ./src tests/cases"
],
"phpstan-install": [
"mkdir -p temp/phpstan",
Expand Down
2 changes: 1 addition & 1 deletion src/DI/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Helpers
public static function sort(array $definitions, int $default = 10): array
{
// Sort by priority
uasort($definitions, function (int $a, int $b) use ($default) {
uasort($definitions, function (array $a, array $b) use ($default) {
$p1 = $a['priority'] ?? $default;
$p2 = $b['priority'] ?? $default;

Expand Down
4 changes: 2 additions & 2 deletions src/DI/Plugin/PluginCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ public function getPlugin(string $name): ?AbstractPlugin
{
$plugins = $this->manager->getPlugins();

return $plugins[$name] ?? null;
return $plugins[$name]['inst'] ?? null;
}

public function getPluginByType(string $class): ?AbstractPlugin
{
foreach ($this->manager->getPlugins() as $plugin) {
if (get_class($plugin['inst']) === $class) return $plugin;
if (get_class($plugin['inst']) === $class) return $plugin['inst'];
}

return null;
Expand Down
5 changes: 4 additions & 1 deletion src/Handler/ServiceCallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ public function setArguments(array $args): void
$this->arguments = $args;
}

public function __invoke(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
/**
* @return mixed
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
{
return call_user_func_array([$this->service, $this->method], $this->arguments);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Http/ApiResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function withEntity(AbstractEntity $entity): self
return $this->withAttribute(ResponseAttributes::ATTR_ENTITY, $entity);
}

public function getEndpoint(): Endpoint
public function getEndpoint(): ?Endpoint
{
return $this->getAttribute(ResponseAttributes::ATTR_ENDPOINT, null);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Schema/Builder/Controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function addGroupPath(string $path): void
$this->groupPaths[] = $path;
}

public function addTag(string $name, string $value): void
public function addTag(string $name, ?string $value): void
{
$this->tags[$name] = $value;
}
Expand Down
2 changes: 0 additions & 2 deletions src/Schema/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ final class Endpoint

// Tags
public const TAG_ID = 'id';
public const TAG_GROUP_IDS = 'group.ids';
public const TAG_GROUP_PATHS = 'group.paths';

/** @var string[] */
private $methods = [];
Expand Down
2 changes: 1 addition & 1 deletion src/Schema/EndpointNegotiation.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function getRenderer(): ?string
return $this->renderer;
}

public function setRenderer(string $renderer): void
public function setRenderer(?string $renderer): void
{
$this->renderer = $renderer;
}
Expand Down
8 changes: 0 additions & 8 deletions src/Schema/SchemaInspector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@ public function __construct(Schema $schema)
$this->schema = $schema;
}

/**
* @return Endpoint[]
*/
public function getEndpointByGroup(string $group): array
{
return $this->getEndpointsByTag(Endpoint::TAG_GROUP_IDS, $group);
}

/**
* @return Endpoint[]
*/
Expand Down
8 changes: 4 additions & 4 deletions src/Schema/Serialization/ArrayHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ protected function hydrateEndpoint(array $data): Endpoint
$handler->setArguments($data['handler']['arguments']);
$endpoint->setHandler($handler);

if (isset($data['id'])) {
$endpoint->addTag(Endpoint::TAG_ID, $data['id']);
}

if (isset($data['tags'])) {
foreach ($data['tags'] as $name => $value) {
$endpoint->addTag($name, $value);
}
}

if (isset($data['id'])) {
$endpoint->addTag(Endpoint::TAG_ID, $data['id']);
}

if (isset($data['attributes']['pattern'])) {
$endpoint->setAttribute('pattern', $data['attributes']['pattern']);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Schema/Serialization/ArraySerializator.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ protected function serializeInit(Controller $controller, Method $method): array
'arguments' => $method->getArguments(),
],
'id' => $id,
'tags' => $method->getTags(),
'tags' => array_merge($controller->getTags(), $method->getTags()),
'methods' => $method->getMethods(),
'mask' => $mask,
'description' => $method->getDescription(),
Expand Down
72 changes: 72 additions & 0 deletions tests/cases/Handler/ServiceHandler.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php declare(strict_types = 1);

/**
* Test: Handler\ServiceHandler
*/

use Apitte\Core\Exception\Logical\InvalidStateException;
use Apitte\Core\Handler\ServiceHandler;
use Apitte\Core\Http\RequestAttributes;
use Apitte\Core\Schema\Endpoint;
use Apitte\Core\Schema\EndpointHandler;
use Apitte\Core\UI\Controller\IController;
use Contributte\Psr7\Psr7ResponseFactory;
use Contributte\Psr7\Psr7ServerRequestFactory;
use Nette\DI\Container;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Tester\Assert;

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

// Missing endpoint
test(function (): void {
$container = new Container();
$sh = new ServiceHandler($container);

Assert::exception(function () use ($sh): void {
$request = Psr7ServerRequestFactory::fromSuperGlobal();
$response = Psr7ResponseFactory::fromGlobal();

$sh->handle($request, $response);
}, InvalidStateException::class, 'Attribute "apitte.core.endpoint" is required');
});

// Missing endpoint
test(function (): void {
$controller = new class() implements IController
{

/**
* @return mixed[]
*/
public function foobar(ServerRequestInterface $request, ResponseInterface $response): array
{
return [$request, $response];
}

};

$eh = new EndpointHandler();
$eh->setClass(get_class($controller));
$eh->setMethod('foobar');

$endpoint = new Endpoint();
$endpoint->setHandler($eh);

$container = Mockery::mock(Container::class);
$container->shouldReceive('getByType')
->once()
->andReturn($controller);

$sh = new ServiceHandler($container);

$request = Psr7ServerRequestFactory::fromSuperGlobal()
->withAttribute(RequestAttributes::ATTR_ENDPOINT, $endpoint);
$response = Psr7ResponseFactory::fromGlobal();

$res = $sh->handle($request, $response);

Assert::same($request, $res[0]);
Assert::same($response, $res[1]);
});
7 changes: 0 additions & 7 deletions tests/php-unix.ini

This file was deleted.

0 comments on commit c7da2db

Please sign in to comment.