forked from Power-Components/livewire-powergrid
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
013b44f
commit 6f70943
Showing
5 changed files
with
84 additions
and
20 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
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,20 @@ | ||
<?php | ||
|
||
namespace PowerComponents\LivewirePowerGrid\Actions; | ||
|
||
final class ParseFqnClassInCode | ||
{ | ||
/** | ||
* Parse namespace from PHP source code | ||
* Inspired by: https://gist.github.com/ludofleury/1886076 | ||
* @throws \Exception | ||
*/ | ||
public static function handle(string $sourceCode): string | ||
{ | ||
if (preg_match('#^namespace\s+(.+?);.*class\s+(\w+).+;$#sm', $sourceCode, $matches)) { | ||
return $matches[1] . '\\' . $matches[2]; | ||
} | ||
|
||
throw new \Exception('could not find a FQN Class is source-code'); | ||
} | ||
} |
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,20 +1,28 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\File; | ||
<?php | ||
|
||
use PowerComponents\LivewirePowerGrid\Actions\ListModels; | ||
|
||
beforeEach(function () { | ||
File::cleanDirectory(base_path('app/Models')); | ||
|
||
$this->artisan('make:model Demo'); | ||
}); | ||
it('list all Eloquent Models in a directory', function () { | ||
app()->config->set('livewire-powergrid.auto_discover_models_paths', [ | ||
'tests/Concerns/Models', | ||
]); | ||
|
||
test('list models', function () { | ||
expect(ListModels::handle())->toBe( | ||
[ | ||
'Demo', | ||
'App\Models\Demo', | ||
'PowerComponents\LivewirePowerGrid\Tests\Concerns\Models\Category', | ||
'PowerComponents\LivewirePowerGrid\Tests\Concerns\Models\Chef', | ||
'PowerComponents\LivewirePowerGrid\Tests\Concerns\Models\Dish', | ||
'PowerComponents\LivewirePowerGrid\Tests\Concerns\Models\Restaurant', | ||
] | ||
); | ||
}); | ||
|
||
it('will not list non-Eloquent Models', function () { | ||
app()->config->set('livewire-powergrid.auto_discover_models_paths', [ | ||
'tests/Concerns/Enums', //There are no models in this directory. | ||
|
||
]); | ||
|
||
expect(ListModels::handle())->toBe([]); | ||
}); |
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,25 @@ | ||
<?php | ||
|
||
use PowerComponents\LivewirePowerGrid\Actions\ParseFqnClassInCode; | ||
|
||
it('can find the namespace in a PHP file source code', function () { | ||
$code = <<<EOD | ||
<?php | ||
namespace App\Models; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
class DemoModel extends Model | ||
{ | ||
use HasFactory; | ||
} | ||
EOD; | ||
|
||
expect(ParseFqnClassInCode::handle($code))->toBe('App\Models\DemoModel'); | ||
}); | ||
|
||
it('throws an exception when namespace cannot be found', function () { | ||
ParseFqnClassInCode::handle('foobar'); | ||
})->throws('could not find a FQN Class is source-code'); |