Skip to content

Commit

Permalink
Fix defaults (#7)
Browse files Browse the repository at this point in the history
* adding defaults to paths that can't be reflected on
* moving phpstan to dist file
  • Loading branch information
Idrinth committed Feb 7, 2019
1 parent 52e609b commit 15bceb4
Show file tree
Hide file tree
Showing 11 changed files with 315 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ jobs:
before_install:
- phpenv config-rm xdebug.ini || true
- mkdir ../tools && composer init --name=putg/tools --working-dir=../tools
- composer require phpstan/phpstan:^0.10 phpstan/phpstan-phpunit phpstan/phpstan-strict-rules --working-dir=../tools
- composer require phpstan/phpstan:^0.11 phpstan/phpstan-phpunit phpstan/phpstan-strict-rules:^0.11 --working-dir=../tools
- sh install-phalcon.sh
install:
- composer update
Expand Down
2 changes: 1 addition & 1 deletion install-phalcon.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
mkdir tmp && \
mv composer.json tmp/ && \
composer require --dev techpivot/phalcon-ci-installer && \
vendor/bin/install-phalcon.sh && \
vendor/bin/install-phalcon.sh 3.2.x && \
rm composer.lock && \
rm composer.json && \
mv tmp/composer.json ./ && \
Expand Down
11 changes: 6 additions & 5 deletions phpstan.neon → phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
includes:
- ../tools/vendor/phpstan/phpstan-phpunit/extension.neon
- ../tools/vendor/phpstan/phpstan-phpunit/rules.neon
- ../tools/vendor/phpstan/phpstan-strict-rules/rules.neon
parameters:
ignoreErrors:
- '#^Parameter \#3 \$subject of function preg_replace expects array\|string, array\|string\|null given\.$#'
- '#^Parameter \#3 \$subject of function preg_replace expects array\|string, string\|null given\.$#'
- '#^Access to an undefined property De\\Idrinth\\PhalconRoutes2OpenApi\\Implementations\\Controller::\$(router|request|di|response)\.$#'
- '#^Parameter \#4 ...\$sets of method De\\Idrinth\\PhalconRoutes2OpenApi\\Implementations\\NoValueConversionMerger::mergeAll\(\) expects array, string given\.$#'
includes:
- ../tools/vendor/phpstan/phpstan-phpunit/extension.neon
- ../tools/vendor/phpstan/phpstan-phpunit/rules.neon
- ../tools/vendor/phpstan/phpstan-strict-rules/rules.neon
- '#^Parameter \#4 \.\.\.\$sets of method De\\Idrinth\\PhalconRoutes2OpenApi\\Implementations\\NoValueConversionMerger::mergeAll\(\) expects array, string given\.$#'
- '#^Parameter \#1 \$sets \(array<int, array>\) of method De\\Idrinth\\PhalconRoutes2OpenApi\\Implementations\\NoValueConversionMerger::mergeAll\(\) should be contravariant with parameter \$sets \(array<int, mixed>\) of method De\\Idrinth\\PhalconRoutes2OpenApi\\Interfaces\\RecursiveMerger::mergeAll\(\)$#'
15 changes: 15 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
>
<testsuites>
<testsuite name="default">
<directory>test</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory>src</directory>
</whitelist>
</filter>
</phpunit>
3 changes: 2 additions & 1 deletion src/Implementations/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ class Controller extends PhalconController implements ControllerInterface
public function index(): ResponseInterface
{
$paths = [];
$converter = $this->di->get(Path2PathConverter::class);
foreach ($this->router->getRoutes() as $route) {
$paths[] = $this->di->get(Path2PathConverter::class)->convert($route);
$paths[] = $converter->convert($route);
}
$merger = $this->di->get(RecursiveMerger::class);
return $this
Expand Down
31 changes: 31 additions & 0 deletions src/Implementations/DefaultResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace De\Idrinth\PhalconRoutes2OpenApi\Implementations;

use stdClass;

class DefaultResponse
{
/**
* @param array $route
* @return array
*/
public static function add(array $route): array
{
if (!isset($route['responses']) || count($route['responses']) === 0) {
$route['responses'] = [
'200' => [
"description" => 'unknown return',
'content' => [
'*/*' => [
'schema' => new stdClass()
]
]
]
];
}
$route['summary'] = $route['summary'] ?? '';
$route['description'] = $route['description'] ?? '';
return $route;
}
}
17 changes: 12 additions & 5 deletions src/Implementations/PhalconPath2PathArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,14 @@ private function handleParams(string &$path, array &$openapi)
$parts = explode(':', substr($match, 1, -1), 2);
$param = [
'in' => 'path',
'name' => $parts[0]
'name' => $parts[0],
'required' => true,
'schema' => [
'type' => 'string',
'pattern' => count($parts) === 2 ? $parts[1] : '.+'
]
];
if (count($parts) === 2) {
$param['schema'] = [
'type' => 'string',
'pattern' => $parts[1]
];
$path = str_replace($match, '{'.$parts[0].'}', $path);
}
$openapi['parameters'][] = $param;
Expand All @@ -94,6 +95,7 @@ private function handleQuery(string &$path, array &$openapi, RouteInterface $rou
$openapi['parameters'][] = [
'name' => $name,
'in' => 'path',
'required' => true,
'schema' => [
'type' => 'string',
'pattern' => $match
Expand All @@ -120,9 +122,14 @@ public function convert(RouteInterface $route):array
(string) ($route->getPaths()['controller'] ?? ''),
(string) ($route->getPaths()['action'] ?? '')
);
$methods = ['get'];
foreach ((array)$route->getHttpMethods() as $method) {
$methods[] = strtolower($method);
$openapi[strtolower($method)] = $this->merger->merge((array) ($openapi[strtolower($method)]??[]), $data);
}
foreach (array_unique($methods) as $method) {
$openapi[$method] = DefaultResponse::add($openapi[$method]??[]);
}
ksort($openapi);
return [$path => $openapi];
}
Expand Down
15 changes: 8 additions & 7 deletions src/Implementations/Reflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,17 @@ public function __invoke(string $class, string $method):array
$this->cache[$class]['____class'] = new ReflectionClass($class);
}
if (!isset($this->cache[$class][$method])) {
$this->cache[$class][$method] = $this->getReflect($this->cache[$class]['____class'], $method);
$this->cache[$class][$method] = DefaultResponse::add(
$this->getReflect($this->cache[$class]['____class'], $method)
);
}
return $this->cache[$class][$method];
} catch (Exception $e) {
return [
"description" => '',
"summary" => '',
"responses" => []
];
$this->cache[$class][$method] = DefaultResponse::add([
"summary" => 'unretrievable definition',
"description" => "$class::$method could not be reflected on.",
]);
}
return $this->cache[$class][$method];
}

/**
Expand Down
44 changes: 44 additions & 0 deletions test/DefaultResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace De\Idrinth\Test\PhalconRoutes2OpenApi;

use De\Idrinth\PhalconRoutes2OpenApi\Implementations\DefaultResponse;
use PHPUnit\Framework\TestCase;
use stdClass;

class DefaultResponseTest extends TestCase
{
/**
* @return array
*/
public function provideAdd(): array
{
$default = [
'summary' => '',
'description' => '',
'responses' => [
'200' => ['description' => 'unknown return','content' => ['*/*' => ['schema' => new stdClass()]]]
]
];
return [
'empty' => [[], $default],
'existing summary' => [['summary' => 'a'], array_merge($default, ['summary' => 'a'])],
'existing description' => [['description' => 'b'], array_merge($default, ['description' => 'b'])],
'existing response' => [
['responses' => ['200' => []]],
array_merge($default, ['responses' => ['200' => []]])
],
];
}

/**
* @dataProvider provideAdd
* @param array $route
* @param array $expectation
* @return void
*/
public function testAdd(array $route, array $expectation)
{
self::assertEquals($expectation, DefaultResponse::add($route));
}
}

0 comments on commit 15bceb4

Please sign in to comment.