|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace VGirol\JsonApiFaker\Laravel\Testing; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
use VGirol\JsonApiAssert\Members; |
|
8
|
|
|
|
|
9
|
|
|
trait CanCreateFake |
|
10
|
|
|
{ |
|
11
|
|
|
protected $modelClass = DummyModel::class; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Undocumented function |
|
15
|
|
|
* |
|
16
|
|
|
* @return DummyModel |
|
17
|
|
|
*/ |
|
18
|
21 |
|
protected function createModel(): DummyModel |
|
19
|
|
|
{ |
|
20
|
21 |
|
return ($this->modelClass)::fake(); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Undocumented function |
|
25
|
|
|
* |
|
26
|
|
|
* @param integer $count |
|
27
|
|
|
* |
|
28
|
|
|
* @return Collection |
|
29
|
|
|
*/ |
|
30
|
11 |
|
protected function createCollection($count = 5): Collection |
|
31
|
|
|
{ |
|
32
|
11 |
|
$collection = new Collection(); |
|
33
|
11 |
|
for ($i = 1; $i <= $count; $i++) { |
|
34
|
11 |
|
$collection->push($this->createModel()); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
11 |
|
return $collection; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Undocumented function |
|
42
|
|
|
* |
|
43
|
|
|
* @param Model $model |
|
44
|
|
|
* @param string $resourceType |
|
45
|
|
|
* @param boolean $isResourceIdentifier |
|
46
|
|
|
* @param string|null $withError |
|
47
|
|
|
* @param array|null $additional |
|
48
|
|
|
* |
|
49
|
|
|
* @return array |
|
50
|
|
|
*/ |
|
51
|
5 |
|
protected function createResource( |
|
52
|
|
|
$model, |
|
53
|
|
|
string $resourceType, |
|
54
|
|
|
bool $isResourceIdentifier, |
|
55
|
|
|
$withError = null, |
|
56
|
|
|
$additional = null |
|
57
|
|
|
): array { |
|
58
|
|
|
$resource = [ |
|
59
|
5 |
|
Members::TYPE => $resourceType, |
|
60
|
5 |
|
Members::ID => strval($model->getKey()) |
|
61
|
|
|
]; |
|
62
|
5 |
|
if (!$isResourceIdentifier) { |
|
63
|
4 |
|
$resource[Members::ATTRIBUTES] = $model->attributesToArray(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
5 |
|
switch ($withError) { |
|
67
|
5 |
|
case 'value': |
|
68
|
|
|
$error = 10; |
|
69
|
|
|
$resource[Members::ID] = strval($model->getKey() + $error); |
|
70
|
|
|
break; |
|
71
|
5 |
|
case 'structure': |
|
72
|
|
|
$resource[Members::ID] = intval($model->getKey()); |
|
73
|
|
|
break; |
|
74
|
|
|
default: |
|
75
|
5 |
|
break; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
5 |
|
if ($additional !== null) { |
|
79
|
4 |
|
$resource = array_merge($resource, $additional); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
5 |
|
return $resource; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Undocumented function |
|
87
|
|
|
* |
|
88
|
|
|
* @param Collection $collection |
|
89
|
|
|
* @param string $resourceType |
|
90
|
|
|
* @param bool $isResourceIdentifier |
|
91
|
|
|
* @param string|null $withError |
|
92
|
|
|
* |
|
93
|
|
|
* @return array |
|
94
|
|
|
*/ |
|
95
|
3 |
|
protected function createResourceCollection( |
|
96
|
|
|
Collection $collection, |
|
97
|
|
|
string $resourceType, |
|
98
|
|
|
bool $isResourceIdentifier, |
|
99
|
|
|
$withError = null |
|
100
|
|
|
): array { |
|
101
|
3 |
|
$data = []; |
|
102
|
3 |
|
$index = rand(1, $collection->count() - 1); |
|
103
|
3 |
|
foreach ($collection as $i => $model) { |
|
104
|
3 |
|
array_push( |
|
105
|
3 |
|
$data, |
|
106
|
3 |
|
$this->createResource( |
|
107
|
3 |
|
$model, |
|
108
|
3 |
|
$resourceType, |
|
109
|
3 |
|
$isResourceIdentifier, |
|
110
|
3 |
|
(($withError !== null) && ($i == $index)) ? $withError : null |
|
111
|
|
|
) |
|
112
|
|
|
); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
3 |
|
return $data; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|