-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(deploy): add FOREST_SEND_APIMAP_AUTOMATIC env to enable automati…
…c sending of the apimap (#39)
- Loading branch information
Showing
7 changed files
with
225 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
<?php | ||
|
||
return [ | ||
'models_directory' => env('MODEL_DIRECTORY', 'app/Models/'), | ||
'models_namespace' => env('MODEL_NAMESPACE', 'App\Models\\'), | ||
'json_file_path' => env('JSON_FILE_PATH', '.forestadmin-schema.json'), | ||
'api' => [ | ||
'models_directory' => env('MODEL_DIRECTORY', 'app/Models/'), | ||
'models_namespace' => env('MODEL_NAMESPACE', 'App\Models\\'), | ||
'json_file_path' => env('JSON_FILE_PATH', '.forestadmin-schema.json'), | ||
'send_apimap_automatic' => env('FOREST_SEND_APIMAP_AUTOMATIC', false), | ||
'api' => [ | ||
'url' => env('FOREST_URL', 'https://api.forestadmin.com'), | ||
'secret' => env('FOREST_ENV_SECRET'), | ||
'auth-secret' => env('FOREST_AUTH_SECRET'), | ||
] | ||
], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace ForestAdmin\LaravelForestAdmin\Listeners; | ||
|
||
use ForestAdmin\LaravelForestAdmin\Schema\Schema; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\App; | ||
use Illuminate\Support\Facades\Cache; | ||
use Illuminate\Support\Facades\File; | ||
use Illuminate\Routing\Events\RouteMatched as RouteMatchedEvent; | ||
use Illuminate\Support\Str; | ||
|
||
/** | ||
* Class RouteMatched | ||
* | ||
* @package Laravel-forestadmin | ||
* @license GNU https://www.gnu.org/licenses/licenses.html | ||
* @link https://github.com/ForestAdmin/laravel-forestadmin | ||
*/ | ||
class RouteMatched | ||
{ | ||
public const APIMAP_DATE = 'forest:apimap-date'; | ||
|
||
/** | ||
* @param RouteMatchedEvent $routeMatchedEvent | ||
* @return void | ||
*/ | ||
public function handle(RouteMatchedEvent $routeMatchedEvent): void | ||
{ | ||
if ($this->shouldRun($routeMatchedEvent->request) && config('forest.send_apimap_automatic')) { | ||
$filePath = App::basePath(config('forest.json_file_path')); | ||
|
||
if (File::exists($filePath)) { | ||
$date = File::lastModified($filePath); | ||
if (Cache::get(self::APIMAP_DATE) !== $date) { | ||
App::make(Schema::class)->sendApiMap(); | ||
Cache::put(self::APIMAP_DATE, $date); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param Request $request | ||
* @return bool | ||
*/ | ||
protected function shouldRun(Request $request): bool | ||
{ | ||
return Str::startsWith($request->getRequestUri(), '/forest'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
<?php | ||
|
||
namespace ForestAdmin\LaravelForestAdmin\Tests\Feature; | ||
|
||
use ForestAdmin\LaravelForestAdmin\Auth\Guard\Model\ForestUser; | ||
use ForestAdmin\LaravelForestAdmin\Exports\CollectionExport; | ||
use ForestAdmin\LaravelForestAdmin\Listeners\RouteMatched; | ||
use ForestAdmin\LaravelForestAdmin\Schema\Schema; | ||
use ForestAdmin\LaravelForestAdmin\Services\ForestApiRequester; | ||
use ForestAdmin\LaravelForestAdmin\Tests\Feature\Models\Book; | ||
use ForestAdmin\LaravelForestAdmin\Tests\Feature\Models\Category; | ||
use ForestAdmin\LaravelForestAdmin\Tests\TestCase; | ||
use ForestAdmin\LaravelForestAdmin\Tests\Utils\FakeData; | ||
use ForestAdmin\LaravelForestAdmin\Tests\Utils\FakeSchema; | ||
use ForestAdmin\LaravelForestAdmin\Tests\Utils\MockForestUserFactory; | ||
use ForestAdmin\LaravelForestAdmin\Tests\Utils\ScopeManagerFactory; | ||
use Illuminate\Contracts\Config\Repository; | ||
use Illuminate\Foundation\Application; | ||
use Illuminate\Support\Facades\App; | ||
use Illuminate\Support\Facades\Cache; | ||
use Illuminate\Support\Facades\File; | ||
use Maatwebsite\Excel\Facades\Excel; | ||
use Prophecy\Argument; | ||
use Symfony\Component\Console\Output\ConsoleOutput; | ||
|
||
/** | ||
* Class ResourcesControllerTest | ||
* | ||
* @package Laravel-forestadmin | ||
* @license GNU https://www.gnu.org/licenses/licenses.html | ||
* @link https://github.com/ForestAdmin/laravel-forestadmin | ||
*/ | ||
class RouteMatchedTest extends TestCase | ||
{ | ||
use FakeSchema; | ||
use MockForestUserFactory; | ||
use ScopeManagerFactory; | ||
|
||
/** | ||
* @var ForestUser | ||
*/ | ||
private ForestUser $forestUser; | ||
|
||
/** | ||
* @param Application $app | ||
* @return void | ||
*/ | ||
protected function getEnvironmentSetUp($app): void | ||
{ | ||
parent::getEnvironmentSetUp($app); | ||
$app['config']->set('forest.models_namespace', 'ForestAdmin\LaravelForestAdmin\Tests\Feature\Models\\'); | ||
$app['config']->set('forest.send_apimap_automatic', true); | ||
} | ||
|
||
/** | ||
* @return 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>')); | ||
App::shouldReceive('make')->andReturn($schema); | ||
|
||
$this->assertNull(Cache::get(RouteMatched::APIMAP_DATE)); | ||
$this->get('/forest'); | ||
$this->assertNull(Cache::get(RouteMatched::APIMAP_DATE)); | ||
} | ||
|
||
/** | ||
* @return 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>')); | ||
App::shouldReceive('make')->andReturn($schema); | ||
|
||
$this->assertNull(Cache::get(RouteMatched::APIMAP_DATE)); | ||
$this->get('/foo'); | ||
$this->assertNull(Cache::get(RouteMatched::APIMAP_DATE)); | ||
} | ||
|
||
/** | ||
* @return 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>')); | ||
App::shouldReceive('make')->andReturn($schema); | ||
file_put_contents(App::basePath(config('forest.json_file_path')), '{}'); | ||
|
||
$this->assertNull(Cache::get(RouteMatched::APIMAP_DATE)); | ||
$this->get('/forest'); | ||
$this->assertNotNull(Cache::get(RouteMatched::APIMAP_DATE)); | ||
$this->assertEquals(File::lastModified(config('forest.json_file_path')), Cache::get(RouteMatched::APIMAP_DATE)); | ||
File::delete(config('forest.json_file_path')); | ||
} | ||
|
||
/** | ||
* @return 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>')); | ||
App::shouldReceive('make')->andReturn($schema); | ||
file_put_contents(App::basePath(config('forest.json_file_path')), '{}'); | ||
|
||
Cache::put(RouteMatched::APIMAP_DATE, File::lastModified(config('forest.json_file_path'))); | ||
$this->get('/forest'); | ||
$this->assertEquals(File::lastModified(config('forest.json_file_path')), Cache::get(RouteMatched::APIMAP_DATE)); | ||
File::delete(config('forest.json_file_path')); | ||
} | ||
|
||
/** | ||
* @return object | ||
*/ | ||
public function forestApiPost() | ||
{ | ||
$forestApiPost = $this->prophesize(ForestApiRequester::class); | ||
$forestApiPost | ||
->post(Argument::type('string'), Argument::size(0), Argument::type('array')) | ||
->shouldBeCalled() | ||
->willReturn( | ||
new \GuzzleHttp\Psr7\Response(204, [], null) | ||
); | ||
|
||
return $forestApiPost->reveal(); | ||
} | ||
|
||
/** | ||
* @return object | ||
*/ | ||
private function getConfig() | ||
{ | ||
$config = $this->prophesize(Repository::class); | ||
$config | ||
->get('database.default') | ||
->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 | ||
*/ | ||
public function getConsole() | ||
{ | ||
$console = $this->prophesize(ConsoleOutput::class); | ||
|
||
return $console->reveal(); | ||
} | ||
} |