Skip to content

Commit

Permalink
feat(config): add include/exclude models settings (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthv authored May 9, 2022
1 parent 456bd57 commit 1dcab5a
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 56 deletions.
2 changes: 2 additions & 0 deletions config/forest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@
'secret' => env('FOREST_ENV_SECRET'),
'auth-secret' => env('FOREST_AUTH_SECRET'),
],
'included_models' => [],
'excluded_models' => [],
];
20 changes: 18 additions & 2 deletions src/Schema/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Response;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\File;
Expand Down Expand Up @@ -90,6 +89,23 @@ public function sendApiMap(): void
}
}

/**
* @param string $modelName
* @return bool
*/
public function modelIncluded(string $modelName): bool
{
if (empty(config('forest.included_models')) && empty(config('forest.excluded_models'))) {
return true;
}

if (!empty(config('forest.included_models'))) {
return in_array($modelName, config('forest.included_models'), true);
} else {
return !in_array($modelName, config('forest.excluded_models'), true);
}
}

/**
* @return array
* @throws BindingResolutionException
Expand All @@ -105,7 +121,7 @@ private function generate(): array
foreach ($files as $file) {
if (class_exists($file)) {
$class = (new \ReflectionClass($file));
if ($class->isSubclassOf(Model::class) && $class->isInstantiable()) {
if ($class->isSubclassOf(Model::class) && $class->isInstantiable() && $this->modelIncluded($file)) {
$model = app()->make($file);
$forestModel = new ForestModel($model);
$collections[] = $forestModel->serialize();
Expand Down
32 changes: 4 additions & 28 deletions tests/Feature/Listeners/RouteMatchedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ protected function getEnvironmentSetUp($app): void
*/
public function testApiMapNotSendWithOutFile(): void
{
App::partialMock()->shouldReceive('basePath')->andReturn(config('forest.json_file_path'));
$schema = new Schema($this->getConfig(), new ForestApiRequester(), $this->getConsole('<info>Apimap Received<info>'));
$schema = new Schema(config(), new ForestApiRequester(), $this->getConsole('<info>Apimap Received<info>'));
App::shouldReceive('make')->andReturn($schema);

$this->assertNull(Cache::get(RouteMatched::APIMAP_DATE));
Expand All @@ -71,8 +70,7 @@ public function testApiMapNotSendWithOutFile(): void
*/
public function testApiMapRouteNotMatchPattern(): void
{
App::partialMock()->shouldReceive('basePath')->andReturn(config('forest.json_file_path'));
$schema = new Schema($this->getConfig(), new ForestApiRequester(), $this->getConsole('<info>Apimap Received<info>'));
$schema = new Schema(config(), new ForestApiRequester(), $this->getConsole('<info>Apimap Received<info>'));
App::shouldReceive('make')->andReturn($schema);

$this->assertNull(Cache::get(RouteMatched::APIMAP_DATE));
Expand All @@ -86,7 +84,7 @@ public function testApiMapRouteNotMatchPattern(): void
public function testApiMapSend(): void
{
App::partialMock()->shouldReceive('basePath')->andReturn(config('forest.json_file_path'));
$schema = new Schema($this->getConfig(), $this->forestApiPost(), $this->getConsole('<info>Apimap Received<info>'));
$schema = new Schema(config(), $this->forestApiPost(), $this->getConsole('<info>Apimap Received<info>'));
App::shouldReceive('make')->andReturn($schema);
file_put_contents(App::basePath(config('forest.json_file_path')), '{}');

Expand All @@ -103,7 +101,7 @@ public function testApiMapSend(): void
public function testApiMapSendOnce(): void
{
App::partialMock()->shouldReceive('basePath')->andReturn(config('forest.json_file_path'));
$schema = new Schema($this->getConfig(), new ForestApiRequester(), $this->getConsole('<info>Apimap Received<info>'));
$schema = new Schema(config(), new ForestApiRequester(), $this->getConsole('<info>Apimap Received<info>'));
App::shouldReceive('make')->andReturn($schema);
file_put_contents(App::basePath(config('forest.json_file_path')), '{}');

Expand All @@ -129,28 +127,6 @@ public function forestApiPost()
return $forestApiPost->reveal();
}

/**
* @return object
*/
private function getConfig()
{
$config = $this->prophesize(Repository::class);
$config
->get('database.default')
->willReturn('sqlite');
$config
->get('database.connections.sqlite.driver')
->willReturn('sqlite');
$config
->get('forest.models_directory')
->willReturn(__DIR__ . '/../Feature/Models');
$config
->get('forest.json_file_path')
->willReturn('.forestadmin-schema.json');

return $config->reveal();
}

/**
* @return object
*/
Expand Down
3 changes: 2 additions & 1 deletion tests/Feature/RelationshipsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ public function testAssociateMorphMany(): void
public function testAssociateBelongsToMany(): void
{
$book = Book::first();
$range = Range::whereRelation('books', 'books.id', '!=', $book->id)->first();
$range = Range::whereDoesntHave('books', static fn($query) => $query->where('books.id', $book->id))->first();

$call = $this->post(
'/forest/book/' . $book->id . '/relationships/ranges',
[
Expand Down
2 changes: 2 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ protected function getEnvironmentSetUp($app): void
parent::getEnvironmentSetUp($app);
$config = $app['config'];
$config->set('app.debug', true);
$config->set('database.default', 'sqlite');
$config->set('database.connections.sqlite.database', ':memory:');
$config->set('forest.api.secret', 'my-secret-key');
$config->set('forest.api.auth-secret', 'auth-secret-key');
$config->set('forest.models_namespace', 'ForestAdmin\LaravelForestAdmin\Tests\Utils\Models\\');
Expand Down
55 changes: 31 additions & 24 deletions tests/Unit/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class SchemaTest extends TestCase
*/
public function testFetchFiles(): void
{
$schema = new Schema($this->getConfig(), $this->getForestApi(), $this->getConsole());
$schema = new Schema(config(), $this->getForestApi(), $this->getConsole());
$this->invokeProperty($schema, 'directory', __DIR__ . '/../Feature/Models');
$files = $this->invokeMethod($schema, 'fetchFiles');

Expand All @@ -55,7 +55,7 @@ public function testFetchFiles(): void
*/
public function testMetadata(): void
{
$schema = new Schema($this->getConfig(), $this->getForestApi(), $this->getConsole());
$schema = new Schema(config(), $this->getForestApi(), $this->getConsole());
$metadata = $this->invokeMethod($schema, 'metadata');

$this->assertIsArray($metadata);
Expand All @@ -75,9 +75,7 @@ public function testMetadata(): void
*/
public function testGenerate(): void
{
App::partialMock()->shouldReceive('basePath')
->andReturn(__DIR__ . '/../Feature/Models');
$schema = new Schema($this->getConfig(), $this->getForestApi(), $this->getConsole());
$schema = new Schema(config(), $this->getForestApi(), $this->getConsole());
File::shouldReceive('put')->andReturn(true);
$generate = $this->invokeMethod($schema, 'generate');

Expand All @@ -87,33 +85,42 @@ public function testGenerate(): void
}

/**
* @return object
* @return void
*/
private function getForestApi()
public function testModelIncludedWithoutIncludedOrExcludedModels(): void
{
return $this->prophesize(ForestApiRequester::class)->reveal();
$schema = new Schema(config(), $this->getForestApi(), $this->getConsole());
$this->assertTrue($schema->modelIncluded('foo'));
}

/**
* @return void
*/
public function testModelIncludedWithIncludedModelsConfig(): void
{
config()->set('forest.included_models', ['foo', 'bar']);
$schema = new Schema(config(), $this->getForestApi(), $this->getConsole());
$this->assertTrue($schema->modelIncluded('foo'));
$this->assertFalse($schema->modelIncluded('foo2'));
}

/**
* @return void
*/
public function testModelIncludedWithExcludedModelsConfig(): void
{
config()->set('forest.excluded_models', ['foo', 'bar']);
$schema = new Schema(config(), $this->getForestApi(), $this->getConsole());
$this->assertFalse($schema->modelIncluded('foo'));
$this->assertTrue($schema->modelIncluded('foo2'));
}

/**
* @return object
*/
private function getConfig()
private function getForestApi()
{
$config = $this->prophesize(Repository::class);
$config
->get('database.default')
->willReturn('sqlite');
$config
->get('database.connections.sqlite.driver')
->willReturn('sqlite');
$config
->get('forest.models_directory')
->willReturn(__DIR__ . '/../Feature/Models');
$config
->get('forest.json_file_path')
->willReturn('.forestadmin-schema.json');

return $config->reveal();
return $this->prophesize(ForestApiRequester::class)->reveal();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace ForestAdmin\LaravelForestAdmin\Tests\Feature\Models\SmartCollections;
namespace ForestAdmin\LaravelForestAdmin\Tests\Utils\Models\SmartCollections;

use ForestAdmin\LaravelForestAdmin\Services\SmartFeatures\SmartCollection;
use ForestAdmin\LaravelForestAdmin\Services\SmartFeatures\SmartField;
Expand Down

0 comments on commit 1dcab5a

Please sign in to comment.