Apply fixes from StyleCI

This commit is contained in:
Marcel Pociot
2019-10-08 21:16:47 +00:00
committed by StyleCI Bot
parent 901f153ea3
commit 7d94de8b16
7 changed files with 62 additions and 58 deletions

View File

@@ -3,23 +3,21 @@
namespace Mpociot\ApiDoc\Strategies\Responses;
use Exception;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Mpociot\ApiDoc\Tools\Utils;
use ReflectionClass;
use ReflectionMethod;
use Illuminate\Support\Arr;
use League\Fractal\Manager;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use Mpociot\ApiDoc\Tools\Flags;
use Mpociot\ApiDoc\Tools\Utils;
use Mpociot\Reflection\DocBlock;
use League\Fractal\Resource\Item;
use Mpociot\Reflection\DocBlock\Tag;
use Illuminate\Database\Eloquent\Model;
use League\Fractal\Resource\Collection;
use Mpociot\ApiDoc\Strategies\Strategy;
use Mpociot\ApiDoc\Tools\RouteDocBlocker;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\Json\ResourceCollection;
/**
* Parse an Eloquent API resource response from the docblock ( @apiResource || @apiResourcecollection ).
@@ -64,13 +62,13 @@ class UseApiResourceTags extends Strategy
$model = $this->getClassToBeTransformed($tags);
$modelInstance = $this->instantiateApiResourceModel($model);
try {
$resource = new $apiResourceClass($modelInstance);
} catch (\Exception $e) {
// If it is a ResourceCollection class, it might throw an error
// when trying to instantiate with something other than a collection
$resource = new $apiResourceClass(collect([$modelInstance]));
}
try {
$resource = new $apiResourceClass($modelInstance);
} catch (\Exception $e) {
// If it is a ResourceCollection class, it might throw an error
// when trying to instantiate with something other than a collection
$resource = new $apiResourceClass(collect([$modelInstance]));
}
if (strtolower($apiResourceTag->getName()) == 'apiresourcecollection') {
// Collections can either use the regular JsonResource class (via `::collection()`,
// or a ResourceCollection (via `new`)
@@ -84,7 +82,7 @@ try {
return [
$statusCode => response()
->json($resource->toArray(app(Request::class)))
->getContent()
->getContent(),
];
} catch (\Exception $e) {
echo 'Exception thrown when fetching Eloquent API resource response for ['.implode(',', $route->methods)."] {$route->uri}.\n";
@@ -93,6 +91,7 @@ try {
} else {
echo "Run this again with the --verbose flag to see the exception.\n";
}
return null;
}
}
@@ -126,7 +125,7 @@ try {
$type = $modelTag->getContent();
if (empty($type)) {
throw new Exception("Failed to detect an Eloquent API resource model. Please specify a model using @apiResourceModel.");
throw new Exception('Failed to detect an Eloquent API resource model. Please specify a model using @apiResourceModel.');
}
return $type;
@@ -144,7 +143,8 @@ try {
// Factories are usually defined without the leading \ in the class name,
// but the user might write it that way in a comment. Let's be safe.
$type = ltrim($type, "\\");
$type = ltrim($type, '\\');
return factory($type)->make();
} catch (\Exception $e) {
if (Flags::$shouldBeVerbose) {

View File

@@ -3,17 +3,17 @@
namespace Mpociot\ApiDoc\Strategies\Responses;
use Exception;
use Illuminate\Database\Eloquent\Model;
use Mpociot\ApiDoc\Tools\Utils;
use ReflectionClass;
use ReflectionMethod;
use Illuminate\Support\Arr;
use League\Fractal\Manager;
use Illuminate\Routing\Route;
use Mpociot\ApiDoc\Tools\Flags;
use Mpociot\ApiDoc\Tools\Utils;
use Mpociot\Reflection\DocBlock;
use League\Fractal\Resource\Item;
use Mpociot\Reflection\DocBlock\Tag;
use Illuminate\Database\Eloquent\Model;
use League\Fractal\Resource\Collection;
use Mpociot\ApiDoc\Strategies\Strategy;
use Mpociot\ApiDoc\Tools\RouteDocBlocker;
@@ -75,7 +75,7 @@ class UseTransformerTags extends Strategy
return [
$statusCode => response($fractal->createData($resource)->toJson())
->getContent()
->getContent(),
];
} catch (\Exception $e) {
echo 'Exception thrown when fetching transformer response for ['.implode(',', $route->methods)."] {$route->uri}.\n";
@@ -84,6 +84,7 @@ class UseTransformerTags extends Strategy
} else {
echo "Run this again with the --verbose flag to see the exception.\n";
}
return null;
}
}
@@ -127,7 +128,7 @@ class UseTransformerTags extends Strategy
}
if ($type == null) {
throw new Exception("Failed to detect a transformer model. Please specify a model using @transformerModel.");
throw new Exception('Failed to detect a transformer model. Please specify a model using @transformerModel.');
}
return $type;
@@ -145,7 +146,8 @@ class UseTransformerTags extends Strategy
// Factories are usually defined without the leading \ in the class name,
// but the user might write it that way in a comment. Let's be safe.
$type = ltrim($type, "\\");
$type = ltrim($type, '\\');
return factory($type)->make();
} catch (\Exception $e) {
if (Flags::$shouldBeVerbose) {

View File

@@ -4,4 +4,6 @@ namespace Mpociot\ApiDoc\Tests\Fixtures;
use Illuminate\Database\Eloquent\Model;
class TestUser extends Model {}
class TestUser extends Model
{
}

View File

@@ -3,7 +3,6 @@
namespace Mpociot\ApiDoc\Tests\Fixtures;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\Json\ResourceCollection;
class TestUserApiResource extends JsonResource
{
@@ -11,13 +10,14 @@ class TestUserApiResource extends JsonResource
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
*
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->first_name." ".$this->last_name,
'name' => $this->first_name.' '.$this->last_name,
'email' => $this->email,
];
}

View File

@@ -10,6 +10,7 @@ class TestUserApiResourceCollection extends ResourceCollection
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
*
* @return array
*/
public function toArray($request)
@@ -22,4 +23,3 @@ class TestUserApiResourceCollection extends ResourceCollection
];
}
}

