Skip to content

Commit

Permalink
more clean up and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mtrajano committed Feb 10, 2020
1 parent bad6685 commit 78c1fe5
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 69 deletions.
20 changes: 5 additions & 15 deletions src/DataObjects/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private function getModelNameFromControllerDocs(): ?string

$docBlock = $reflection->getDocComment();

return $this->getModelAnnotation($docBlock);
return $this->getModelNameFromMethodDocs($docBlock);
}

/**
Expand Down Expand Up @@ -154,18 +154,6 @@ private function getActionDocBlock()
return $actionInstance ? $actionInstance->getDocComment() ?: '' : '';
}

/**
* Get Model name from method DocBlock.
*
* @return string|null
*
* @throws ReflectionException
*/
private function getModelNameFromMethodDocs(): ?string
{
return $this->getModelAnnotation();
}

/**
* Return a ReflectionMethod instance from current action.
*
Expand Down Expand Up @@ -254,7 +242,9 @@ public function getThrows(): array

if (!empty($exceptions)) {
$exceptions = array_unique(array_map(function ($e) {
return trim($e, "\ \t\n\r\0\x0B");
$trimmed_exception = preg_replace('/^\s+|\s+$/', '', $e);

return trim($trimmed_exception, '\\');
}, $exceptions));
}

Expand All @@ -271,7 +261,7 @@ public function getThrows(): array
*
* @throws \ReflectionException
*/
private function getModelAnnotation(?string $docBlock = null): ?string
private function getModelNameFromMethodDocs(?string $docBlock = null): ?string
{
$docBlock = $docBlock ?? $this->getActionDocBlock();

Expand Down
21 changes: 1 addition & 20 deletions src/Definitions/DefinitionGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Mtrajano\LaravelSwagger\Definitions;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Builder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -102,17 +101,9 @@ private function allowsHttpMethodGenerate(): bool
return true;
}

/**
* Get the model's columns from table.
*
* @return array
*/
private function getModelColumns(): array
{
/** @var Builder $schema */
$schema = Schema::getFacadeRoot();

return $schema->getColumnListing($this->model->getTable());
return Schema::getColumnListing($this->model->getTable());
}

private function getDefinitionProperties()
Expand All @@ -122,14 +113,6 @@ private function getDefinitionProperties()
$hiddenColumns = $this->model->getHidden();

if (method_exists($this->model, 'getAppends')) {
$appends = $this->model->getAppends();
// TODO: Test condition
if (!is_array($appends)) {
throw new RuntimeException(
'The return type of the "getAppends" method must be an array.'
);
}

$columns = array_merge($columns, $this->model->getAppends());
}

Expand All @@ -152,8 +135,6 @@ private function getDefinitionName(): string

