Passed
Push — master ( 8c1a6c...28368a )
by Yunus Emre
03:25
created

BaseMock::getJsonContentType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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