View File

@@ -2,13 +2,13 @@
namespace Mpociot\ApiDoc\Tests;
use Mpociot\ApiDoc\Tests\Fixtures\TestUser;
use ReflectionException;
use Illuminate\Support\Str;
use Mpociot\ApiDoc\Tools\Utils;
use Orchestra\Testbench\TestCase;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Mpociot\ApiDoc\Tests\Fixtures\TestUser;
use Mpociot\ApiDoc\Tests\Fixtures\TestController;
use Mpociot\ApiDoc\ApiDocGeneratorServiceProvider;
use Illuminate\Support\Facades\Route as RouteFacade;
@@ -24,14 +24,13 @@ class GenerateDocumentationTest extends TestCase
{
parent::setUp();
$factory = app(\Illuminate\Database\Eloquent\Factory::class);
$factory->define(TestUser::class, function () {
return [
'id' => 4,
'first_name' => "Tested",
'last_name' => "Again",
'email' => "a@b.com",
'first_name' => 'Tested',
'last_name' => 'Again',
'email' => 'a@b.com',
];
});
}
@@ -232,7 +231,8 @@ class GenerateDocumentationTest extends TestCase
/** @test */
public function generated_postman_collection_file_is_correct()
{ RouteFacade::get('/api/withDescription', [TestController::class, 'withEndpointDescription']);
{
RouteFacade::get('/api/withDescription', [TestController::class, 'withEndpointDescription']);
RouteFacade::get('/api/withResponseTag', TestController::class.'@withResponseTag');
RouteFacade::get('/api/withBodyParameters', TestController::class.'@withBodyParameters');
RouteFacade::get('/api/withQueryParameters', TestController::class.'@withQueryParameters');

View File

@@ -5,9 +5,9 @@
namespace Mpociot\ApiDoc\Tests\Unit;
use Illuminate\Support\Arr;
use Mpociot\ApiDoc\Tests\Fixtures\TestUser;
use Orchestra\Testbench\TestCase;
use Mpociot\ApiDoc\Tools\Generator;
use Mpociot\ApiDoc\Tests\Fixtures\TestUser;
use Mpociot\ApiDoc\Tools\DocumentationConfig;
use Mpociot\ApiDoc\Tests\Fixtures\TestController;
use Mpociot\ApiDoc\ApiDocGeneratorServiceProvider;
@@ -60,9 +60,9 @@ abstract class GeneratorTestCase extends TestCase
$factory->define(TestUser::class, function () {
return [
'id' => 4,
'first_name' => "Tested",
'last_name' => "Again",
'email' => "a@b.com",
'first_name' => 'Tested',
'last_name' => 'Again',
'email' => 'a@b.com',
];
});
$this->generator = new Generator(new DocumentationConfig($this->config));
@@ -395,7 +395,7 @@ abstract class GeneratorTestCase extends TestCase
$route = $this->createRoute('POST', '/withEloquentApiResource', 'withEloquentApiResource');
$config = $this->config;
$config['strategies']['responses'] = [\Mpociot\ApiDoc\Strategies\Responses\UseApiResourceTags::class,];
$config['strategies']['responses'] = [\Mpociot\ApiDoc\Strategies\Responses\UseApiResourceTags::class];
$generator = new Generator(new DocumentationConfig($config));
$parsed = $this->generator->processRoute($route);
@@ -407,8 +407,8 @@ abstract class GeneratorTestCase extends TestCase
$this->assertEquals(200, $response['status']);
$this->assertArraySubset([
'id' => 4,
'name' => "Tested Again",
'email' => "a@b.com",
'name' => 'Tested Again',
'email' => 'a@b.com',
], json_decode($response['content'], true));
}
@@ -418,7 +418,7 @@ abstract class GeneratorTestCase extends TestCase
$route = $this->createRoute('POST', '/withEloquentApiResourceCollection', 'withEloquentApiResourceCollection');
$config = $this->config;
$config['strategies']['responses'] = [\Mpociot\ApiDoc\Strategies\Responses\UseApiResourceTags::class,];
$config['strategies']['responses'] = [\Mpociot\ApiDoc\Strategies\Responses\UseApiResourceTags::class];
$generator = new Generator(new DocumentationConfig($config));
$parsed = $this->generator->processRoute($route);
@@ -432,13 +432,13 @@ abstract class GeneratorTestCase extends TestCase
$this->assertIsArray($content);
$this->assertArraySubset([
'id' => 4,
'name' => "Tested Again",
'email' => "a@b.com",
'name' => 'Tested Again',
'email' => 'a@b.com',
], $content[0]);
$this->assertArraySubset([
'id' => 4,
'name' => "Tested Again",
'email' => "a@b.com",
'name' => 'Tested Again',
'email' => 'a@b.com',
], $content[1]);
}
@@ -448,7 +448,7 @@ abstract class GeneratorTestCase extends TestCase
$route = $this->createRoute('POST', '/withEloquentApiResourceCollectionClass', 'withEloquentApiResourceCollectionClass');
$config = $this->config;
$config['strategies']['responses'] = [\Mpociot\ApiDoc\Strategies\Responses\UseApiResourceTags::class,];
$config['strategies']['responses'] = [\Mpociot\ApiDoc\Strategies\Responses\UseApiResourceTags::class];
$generator = new Generator(new DocumentationConfig($config));
$parsed = $this->generator->processRoute($route);
@@ -461,20 +461,20 @@ abstract class GeneratorTestCase extends TestCase
$content = json_decode($response['content'], true);
$this->assertIsArray($content);
$this->assertArraySubset([
"data" => [
'data' => [
[
'id' => 4,
'name' => "Tested Again",
'email' => "a@b.com",
'name' => 'Tested Again',
'email' => 'a@b.com',
],
[
'id' => 4,
'name' => "Tested Again",
'email' => "a@b.com",
'name' => 'Tested Again',
'email' => 'a@b.com',
],
],
"links" => [
"self" => "link-value",
'links' => [
'self' => 'link-value',
],
], $content);
}
@@ -617,7 +617,7 @@ abstract class GeneratorTestCase extends TestCase
$this->assertEquals(200, $response['status']);
$this->assertSame(
$response['content'],
'{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},' .
'{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},'.
'{"id":1,"description":"Welcome on this test versions","name":"TestName"}]}'
);
}
@@ -636,7 +636,7 @@ abstract class GeneratorTestCase extends TestCase
$this->assertEquals(200, $response['status']);
$this->assertSame(
$response['content'],
'{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},' .
'{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},'.
'{"id":1,"description":"Welcome on this test versions","name":"TestName"}]}'
);
}
@@ -738,7 +738,7 @@ abstract class GeneratorTestCase extends TestCase
public function can_parse_response_file_tag()
{
// copy file to storage
$filePath = __DIR__ . '/../Fixtures/response_test.json';
$filePath = __DIR__.'/../Fixtures/response_test.json';
$fixtureFileJson = file_get_contents($filePath);
copy($filePath, storage_path('response_test.json'));
@@ -763,7 +763,7 @@ abstract class GeneratorTestCase extends TestCase
public function can_add_or_replace_key_value_pair_in_response_file()
{
// copy file to storage
$filePath = __DIR__ . '/../Fixtures/response_test.json';
$filePath = __DIR__.'/../Fixtures/response_test.json';
$fixtureFileJson = file_get_contents($filePath);
copy($filePath, storage_path('response_test.json'));
@@ -788,10 +788,10 @@ abstract class GeneratorTestCase extends TestCase
public function can_parse_multiple_response_file_tags_with_status_codes()
{
// copy file to storage
$successFilePath = __DIR__ . '/../Fixtures/response_test.json';
$successFilePath = __DIR__.'/../Fixtures/response_test.json';
$successFixtureFileJson = file_get_contents($successFilePath);
copy($successFilePath, storage_path('response_test.json'));
$errorFilePath = __DIR__ . '/../Fixtures/response_error_test.json';
$errorFilePath = __DIR__.'/../Fixtures/response_error_test.json';
$errorFixtureFileJson = file_get_contents($errorFilePath);
copy($errorFilePath, storage_path('response_error_test.json'));