-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: 💥 Handled model option by context in Policies
- When passing the `--model` options together with the `--context` option then the `use` statement in the policy must follow the context namespace, not the laravel pattern. - When not using the `--context` option, only the `--model` option, the model must be created in the default Laravel folder closes #5
- Loading branch information
1 parent
793ce53
commit abfd06d
Showing
3 changed files
with
159 additions
and
5 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
117 changes: 117 additions & 0 deletions
117
tests/Unit/Commands/Foundation/PolicyMakeCommandTest.php
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,117 @@ | ||
<?php | ||
|
||
namespace Allyson\ArtisanDomainContext\Tests\Unit\Commands\Foundation; | ||
|
||
use PhpToken; | ||
use Illuminate\Support\Facades\File; | ||
use Allyson\ArtisanDomainContext\Tests\Unit\MakeCommandTestCase; | ||
|
||
/** | ||
* @group Making | ||
*/ | ||
class PolicyMakeCommandTest extends MakeCommandTestCase | ||
{ | ||
private string $commandName = 'make:policy'; | ||
private string $returnMessage = 'Policy created successfully.'; | ||
|
||
/** | ||
* Class name for use in assertions. | ||
* | ||
* @return string | ||
*/ | ||
protected function className(): string | ||
{ | ||
return "Foo{$this->randomString()}Policy"; | ||
} | ||
|
||
/** | ||
* Model class name for use in assertions. | ||
* | ||
* @return string | ||
*/ | ||
protected function modelClassName(): string | ||
{ | ||
return 'YourModel'; | ||
} | ||
|
||
/** | ||
* Component folder name. Where are the component classes. | ||
* | ||
* @return string | ||
*/ | ||
protected function componentFolder(): string | ||
{ | ||
return config('context.folders.components.policies'); | ||
} | ||
|
||
/** | ||
* @test | ||
* @testdox When using the `--model` option and also the `--context` options, then the model must be referenced to the context namespace | ||
*/ | ||
public function creatingPolicyWithCustomModel() | ||
{ | ||
$policyClassName = $this->className(); | ||
|
||
$modelsFolder = config('context.folders.components.models'); | ||
$modelsComponentNamespace = $this->getDomainComponentNamespace($modelsFolder, $this->contextFolder) . "\\{$this->modelClassName()}"; | ||
|
||
$this->artisan($this->commandName, [ | ||
'--context' => $this->contextFolder, | ||
'--model' => $this->modelClassName(), | ||
'name' => $policyClassName | ||
]) | ||
->assertSuccessful() | ||
->expectsOutput($this->returnMessage); | ||
|
||
$policyFilepath = $this->getComponentFilepath($policyClassName, $this->componentFolder(), $this->contextFolder); | ||
$hasModelInUseStatement = $this->hasModelInUseStatement($policyFilepath, $modelsComponentNamespace); | ||
|
||
File::delete($policyFilepath); | ||
|
||
self::assertTrue($hasModelInUseStatement); | ||
} | ||
|
||
/** | ||
* @test | ||
* @testdox Creating a policy in laravel's default folder with a custom model with the `--model` option | ||
*/ | ||
public function creatingPolicyWithCustomModelInLaravelDefaultFolder() | ||
{ | ||
$policyClassName = $this->className(); | ||
$modelsPath = app_path('Models'); | ||
|
||
File::makeDirectory(path: $modelsPath, force: true); | ||
|
||
$this->artisan($this->commandName, [ | ||
'--model' => $this->modelClassName(), | ||
'name' => $policyClassName | ||
]) | ||
->assertSuccessful() | ||
->expectsOutput($this->returnMessage); | ||
|
||
$policyFilepath = app_path("Policies/{$policyClassName}.php"); | ||
$hasModelInUseStatement = $this->hasModelInUseStatement($policyFilepath, "App\\Models\\{$this->modelClassName()}"); | ||
|
||
File::delete($policyFilepath); | ||
File::deleteDirectory($modelsPath); | ||
|
||
self::assertTrue($hasModelInUseStatement); | ||
} | ||
|
||
/** | ||
* Checks if the policy class has a use statement referring to the model. | ||
* | ||
* @param string $policyFilepath | ||
* @param string $modelsComponentNamespace | ||
* | ||
* @return bool | ||
*/ | ||
private function hasModelInUseStatement(string $policyFilepath, string $modelsComponentNamespace): bool | ||
{ | ||
$policyTokens = PhpToken::tokenize(file_get_contents($policyFilepath)); | ||
$tokensFullQualified = array_filter($policyTokens, fn (PhpToken $token) => $token->getTokenName() === 'T_NAME_QUALIFIED'); | ||
$containsModelInUseStatement = ! empty(array_filter($tokensFullQualified, fn (PhpToken $token) => $token->text === $modelsComponentNamespace)); | ||
|
||
return $containsModelInUseStatement; | ||
} | ||
} |