Skip to content

Commit

Permalink
Fix definition name
Browse files Browse the repository at this point in the history
  • Loading branch information
fsavina committed Mar 28, 2020
1 parent a0c3515 commit 8f5d5e3
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 80 deletions.
10 changes: 2 additions & 8 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public function tags ( array $tags )
*/
public function definition ( $name )
{
$definition = Definition::create ( compact ('name' ) );
$definition = Definition::create ()->setName ($name );

$this->swagger->getDefinitions ()->set ( $name, $definition );

Expand All @@ -196,13 +196,7 @@ public function version ( $version, $routes )
*/
public function resource ( $name, $controller, array $options = [] )
{
if ( $this->app && $this->app->bound ( ResourceRegistrar::class ) )
{
$registrar = $this->app->make ( ResourceRegistrar::class );
} else
{
$registrar = new ResourceRegistrar( $this->router );
}
$registrar = $this->app->make ( ResourceRegistrar::class );

$options = array_merge ( [ 'only' => [ 'index', 'show', 'store', 'update', 'destroy' ], ], $options );

Expand Down
27 changes: 18 additions & 9 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@
namespace LaravelApi;


use Illuminate\Contracts\Routing\Registrar;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider as IlluminateServiceProvider;


class ServiceProvider extends IlluminateServiceProvider
class ServiceProvider extends IlluminateServiceProvider implements DeferrableProvider
{


public function register ()
{
$this->mergeConfigFrom ( __DIR__ . '/../resources/config.php', 'api' );

$this->app->singleton ( Api::class );

$this->registerModelEndpointRegistry ();
Expand All @@ -29,6 +27,9 @@ public function register ()
}


/**
* @return void
*/
protected function registerModelEndpointRegistry ()
{
$this->app->singleton ( Endpoints\ModelsEndpointRegistry::class, function () {
Expand All @@ -43,9 +44,14 @@ protected function registerModelEndpointRegistry ()
}


public function boot ( Registrar $router )
/**
* @return void
*/
public function boot ()
{
$this->initRoutes ( $router );
$this->mergeConfigFrom ( __DIR__ . '/../resources/config.php', 'api' );

$this->initRoutes ( $this->app[ 'router' ] );

$resourcesPath = __DIR__ . '/../resources';

Expand All @@ -58,12 +64,15 @@ public function boot ( Registrar $router )
}


protected function initRoutes ( Registrar $router )
/**
* @param \Illuminate\Routing\RouteRegistrar|\Illuminate\Contracts\Routing\Registrar $router
*/
protected function initRoutes ( $router )
{
$router->prefix ( config ( 'api.prefix' ) )
->middleware ( 'api' )
->namespace ( 'LaravelApi\\Http\\Controllers' )
->group ( function ( Registrar $router ) {
->group ( function ( $router ) {

if ( $jsonPath = config ( 'api.swagger_json_path' ) )
{
Expand All @@ -83,7 +92,7 @@ protected function initRoutes ( Registrar $router )

public function provides ()
{
return [ Api::class ];
return [ Api::class, Endpoints\ModelsEndpointRegistry::class ];
}

}
Expand Down
18 changes: 9 additions & 9 deletions tests/ApiDefinitionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,33 @@

class ApiDefinitionsTest extends TestCase
{

public function testCreation ()
{
$definition = $this->api->definition ( 'DefinitionName' );

$this->assertInstanceOf ( Definition::class, $definition );
}


public function testProperties ()
{
$definition = $this->api->definition ( 'DefinitionName' );

$this->assertInstanceOf (
Definition::class,
$definition->addProperty ( 'name', 'Property description', 42, 'integer' )
);

$property = $definition->getProperties ()->get ( 'name' );
$this->assertInstanceOf ( Schema::class, $property );
$this->assertEquals ( 'integer', $property->getType () );
$this->assertEquals ( 'Property description', $property->getDescription () );
$this->assertEquals ( 42, $property->getDefault () );

$schema = $definition->toRef ();
$this->assertInstanceOf ( Schema::class, $schema );
$this->assertEquals ( '#/definitions/DefinitionName', $schema->getRef () );
}
}

}
15 changes: 7 additions & 8 deletions tests/ApiTagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,24 @@

class ApiTagsTest extends TestCase
{


public function testTags ()
{
$tag = $this->api->tag ( 'foobar', 'Tag description', function () {
$operation = $this->api->get ( 'tagged-uri', 'Controller@action' );
$this->assertTrue ( in_array ( 'foobar', $operation->getTags () ) );
} );

$this->assertInstanceOf ( Tag::class, $tag );
$this->assertEquals ( 'foobar', $tag->getName () );
$this->assertEquals ( 'Tag description', $tag->getDescription () );

$api = $this->api->tags ( [
'tag_1' => 'Some tag description',
'tag_2' => 'Some tag description',
] );
$this->assertInstanceOf ( Api::class, $api );
}
}


}
91 changes: 45 additions & 46 deletions tests/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
namespace LaravelApi\Tests;


use Illuminate\Contracts\Routing\Registrar;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Routing\Router;
use LaravelApi\Endpoints\Endpoint;
use LaravelApi\Endpoints\ModelsEndpointRegistry;
use LaravelApi\Endpoints\Operation;
use LaravelApi\Endpoints\ResourceEndpoint;
use Illuminate\Contracts\Routing\Registrar;
use Illuminate\Routing\Router;


class Page extends Model
Expand All @@ -27,94 +27,93 @@ class ApiTest extends TestCase
protected $router;


protected function setUp (): void
protected function setUp(): void
{
parent::setUp ();
parent::setUp();

$this->router = $this->app->make ( Registrar::class );
$this->router = $this->app->make(Registrar::class);
}


public function testBaseEndpointRegistration ()
public function testBaseEndpointRegistration()
{
$methods = [ 'get', 'post', 'put', 'delete', 'patch', 'options' ];
$methods = ['get', 'post', 'put', 'delete', 'patch', 'options'];

foreach ( $methods as $method )
{
$operation = $this->api->$method ( 'uri', 'Controller@action' );
$this->assertInstanceOf ( Operation::class, $operation );
foreach ($methods as $method) {
$operation = $this->api->$method ('uri', 'Controller@action');
$this->assertInstanceOf(Operation::class, $operation);
}

$this->assertInstanceOf ( Endpoint::class, $this->api->getEndpointByUri ( 'uri' ) );
$this->assertInstanceOf(Endpoint::class, $this->api->getEndpointByUri('uri'));
}


public function testCustomEndpointRegistration ()
public function testCustomEndpointRegistration()
{
$endpoint = $this->api->getEndpointByUri ( 'foobar' );
$endpoint = $this->api->getEndpointByUri('foobar');

$this->assertInstanceOf ( Endpoint::class, $endpoint );
$this->assertInstanceOf ( Operation::class, $endpoint->setMethod ( 'post' ) );
$this->assertInstanceOf(Endpoint::class, $endpoint);
$this->assertInstanceOf(Operation::class, $endpoint->setMethod('post'));
}


public function testAggregateEndpointRegistration ()
public function testAggregateEndpointRegistration()
{
$operation = $this->api->aggregate ( 'aggregate-uri', [ 'App\\Page', 'App\\User' ] );
$operation = $this->api->aggregate('aggregate-uri', ['App\\Page', 'App\\User']);

$this->assertInstanceOf ( Operation::class, $operation );
$this->assertInstanceOf(Operation::class, $operation);
}


public function testResourceEndpointRegistration ()
public function testResourceEndpointRegistration()
{
$endpoint = $this->api->resource ( 'users', 'Controller@action' );
$endpoint = $this->api->resource('users', 'Controller@action');

$this->assertInstanceOf ( ResourceEndpoint::class, $endpoint );
$this->assertInstanceOf ( ResourceEndpoint::class, $endpoint->setApi ( $this->api ) );
$this->assertInstanceOf(ResourceEndpoint::class, $endpoint);
$this->assertInstanceOf(ResourceEndpoint::class, $endpoint->setApi($this->api));
}


public function testModelsEndpointRegistration ()
public function testModelsEndpointRegistration()
{
$registry = $this->api->models (
[
Page::class,
'posts' => 'App\Post',
]
);
$registry = $this->app->make(ModelsEndpointRegistry::class);

$registry->add([Page::class, 'posts' => 'App\Post',]);

$this->assertInstanceOf ( ModelsEndpointRegistry::class, $registry );
$this->assertInstanceOf(ModelsEndpointRegistry::class, $registry);

$this->assertTrue ( $registry->has ( Page::class ) );
$this->assertTrue ( $registry->has ( 'App\Post' ) );
$this->assertTrue($registry->has(Page::class));
$this->assertTrue($registry->has('App\Post'));

$this->assertInstanceOf ( Page::class, $registry->resolve ( 'pages' ) );
$this->assertInstanceOf(Page::class, $registry->resolve('pages'));

$this->assertInstanceOf ( ModelsEndpointRegistry::class, $registry->clear () );
$this->assertFalse ( $registry->has ( Page::class ) );
$this->assertInstanceOf(ModelsEndpointRegistry::class, $registry->clear());
$this->assertFalse($registry->has(Page::class));
}


public function testVersion ()
public function testVersion()
{
$this->api->version ( 'v1', function () {
$operation = $this->api->get ( 'uri', 'Controller@action' );
$this->assertEquals ( 'v1', $operation->getPrefix () );
$this->assertTrue ( in_array ( 'v1', $operation->getTags () ) );
} );
$this->api->version(
'v1',
function () {
$operation = $this->api->get('uri', 'Controller@action');
$this->assertEquals('v1', $operation->getPrefix());
$this->assertTrue(in_array('v1', $operation->getTags()));
}
);
}


public function testTitle ()
public function testTitle()
{
$this->assertIsString ( $this->api->title () );
$this->assertIsString($this->api->title());
}


public function testJsonSerialization ()
public function testJsonSerialization()
{
$this->assertTrue ( is_array ( $this->api->jsonSerialize () ) );
$this->assertTrue(is_array($this->api->jsonSerialize()));
}


Expand Down

0 comments on commit 8f5d5e3

Please sign in to comment.