Skip to content

Commit

Permalink
Merge pull request #13 from sunrise-php/release/v1.4.0
Browse files Browse the repository at this point in the history
v1.4.0
  • Loading branch information
fenric authored Apr 20, 2020
2 parents 4bf576e + 1a8429e commit a1cb5a2
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"require": {
"php": "^7.1",
"doctrine/annotations": "^1.6",
"sunrise/http-router": "^2.1"
"sunrise/http-router": "^2.4"
},
"require-dev": {
"phpbench/phpbench": "0.16.10",
Expand Down
32 changes: 32 additions & 0 deletions src/OpenApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
*/
use function Sunrise\Http\Router\path_parse;
use function Sunrise\Http\Router\path_plain;
use function json_encode;
use function strtolower;

/**
Expand Down Expand Up @@ -164,6 +165,16 @@ public function __construct(Info $info)
$this->annotationReader->addNamespace(self::ANNOTATIONS_NAMESPACE);
}

/**
* @param int $options
*
* @return string
*/
public function toJson(int $options = 0) : string
{
return json_encode($this->toArray(), $options);
}

/**
* @param bool $value
*
Expand Down Expand Up @@ -290,6 +301,27 @@ private function fetchOperation(RouteInterface $route) : ?OperationAnnotation
$operation->operationId = $route->getName();
}

if (null === $operation->tags) {
$routeTags = $route->getTags();
if ([] !== $routeTags) {
$operation->tags = $routeTags;
}
}

if (null === $operation->summary) {
$routeSummary = $route->getSummary();
if ('' !== $routeSummary) {
$operation->summary = $routeSummary;
}
}

if (null === $operation->description) {
$routeDescription = $route->getDescription();
if ('' !== $routeDescription) {
$operation->description = $routeDescription;
}
}

$attributes = path_parse($route->getPath());

foreach ($attributes as $attribute) {
Expand Down
50 changes: 50 additions & 0 deletions tests/OpenApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,4 +345,54 @@ public function testNotIncludeUndescribedRoutes() : void
],
], $object->toArray());
}

/**
* @return void
*/
public function testRouteData() : void
{
$handler = $this->createMock(RequestHandlerInterface::class);

$route = $this->createMock(RouteInterface::class);
$route->method('getRequestHandler')->willReturn($handler);
$route->method('getName')->willReturn('foo');
$route->method('getMethods')->willReturn(['GET']);
$route->method('getPath')->willReturn('/foo');
$route->method('getTags')->willReturn(['foo', 'bar']);
$route->method('getSummary')->willReturn('summary of the route');
$route->method('getDescription')->willReturn('description of the route');

$object = new OpenApi(new Info('foo', 'bar'));
$object->addRoute($route);

$this->assertSame([
'openapi' => '3.0.2',
'info' => [
'title' => 'foo',
'version' => 'bar',
],
'paths' => [
'/foo' => [
'get' => [
'operationId' => 'foo',
'tags' => ['foo', 'bar'],
'summary' => 'summary of the route',
'description' => 'description of the route',
],
],
],
], $object->toArray());
}

/**
* @return void
*/
public function testJson() : void
{
$object = new OpenApi(new Info('foo', 'bar'));

$expected = json_encode($object->toArray(), \JSON_PRETTY_PRINT);

$this->assertSame($expected, $object->toJson(\JSON_PRETTY_PRINT));
}
}

0 comments on commit a1cb5a2

Please sign in to comment.