Passed
Push — master ( 0facf0...5efa68 )
by Yunus Emre
03:38
created

BaseMock::generateMeta()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace TarfinLabs\Parasut\Mocks;
4
5
use Faker\Factory;
6
use Illuminate\Http\Response;
7
use Illuminate\Support\Carbon;
8
use Illuminate\Support\Facades\Http;
9
use TarfinLabs\Parasut\Models\BaseModel;
10
use TarfinLabs\Parasut\Enums\ResourceNames;
11
12
abstract class BaseMock
13
{
14
    // region Abstract Functions
15
16
    abstract public static function all(int $count = 3): void;
17
18
    abstract public static function create(BaseModel $model): void;
19
20
    abstract public static function find(): int;
21
22
    abstract public static function update(BaseModel $model): void;
23
24
    abstract public static function delete(BaseModel $model): void;
25
26
    abstract public static function generateResponse(BaseModel $model = null): array;
27
28
    abstract public static function generateResponseMultiple(int $count = 3): array;
29
30
    abstract public static function getExtraMeta(): array;
31
32
    // endregion
33
34
    // region Helpers
35
36 10
    protected static function fakeHttp(string $resource, array $response, int $returnStatus): void
37
    {
38 10
        Http::fake([
39 10
            self::getResourceUrl($resource) => Http::response(
40 10
                $response,
41
                $returnStatus,
42 10
                self::getJsonContentType()
43
            ),
44
        ]);
45 10
    }
46
47 11
    protected static function getJsonContentType(): array
48
    {
49 11
        return ['content-type' => 'application/json; charset=utf-8'];
50
    }
51
52 11
    protected static function getAuthenticationUrl(): string
53
    {
54 11
        return ResourceNames::buildEndpoint(
55 11
            config('parasut.api_url'),
56 11
            config('parasut.token_url')
57
        );
58
    }
59
60 10
    protected static function getResourceUrl(string $resource): string
61
    {
62 10
        return ResourceNames::buildEndpoint(
63 10
            config('parasut.api_url'),
64 10
            config('parasut.api_version'),
65 10
            config('parasut.company_id'),
66
            $resource,
67
        );
68
    }
69
70 2
    protected static function generateMeta(string $resource, ?array $extraMeta = null): array
71
    {
72 2
        $faker = Factory::create('tr_TR');
73
74
        $meta = [
75 2
            'current_page' => $faker->numberBetween(1, 10),
76 2
            'total_pages'  => $faker->numberBetween(11, 100),
77 2
            'total_count'  => $faker->numberBetween(100, 1000),
78 2
            'per_page'     => $faker->numberBetween(1, 10),
79 2
            'export_url'   => "https://api.parasut.com/v4/{$faker->numberBetween(1000, 9999)}/{$resource}/export",
80
        ];
81
82 2
        return array_merge($meta, $extraMeta);
83
    }
84
85 2
    protected static function generateLinks(string $resource): array
86
    {
87 2
        $faker = Factory::create('tr_TR');
88
89
        return [
90 2
            'self' => "https://api.parasut.com/v4/141099/{$resource}?page%5Bnumber%5D={$faker->numberBetween(1, 10)}&page%5Bsize%5D={$faker->numberBetween(1, 10)}",
91 2
            'next' => "https://api.parasut.com/v4/141099/{$resource}?page%5Bnumber%5D={$faker->numberBetween(1, 10)}&page%5Bsize%5D={$faker->numberBetween(1, 10)}",
92 2
            'last' => "https://api.parasut.com/v4/141099/{$resource}?page%5Bnumber%5D={$faker->numberBetween(1, 10)}&page%5Bsize%5D={$faker->numberBetween(1, 10)}",
93
        ];
94
    }
95
96 11
    protected static function fakeAuthenticationResponse(): array
97
    {
98
        return [
99 11
            'access_token'  => 'fake-access-token',
100 11
            'token_type'    => 'bearer',
101 11
            'expires_in'    => 7200,
102 11
            'refresh_token' => 'fake-refresh-token',
103 11
            'scope'         => 'public',
104 11
            'created_at'    => 1583243989,
105 11
            'created_at'    => Carbon::now()->unix(),
106
        ];
107
    }
108
109 10
    protected static function getRelationships(): array
110
    {
111
        return [
112 10
            'category'          => ['meta' => []],
113
            'price_list'        => ['meta' => []],
114
            'contact_portal'    => ['meta' => []],
115
            'contact_people'    => ['meta' => []],
116
            'activities'        => ['meta' => []],
117
            'e_invoice_inboxes' => ['meta' => []],
118
            'sharings'          => ['meta' => []],
119
        ];
120
    }
121
122 10
    protected static function getMeta(BaseModel $model = null): array
123
    {
124 10
        $faker = Factory::create('tr_TR');
125
126
        return [
127 10
            'created_at' => $model->created_at ?? $faker->iso8601,
128 10
            'updated_at' => $model->updated_at ?? $faker->iso8601,
129
        ];
130
    }
131
132 8
    protected static function response(BaseModel $model = null, string $class, string $resource): array
133
    {
134 8
        $faker = Factory::create('tr_TR');
135
136 8
        $attributes = empty($model)
137 2
            ? factory($class)
138 2
                ->states(['creation', 'response'])
139 2
                ->raw()
140 8
            : $model->getAttributes();
141
142
        return [
143
            'data' => [
144 8
                'id'            => (string) $faker->numberBetween(10000, 99999),
145 8
                'type'          => $resource,
146 8
                'attributes'    => $attributes,
147 8
                'relationships' => self::getRelationships(),
148 8
                'meta'          => self::getMeta($model),
149
            ],
150
        ];
151
    }
152
153 2
    protected static function responseMultiple(int $count, string $class, string $resource, array $extraMeta): array
154
    {
155 2
        $data = [];
156
157 2
        foreach (range(1, $count) as $index) {
158 2
            $data['data'][$index - 1] = [
159 2
                'id'            => $index,
160 2
                'type'          => $resource,
161 2
                'attributes'    => factory($class)->states(['creation', 'response'])->raw(),
162 2
                'relationships' => self::getRelationships(),
163 2
                'meta'          => self::getMeta(),
164
            ];
165
        }
166
167 2
        $data['links'] = self::generateLinks($resource);
168 2
        $data['meta'] = self::generateMeta($resource, $extraMeta);
169
170 2
        return $data;
171
    }
172
173
    // endregion
174
175
    // region Public Functions
176
177 11
    public static function fakeAuthentication(): void
178
    {
179 11
        Http::fake([
180
            self::getAuthenticationUrl() => Http::response(
181
                self::fakeAuthenticationResponse(),
182 11
                Response::HTTP_OK,
183 11
                self::getJsonContentType()
184
            ),
185
        ]);
186 11
    }
187
188
    // endregion
189
}
190