1 | <?php |
||||||
2 | |||||||
3 | namespace Digitonic\ApiTestSuite\Concerns; |
||||||
4 | |||||||
5 | use Digitonic\ApiTestSuite\DataGeneration\RuleParser; |
||||||
6 | use Illuminate\Foundation\Testing\TestResponse; |
||||||
7 | use Illuminate\Routing\Middleware\ThrottleRequests; |
||||||
8 | use Illuminate\Support\Collection; |
||||||
9 | |||||||
10 | trait GeneratesTestData |
||||||
11 | { |
||||||
12 | /** |
||||||
13 | * @var Collection |
||||||
14 | */ |
||||||
15 | public $entities; |
||||||
16 | |||||||
17 | public $payload; |
||||||
18 | |||||||
19 | /** |
||||||
20 | * @param $numberOfEntities |
||||||
21 | * @param $httpAction |
||||||
22 | * @param $baseUser |
||||||
23 | * @param $otherUser |
||||||
24 | */ |
||||||
25 | public function generateEntities($numberOfEntities, $httpAction, $baseUser, $otherUser) |
||||||
26 | { |
||||||
27 | $this->payload = $this->generatePayload($baseUser); |
||||||
28 | if (in_array($httpAction, ['put', 'get', 'delete'])) { |
||||||
29 | $this->entities->push($this->generateSingleEntity($baseUser, $this->payload)); |
||||||
30 | } |
||||||
31 | for ($i = 1; $i < $numberOfEntities; $i++) { |
||||||
32 | $this->entities->push($this->generateSingleEntity($baseUser)); |
||||||
33 | } |
||||||
34 | if ($httpAction === 'get' && $this->viewableByOwnerOnly()) { |
||||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
35 | $this->entities->push($this->generateSingleEntity($otherUser)); |
||||||
36 | } |
||||||
37 | } |
||||||
38 | |||||||
39 | public function generatePayload($user) |
||||||
40 | { |
||||||
41 | $payload = []; |
||||||
42 | $rules = $this->creationRules(); |
||||||
0 ignored issues
–
show
It seems like
creationRules() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
43 | foreach ($rules as $field => $rule) { |
||||||
44 | $ruleParser = new RuleParser(); |
||||||
45 | $ruleSet = $ruleParser->parse($rule); |
||||||
46 | $ruleSet->generate($payload, $field, $rules, random_int(0, 999999999), $this->resourceClass(), $user); |
||||||
0 ignored issues
–
show
It seems like
resourceClass() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
47 | } |
||||||
48 | |||||||
49 | return $payload; |
||||||
50 | } |
||||||
51 | |||||||
52 | public function generateSingleEntity($user, $payload = null) |
||||||
53 | { |
||||||
54 | if (!$payload) { |
||||||
55 | $payload = $this->generatePayload($user); |
||||||
56 | } |
||||||
57 | |||||||
58 | if (is_string($this->createResource())) { |
||||||
0 ignored issues
–
show
It seems like
createResource() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
59 | return $this->generateEntityOverApi($payload, $user); |
||||||
60 | } else { |
||||||
61 | return $this->createResource()->call($this, ['payload' => $payload, 'user' => $user]); |
||||||
62 | } |
||||||
63 | } |
||||||
64 | |||||||
65 | public function generateEntityOverApi(array $payload, $user) |
||||||
66 | { |
||||||
67 | $this->withoutMiddleware(ThrottleRequests::class); |
||||||
0 ignored issues
–
show
It seems like
withoutMiddleware() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
68 | /** @var TestResponse $response */ |
||||||
69 | $response = $this->actingAs($user)->call( |
||||||
0 ignored issues
–
show
It seems like
actingAs() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
70 | 'post', |
||||||
71 | route($this->createResource()), |
||||||
72 | $payload, |
||||||
73 | [], |
||||||
74 | [], |
||||||
75 | $this->creationHeaders() |
||||||
0 ignored issues
–
show
It seems like
creationHeaders() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
76 | ); |
||||||
77 | $this->withMiddleware(ThrottleRequests::class); |
||||||
0 ignored issues
–
show
It seems like
withMiddleware() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
78 | |||||||
79 | $id = json_decode($response->getContent(), true)['data'][$this->identifier()]; |
||||||
80 | |||||||
81 | return $this->resourceClass()::where([$this->identifier() => $id])->first(); |
||||||
82 | } |
||||||
83 | |||||||
84 | protected function identifier() |
||||||
85 | { |
||||||
86 | return config('digitonic.api-test-suite.identifier_field')->call($this); |
||||||
87 | } |
||||||
88 | |||||||
89 | /** |
||||||
90 | * @param $payload |
||||||
91 | * @param $user |
||||||
92 | * @return array |
||||||
93 | * @throws \Exception |
||||||
94 | */ |
||||||
95 | protected function generateUpdateData($payload, $user) |
||||||
96 | { |
||||||
97 | foreach ($this->creationRules() as $field => $rule) { |
||||||
98 | if (strpos($field, $this->identifier()) === false) { |
||||||
99 | $ruleParser = new RuleParser(); |
||||||
100 | $ruleSet = $ruleParser->parse($rule); |
||||||
101 | $ruleSet->generate( |
||||||
102 | $payload, |
||||||
103 | $field, |
||||||
104 | $this->creationRules(), |
||||||
105 | random_int(0, 999999999), |
||||||
106 | $this->resourceClass(), |
||||||
107 | $user |
||||||
108 | ); |
||||||
109 | } |
||||||
110 | } |
||||||
111 | |||||||
112 | return $payload; |
||||||
113 | } |
||||||
114 | |||||||
115 | /** |
||||||
116 | * @return string|null |
||||||
117 | */ |
||||||
118 | protected function getCurrentIdentifier() |
||||||
119 | { |
||||||
120 | $identifier = $this->identifier(); |
||||||
121 | return $this->entities->isEmpty() ? null : $this->entities->first()->$identifier; |
||||||
122 | } |
||||||
123 | } |
||||||
124 |