You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.3 KiB

2 years ago
  1. <?php
  2. namespace Tests\Base;
  3. use App\Documents\ModelDocument;
  4. use App\Documents\PolicyDocument;
  5. use Illuminate\Support\Str;
  6. trait DynamicPolicyAndModelTrait
  7. {
  8. protected function makeModel($modelName, $policy = null)
  9. {
  10. $data['name'] = $modelName;
  11. $data['index_policy'] = $policy?->id;
  12. $data['store_policy'] = $policy?->id;
  13. $data['update_policy'] = $policy?->id;
  14. $data['get_policy'] = $policy?->id;
  15. $data['delete_policy'] = $policy?->id;
  16. $data['restore_policy'] = $policy?->id;
  17. $data['trash_policy'] = $policy?->id;
  18. return ModelDocument::factory()->create($data);
  19. }
  20. protected function guessModelName($name): string
  21. {
  22. $name = explode('.', $name);
  23. $name = count($name) > 2 ? $name[1] : $name[0];
  24. return Str::singular($name);
  25. }
  26. protected function provideDynamicModel(string|null $modelName = null, array $needs = [])
  27. {
  28. throw_if(empty($modelName), 'name must be provided.');
  29. return $this->makeModel(Str::singular($modelName), $this->one(PolicyDocument::class, ['needs' => [$needs]]));
  30. }
  31. protected function modelWithPolicy($modelName = null, $needs)
  32. {
  33. $this->provideDynamicModel($modelName, $needs);
  34. return $this;
  35. }
  36. }