Skip to content

Commit

Permalink
Merge pull request #669 from andrey-helldar/patch-2020-01-13-10-27
Browse files Browse the repository at this point in the history
Added support for the latest packages
  • Loading branch information
shalvah authored Jan 19, 2020
2 parents 6fc4837 + 309f338 commit fd4a5a7
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 52 deletions.
42 changes: 29 additions & 13 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
dist: bionic
language: php

php:
- 7.3
- 7.2

env:
- PREFER_LOWEST=""
- PREFER_LOWEST="--prefer-lowest"
global:
- SETUP=stable

jobs:
matrix:
fast_finish: true
include:
- script: composer lint
- php: 7.2
- php: 7.2
env: SETUP=lowest
- php: 7.3
- php: 7.3
env: SETUP=lowest
- php: 7.4
- php: 7.4
env: SETUP=lowest
- php: 7.4
env: SETUP=lint
name: "Lint code"
php: 7.3
env: PREFER_LOWEST=""

script: composer test-ci
before_script:
cache:
directories:
- $HOME/.composer/cache

before_install:
- travis_retry composer self-update
- travis_retry composer update --no-interaction --prefer-dist $PREFER_LOWEST

install:
- if [[ $SETUP = 'stable' ]]; then travis_retry composer update --prefer-dist --no-interaction --prefer-stable --no-suggest; fi
- if [[ $SETUP = 'lowest' ]]; then travis_retry composer update --prefer-dist --no-interaction --prefer-lowest --prefer-stable --no-suggest; fi
- if [[ $SETUP = 'lint' ]]; then travis_retry composer update --prefer-dist --no-interaction --prefer-stable --no-suggest; travis_retry composer lint; fi

script:
- vendor/bin/phpunit
26 changes: 16 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,26 @@
],
"require": {
"php": ">=7.2.0",
"ext-json": "*",
"fzaninotto/faker": "^1.8",
"illuminate/routing": "^5.7|^6.0",
"illuminate/support": "^5.7|^6.0",
"illuminate/console": "^5.7|^6.0",
"illuminate/console": "^5.7|^6.0|^7.0",
"illuminate/routing": "^5.7|^6.0|^7.0",
"illuminate/support": "^5.7|^6.0|^7.0",
"league/flysystem": "^1.0",
"mpociot/documentarian": "^0.3.0",
"mpociot/reflection-docblock": "^1.0.1",
"nunomaduro/collision": "^3.0",
"ramsey/uuid": "^3.8",
"symfony/var-exporter": "^4.0",
"league/flysystem": "^1.0",
"nunomaduro/collision": "^3.0"
"symfony/var-exporter": "^4.0|^5.0"
},
"require-dev": {
"orchestra/testbench": "^3.7.0 || ^4.0",
"phpunit/phpunit": "^7.5.0",
"dingo/api": "^2.3.0",
"mockery/mockery": "^1.2.0",
"dms/phpunit-arraysubset-asserts": "^0.1.0",
"league/fractal": "^0.17.0",
"phpstan/phpstan": "^0.11.15"
"mockery/mockery": "^1.2.0",
"orchestra/testbench": "^3.7|^4.0",
"phpstan/phpstan": "^0.11.15",
"phpunit/phpunit": "^8.0"
},
"suggest": {
"league/fractal": "Required for transformers support"
Expand Down Expand Up @@ -62,5 +64,9 @@
"branch-alias": {
"dev-v4": "4.x-dev"
}
},
"config": {
"preferred-install": "dist",
"sort-packages": true
}
}
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
parameters:
level: 5
reportUnmatchedIgnoredErrors: false
inferPrivatePropertyTypeFromConstructor:: true
inferPrivatePropertyTypeFromConstructor: true
ignoreErrors:
- '#Call to an undefined static method Illuminate\\Support\\Facades\\Route::getRoutes().#'
- '#Call to an undefined static method Illuminate\\Support\\Facades\\URL::forceRootUrl()#'
Expand Down
2 changes: 2 additions & 0 deletions src/Commands/GenerateDocumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ public function handle(RouteMatcherInterface $routeMatcher)
* @param \Mpociot\ApiDoc\Extracting\Generator $generator
* @param Match[] $routes
*
* @throws \ReflectionException
*
* @return array
*/
private function processRoutes(Generator $generator, array $routes)
Expand Down
4 changes: 3 additions & 1 deletion src/Extracting/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ public function getMethods(Route $route)
* @param \Illuminate\Routing\Route $route
* @param array $routeRules Rules to apply when generating documentation for this route
*
* @throws \ReflectionException
*
* @return array
*/
public function processRoute(Route $route, array $routeRules = [])
{
list($controllerName, $methodName) = Utils::getRouteClassAndMethodNames($route->getAction());
[$controllerName, $methodName] = Utils::getRouteClassAndMethodNames($route->getAction());
$controller = new ReflectionClass($controllerName);
$method = $controller->getMethod($methodName);

Expand Down
3 changes: 2 additions & 1 deletion src/Extracting/ParamHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Mpociot\ApiDoc\Extracting;

use Faker\Factory;
use stdClass;

trait ParamHelpers
{
Expand Down Expand Up @@ -32,7 +33,7 @@ protected function generateDummyValue(string $type)
return [];
},
'object' => function () {
return new \stdClass;
return new stdClass;
},
];

