generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added all ConditionallyLoadsAttributes methods support (#652)
* added all ConditionallyLoadsAttributes methods support * added basic test * Fix styling --------- Co-authored-by: romalytvynenko <[email protected]>
- Loading branch information
1 parent
51aa306
commit 43d8f75
Showing
4 changed files
with
209 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
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,78 @@ | ||
<?php | ||
|
||
use Dedoc\Scramble\Infer; | ||
use Dedoc\Scramble\Support\Generator\Components; | ||
use Dedoc\Scramble\Support\Generator\TypeTransformer; | ||
use Dedoc\Scramble\Support\Type\ObjectType; | ||
use Dedoc\Scramble\Support\TypeToSchemaExtensions\JsonResourceTypeToSchema; | ||
use Dedoc\Scramble\Support\TypeToSchemaExtensions\ModelToSchema; | ||
use Dedoc\Scramble\Tests\Files\SamplePostModel; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
uses(RefreshDatabase::class); | ||
|
||
beforeEach(function () { | ||
$this->infer = app(Infer::class); | ||
}); | ||
|
||
/** | ||
* @return array{0: \Dedoc\Scramble\Support\Generator\Types\Type, 1: Components} | ||
*/ | ||
function JsonResourceExtensionTest_analyze(Infer $infer, string $class) | ||
{ | ||
$transformer = new TypeTransformer($infer, $components = new Components, [ | ||
ModelToSchema::class, | ||
JsonResourceTypeToSchema::class, | ||
]); | ||
$extension = new JsonResourceTypeToSchema($infer, $transformer, $components); | ||
|
||
$type = new ObjectType($class); | ||
|
||
$openApiType = $extension->toSchema($type); | ||
|
||
return [$openApiType, $components]; | ||
} | ||
|
||
it('supports whenHas', function () { | ||
[$schema] = JsonResourceExtensionTest_analyze($this->infer, JsonResourceExtensionTest_WhenHas::class); | ||
|
||
expect($schema->toArray())->toBe([ | ||
'type' => 'object', | ||
'properties' => [ | ||
'user' => [ | ||
'$ref' => '#/components/schemas/SampleUserModel', | ||
], | ||
'value' => [ | ||
'type' => 'integer', | ||
'example' => 42, | ||
], | ||
'default' => [ | ||
'anyOf' => [ | ||
[ | ||
'type' => 'string', | ||
'enum' => ['foo'], | ||
], | ||
[ | ||
'type' => 'integer', | ||
'enum' => [42], | ||
], | ||
], | ||
], | ||
], | ||
'required' => ['default'], | ||
]); | ||
}); | ||
/** @mixin SamplePostModel */ | ||
class JsonResourceExtensionTest_WhenHas extends JsonResource | ||
{ | ||
public function toArray(Request $request) | ||
{ | ||
return [ | ||
'user' => $this->whenHas('user'), | ||
'value' => $this->whenHas('user', 42), | ||
'default' => $this->whenHas('user', 42, 'foo'), | ||
]; | ||
} | ||
} |