/**
* Create an instance of the model with fake data or return null.
*
* @return Model|null
*/
private function getModelFake(): ?Model
{
Expand Down
6 changes: 3 additions & 3 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ public function __construct($config, $routeFilter = null)
*/
public function generate()
{
$this->docs = $this->getBaseInfo();
$this->docs['definitions'] = [];
$this->docs = $this->getBaseStructure();

$securityDefinitions = $this->generateSecurityDefinitions();
if ($securityDefinitions) {
Expand Down Expand Up @@ -91,7 +90,7 @@ public function generate()
return $this->docs;
}

protected function getBaseInfo()
protected function getBaseStructure()
{
$baseInfo = [
'swagger' => '2.0',
Expand All @@ -117,6 +116,7 @@ protected function getBaseInfo()
}

$baseInfo['paths'] = [];
$baseInfo['definitions'] = [];

return $baseInfo;
}
Expand Down
4 changes: 1 addition & 3 deletions src/LaravelSwaggerException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

namespace Mtrajano\LaravelSwagger;

class LaravelSwaggerException extends \Exception
{
}
class LaravelSwaggerException extends \Exception {}
5 changes: 1 addition & 4 deletions src/SwaggerDocsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,12 @@ public function getLastVersionKey()
->last();
}

/**
* @return array
*/
public function getRoutesWithVersions(): array
{
$versions = array_column($this->getAllVersionsConfigs(), 'appVersion');

$routesWithVersions = [];
foreach ($versions as $key => $version) {
foreach ($versions as $version) {
$route = route(
config('laravel-swagger.route.name'),
$version,
Expand Down
9 changes: 2 additions & 7 deletions src/Traits/HasAppends.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@

namespace Mtrajano\LaravelSwagger\Traits;

use Exception;

trait HasAppends
{
/**
* Get appends.
*
* @return array
* @throws Exception
* @throws \Mtrajano\LaravelSwagger\LaravelSwaggerException
*/
public function getAppends(): array
{
if (!property_exists($this, 'appends')) {
throw new Exception('The class that use HasAppend must have the property $appends');
return [];
}

return $this->appends;
Expand Down
2 changes: 1 addition & 1 deletion src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,6 @@ function laravel_swagger_asset($asset)
*/
function is_valid_file_name(string $file)
{
return preg_match('/^([-.\w]+)$/', $file) > 0;
return preg_match('/^([-_.\w]+)$/', $file) > 0;
}
}
46 changes: 30 additions & 16 deletions tests/DataObjects/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ protected function getEnvironmentSetUp($app)
->get('/middleware-controller-route', 'Mtrajano\LaravelSwagger\Tests\DataObjects\MiddlewareControllerRoute@index')
->name('middleware-controller-route')
->middleware('test');
$app['router']
->get('/exception-route', 'Mtrajano\LaravelSwagger\Tests\DataObjects\ExceptionRoute@index')
->name('exception-route');
}

public function provideRoutesWithMiddleware()
public function provideRoutesWithMiddleware() : array
{
return [
[
Expand Down Expand Up @@ -68,15 +71,20 @@ public function provideRoutesWithMiddleware()
];
}

public function testGetThrows()
{
$laravelRoute = app('router')->getRoutes()->getByName('exception-route');

$route = new Route($laravelRoute);

$this->assertEquals(['Exception', 'SpaceExcetion', 'TabException'], $route->getThrows());
}

/**
* @dataProvider provideRoutesWithMiddleware
* @param string $routeName
* @param array $expectedMiddleware
*/
public function testCreateFromOnlyControllerWithMiddleware(
string $routeName,
array $expectedMiddleware
) {
public function testCreateFromOnlyControllerWithMiddleware(string $routeName, array $expectedMiddleware)
{
$laravelRoute = app('router')->getRoutes()->getByName($routeName);

$route = new Route($laravelRoute);
Expand All @@ -99,9 +107,7 @@ public function __construct()
$this->middleware('auth');
}

public function index()
{
}
public function index() {}
}

class MiddlewareControllerRoute extends Controller
Expand All @@ -111,14 +117,22 @@ public function __construct()
$this->middleware('auth:api');
}

public function index()
{
}
public function index() {}
}

class OnlyRouteMiddleware extends Controller
{
public function index()
{
}
public function index() {}
}

class ExceptionRoute extends Controller
{
/**
* some description
*
* @throws \Exception
* @throws \SpaceExcetion
* @throws \TabException
*/
public function index() {}
}
38 changes: 38 additions & 0 deletions tests/Traits/HasAppendsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Mtrajano\LaravelSwagger\Tests\Parameters;

use Mtrajano\LaravelSwagger\Tests\TestCase;
use Mtrajano\LaravelSwagger\Traits\HasAppends;
use Illuminate\Database\Eloquent\Model;

class HasAppendsTest extends TestCase
{
public function testModelReturnsAppends() : void
{
$this->assertEquals([
'property-1',
'property-2',
], (new ModelWithAppends())->getAppends());
}

public function testModelWithoutAppendsReturnsEmpty() : void
{
$this->assertEquals([], (new ModelWithoutAppends())->getAppends());
}
}

class ModelWithAppends extends Model
{
use HasAppends;

protected $appends = [
'property-1',
'property-2',
];
}

class ModelWithoutAppends extends Model
{
use HasAppends;
}

0 comments on commit 78c1fe5

Please sign in to comment.