Expand Down
1 change: 1 addition & 0 deletions src/Extracting/RouteDocBlocker.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class RouteDocBlocker
* @param Route $route
*
* @throws \ReflectionException
* @throws \Exception
*
* @return array<string, DocBlock> Method and class docblocks
*/
Expand Down
20 changes: 12 additions & 8 deletions src/Extracting/Strategies/Responses/UseTransformerTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Model as IlluminateModel;
use Illuminate\Routing\Route;
use Illuminate\Support\Arr;
use League\Fractal\Manager;
Expand Down Expand Up @@ -34,7 +35,7 @@ class UseTransformerTags extends Strategy
*
* @return array|null
*/
public function __invoke(Route $route, \ReflectionClass $controller, \ReflectionMethod $method, array $rulesToApply, array $context = [])
public function __invoke(Route $route, ReflectionClass $controller, ReflectionMethod $method, array $rulesToApply, array $context = [])
{
$docBlocks = RouteDocBlocker::getDocBlocksFromRoute($route);
/** @var DocBlock $methodDocBlock */
Expand All @@ -47,6 +48,7 @@ public function __invoke(Route $route, \ReflectionClass $controller, \Reflection
* Get a response from the transformer tags.
*
* @param array $tags
* @param Route $route
*
* @return array|null
*/
Expand All @@ -57,7 +59,7 @@ protected function getTransformerResponse(array $tags, Route $route)
return null;
}

list($statusCode, $transformer) = $this->getStatusCodeAndTransformerClass($transformerTag);
[$statusCode, $transformer] = $this->getStatusCodeAndTransformerClass($transformerTag);
$model = $this->getClassToBeTransformed($tags, (new ReflectionClass($transformer))->getMethod('transform'));
$modelInstance = $this->instantiateTransformerModel($model);

Expand All @@ -81,7 +83,7 @@ protected function getTransformerResponse(array $tags, Route $route)
'content' => $response->getContent(),
],
];
} catch (\Exception $e) {
} catch (Exception $e) {
echo 'Exception thrown when fetching transformer response for ['.implode(',', $route->methods)."] {$route->uri}.\n";
if (Flags::$shouldBeVerbose) {
Utils::dumpException($e);
Expand Down Expand Up @@ -112,6 +114,8 @@ private function getStatusCodeAndTransformerClass($tag): array
* @param array $tags
* @param ReflectionMethod $transformerMethod
*
* @throws Exception
*
* @return string
*/
private function getClassToBeTransformed(array $tags, ReflectionMethod $transformerMethod): string
Expand All @@ -125,9 +129,9 @@ private function getClassToBeTransformed(array $tags, ReflectionMethod $transfor
$type = $modelTag->getContent();
} else {
$parameter = Arr::first($transformerMethod->getParameters());
if ($parameter->hasType() && ! $parameter->getType()->isBuiltin() && class_exists((string) $parameter->getType())) {
if ($parameter->hasType() && ! $parameter->getType()->isBuiltin() && class_exists($parameter->getType()->getName())) {
// Ladies and gentlemen, we have a type!
$type = (string) $parameter->getType();
$type = $parameter->getType()->getName();
}
}

Expand All @@ -153,20 +157,20 @@ protected function instantiateTransformerModel(string $type)
$type = ltrim($type, '\\');

return factory($type)->make();
} catch (\Exception $e) {
} catch (Exception $e) {
if (Flags::$shouldBeVerbose) {
echo "Eloquent model factory failed to instantiate {$type}; trying to fetch from database.\n";
}

$instance = new $type;
if ($instance instanceof \Illuminate\Database\Eloquent\Model) {
if ($instance instanceof IlluminateModel) {
try {
// we can't use a factory but can try to get one from the database
$firstInstance = $type::first();
if ($firstInstance) {
return $firstInstance;
}
} catch (\Exception $e) {
} catch (Exception $e) {
// okay, we'll stick with `new`
if (Flags::$shouldBeVerbose) {
echo "Failed to fetch first {$type} from database; using `new` to instantiate.\n";
Expand Down
8 changes: 8 additions & 0 deletions src/Tools/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ public static function deleteDirectoryAndContents($dir)
$fs->deleteDir($dir);
}

/**
* @param mixed $value
* @param int $indentationLevel
*
* @throws \Symfony\Component\VarExporter\Exception\ExceptionInterface
*
* @return string
*/
public static function printPhpValue($value, $indentationLevel = 0)
{
$output = VarExporter::export($value);
Expand Down
26 changes: 13 additions & 13 deletions tests/GenerateDocumentationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public function console_command_does_not_work_with_closure()
config(['apidoc.routes.0.match.prefixes' => ['api/*']]);
$output = $this->artisan('apidoc:generate');

$this->assertContains('Skipping route: [GET] api/closure', $output);
$this->assertContains('Processed route: [GET] api/test', $output);
$this->assertStringContainsString('Skipping route: [GET] api/closure', $output);
$this->assertStringContainsString('Processed route: [GET] api/test', $output);
}

/** @test */
Expand All @@ -85,8 +85,8 @@ public function console_command_does_not_work_with_closure_using_dingo()
config(['apidoc.routes.0.match.versions' => ['v1']]);
$output = $this->artisan('apidoc:generate');

$this->assertContains('Skipping route: [GET] closure', $output);
$this->assertContains('Processed route: [GET] test', $output);
$this->assertStringContainsString('Skipping route: [GET] closure', $output);
$this->assertStringContainsString('Processed route: [GET] test', $output);
}

/** @test */
Expand All @@ -97,8 +97,8 @@ public function console_command_work_with_routes_callable_tuple()
config(['apidoc.routes.0.match.prefixes' => ['api/*']]);
$output = $this->artisan('apidoc:generate');

$this->assertNotContains('Skipping route: [GET] api/array/test', $output);
$this->assertContains('Processed route: [GET] api/array/test', $output);
$this->assertStringNotContainsString('Skipping route: [GET] api/array/test', $output);
$this->assertStringContainsString('Processed route: [GET] api/array/test', $output);
}

/** @test */
Expand All @@ -110,8 +110,8 @@ public function can_skip_single_routes()
config(['apidoc.routes.0.match.prefixes' => ['api/*']]);
$output = $this->artisan('apidoc:generate');

$this->assertContains('Skipping route: [GET] api/skip', $output);
$this->assertContains('Processed route: [GET] api/test', $output);
$this->assertStringContainsString('Skipping route: [GET] api/skip', $output);
$this->assertStringContainsString('Processed route: [GET] api/test', $output);
}

/** @test */
Expand All @@ -122,8 +122,8 @@ public function can_skip_non_existent_response_files()
config(['apidoc.routes.0.match.prefixes' => ['api/*']]);
$output = $this->artisan('apidoc:generate');

$this->assertContains('Skipping route: [GET] api/non-existent', $output);
$this->assertContains('@responseFile i-do-not-exist.json does not exist', $output);
$this->assertStringContainsString('Skipping route: [GET] api/non-existent', $output);
$this->assertStringContainsString('@responseFile i-do-not-exist.json does not exist', $output);
}

/** @test */
Expand Down Expand Up @@ -392,7 +392,7 @@ public function can_parse_utf8_response()
$this->artisan('apidoc:generate');

$generatedMarkdown = file_get_contents(__DIR__.'/../resources/docs/source/index.md');
$this->assertContains('Лорем ипсум долор сит амет', $generatedMarkdown);
$this->assertStringContainsString('Лорем ипсум долор сит амет', $generatedMarkdown);
}

/** @test */
Expand Down Expand Up @@ -437,7 +437,7 @@ public function supports_partial_resource_controller()

$this->assertNull($thrownException);
$generatedMarkdown = file_get_contents(__DIR__.'/../resources/docs/source/index.md');
$this->assertContains('Group A', $generatedMarkdown);
$this->assertContains('Group B', $generatedMarkdown);
$this->assertStringContainsString('Group A', $generatedMarkdown);
$this->assertStringContainsString('Group B', $generatedMarkdown);
}
}
2 changes: 1 addition & 1 deletion tests/TestHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ private function assertContainsIgnoringWhitespace($needle, $haystack)
{
$haystack = preg_replace('/\s/', '', $haystack);
$needle = preg_replace('/\s/', '', $needle);
$this->assertContains($needle, $haystack);
$this->assertStringContainsString($needle, $haystack);
}
}
3 changes: 3 additions & 0 deletions tests/Unit/GeneratorPluginSystemTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Mpociot\ApiDoc\Tests\Unit;

use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
use Illuminate\Routing\Route;
use Mpociot\ApiDoc\ApiDocGeneratorServiceProvider;
use Mpociot\ApiDoc\Extracting\Generator;
Expand All @@ -13,6 +14,8 @@

class GeneratorPluginSystemTestCase extends LaravelGeneratorTest
{
use ArraySubsetAsserts;

/**
* @var \Mpociot\ApiDoc\Extracting\Generator
*/
Expand Down
3 changes: 3 additions & 0 deletions tests/Unit/GeneratorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Mpociot\ApiDoc\Tests\Unit;

use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
use Illuminate\Support\Arr;
use Mpociot\ApiDoc\ApiDocGeneratorServiceProvider;
use Mpociot\ApiDoc\Extracting\Generator;
Expand All @@ -14,6 +15,8 @@

abstract class GeneratorTestCase extends TestCase
{
use ArraySubsetAsserts;

/**
* @var \Mpociot\ApiDoc\Extracting\Generator
*/
Expand Down
Loading

0 comments on commit fd4a5a7

Please sign in to comment.