Passed
Push — master ( e0ab5f...89e278 )
by Yunus Emre
03:45
created

ParasutMock::deleteContact()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace TarfinLabs\Parasut\Tests\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\Contact;
10
11
class ParasutMock
12
{
13
    private static function getJsonContentType(): array
14
    {
15
        return ['content-type' => 'application/json; charset=utf-8'];
16
    }
17
18
    private static function getAuthenticationUrl(): string
19
    {
20
        return implode('/', [
21
            config('parasut.api_url'),
22
            config('parasut.token_url'),
23
        ]);
24
    }
25
26
    private static function getResourceUrl(string $resource): string
27
    {
28
        return implode('/', [
29
            config('parasut.api_url'),
30
            config('parasut.api_version'),
31
            config('parasut.company_id'),
32
            $resource,
33
        ]);
34
    }
35
36
    private static function generateMeta($faker, string $resource): array
37
    {
38
        return [
39
            'current_page' => $faker->numberBetween(1, 10),
40
            'total_pages'  => $faker->numberBetween(11, 100),
41
            'total_count'  => $faker->numberBetween(100, 1000),
42
            'per_page'     => $faker->numberBetween(1, 10),
43
            'export_url'   => "https://api.parasut.com/v4/{$faker->numberBetween(1000, 9999)}/{$resource}/export",
44
        ];
45
    }
46
47
    private static function generateLinks($faker, string $resource): array
48
    {
49
        return [
50
            'self' => "https://api.parasut.com/v4/141099/{$resource}?page%5Bnumber%5D={$faker->numberBetween(1, 10)}&page%5Bsize%5D={$faker->numberBetween(1, 10)}",
51
            'next' => "https://api.parasut.com/v4/141099/{$resource}?page%5Bnumber%5D={$faker->numberBetween(1, 10)}&page%5Bsize%5D={$faker->numberBetween(1, 10)}",
52
            'last' => "https://api.parasut.com/v4/141099/{$resource}?page%5Bnumber%5D={$faker->numberBetween(1, 10)}&page%5Bsize%5D={$faker->numberBetween(1, 10)}",
53
        ];
54
    }
55
56
    public static function fakeAuthentication(): void
57
    {
58
        Http::fake([
59
            self::getAuthenticationUrl() => Http::response(
60
                self::fakeAuthenticationResponse(),
61
                Response::HTTP_OK,
62
                self::getJsonContentType()
63
            ),
64
        ]);
65
    }
66
67
    private static function fakeAuthenticationResponse(): array
68
    {
69
        return [
70
            'access_token'  => 'fake-access-token',
71
            'token_type'    => 'bearer',
72
            'expires_in'    => 7200,
73
            'refresh_token' => 'fake-refresh-token',
74
            'scope'         => 'public',
75
            'created_at'    => 1583243989,
76
            'created_at'    => Carbon::now()->unix(),
77
        ];
78
    }
79
80
    public static function allContacts(int $count = 3): void
81
    {
82
        self::fakeAuthentication();
83
84
        Http::fake([
85
            self::getResourceUrl('contacts') => Http::response(
86
                self::allContactsResponse(),
87
                Response::HTTP_OK,
88
                self::getJsonContentType()
89
            ),
90
        ]);
91
    }
92
93
    private static function allContactsResponse(int $count = 3): array
94
    {
95
        $faker = Factory::create('tr_TR');
96
97
        $data = [];
98
99
        foreach (range(1, $count) as $index) {
100
            $data['data'][$index - 1] = [
101
                'id'            => $index,
102
                'type'          => 'contacts',
103
                'attributes'    => [
104
                    'created_at'                   => $faker->iso8601,
105
                    'updated_at'                   => $faker->iso8601,
106
                    'contact_type'                 => $faker->randomElement(['person', 'company']),
107
                    'name'                         => $faker->name,
108
                    'email'                        => $faker->email,
109
                    'short_name'                   => null,
110
                    'balance'                      => $faker->randomFloat(4),
111
                    'trl_balance'                  => $faker->randomFloat(4),
112
                    'usd_balance'                  => $faker->randomFloat(4),
113
                    'eur_balance'                  => $faker->randomFloat(4),
114
                    'gbp_balance'                  => $faker->randomFloat(4),
115
                    'tax_number'                   => $faker->numerify('###########'),
116
                    'tax_office'                   => "{$faker->city} Vergi Dairesi",
117
                    'archived'                     => $faker->boolean,
118
                    'account_type'                 => $faker->randomElement(['customer', 'supplier']),
119
                    'city'                         => $faker->city,
120
                    'district'                     => $faker->city,
121
                    'address'                      => $faker->address,
122
                    'phone'                        => $faker->phoneNumber,
123
                    'fax'                          => $faker->phoneNumber,
124
                    'is_abroad'                    => $faker->boolean,
125
                    'term_days'                    => null,
126
                    'invoicing_preferences'        => [],
127
                    'sharings_count'               => 0,
128
                    'ibans'                        => [],
129
                    'exchange_rate_type'           => 'buying',
130
                    'iban'                         => null,
131
                    'sharing_preview_url'          => "https://uygulama.parasut.com/{$faker->numberBetween(1000, 9999)}/portal/preview/{$faker->numberBetween(1000, 9999)}",
132
                    'sharing_preview_path'         => "/{$faker->numberBetween(1000, 9999)}/portal/preview/{$faker->numberBetween(1000, 9999)}",
133
                    'payment_reminder_preview_url' => "https://uygulama.parasut.com/{$faker->numberBetween(1000, 9999)}/portal/preview/{$faker->numberBetween(1000, 9999)}/odeme-hatirlat",
134
                ],
135
                'relationships' => [
136
                    'category'          => ['meta' => []],
137
                    'price_list'        => ['meta' => []],
138
                    'contact_portal'    => ['meta' => []],
139
                    'contact_people'    => ['meta' => []],
140
                    'activities'        => ['meta' => []],
141
                    'e_invoice_inboxes' => ['meta' => []],
142
                    'sharings'          => ['meta' => []],
143
                ],
144
                'meta'          => [
145
                    'created_at' => $faker->iso8601,
146
                    'updated_at' => $faker->iso8601,
147
                ],
148
            ];
149
        }
150
151
        $data['links'] = self::generateLinks($faker, 'contacts');
152
        $data['meta'] = self::generateMeta($faker, 'contacts');
153
154
        return $data;
155
    }
156
157
    private static function createContactResponse(?Contact $contact): array
158
    {
159
        $faker = Factory::create('tr_TR');
160
161
        return [
162
            'data' => [
163
                'id'            => (string) $faker->numberBetween(10000, 99999),
164
                'type'          => 'contacts',
165
                'attributes'    => [
166
                    'created_at'                   => $contact->created_at ?? $faker->iso8601,
167
                    'updated_at'                   => $contact->updated_at ?? $faker->iso8601,
168
                    'contact_type'                 => $contact->contact_type ?? 'company',
0 ignored issues
show
Bug Best Practice introduced by
The property contact_type does not exist on TarfinLabs\Parasut\Models\Contact. Since you implemented __get, consider adding a @property annotation.
Loading history...
169
                    'name'                         => $contact->name ?? $faker->name,
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on TarfinLabs\Parasut\Models\Contact. Since you implemented __get, consider adding a @property annotation.
Loading history...
170
                    'email'                        => $contact->email ?? null,
0 ignored issues
show
Bug Best Practice introduced by
The property email does not exist on TarfinLabs\Parasut\Models\Contact. Since you implemented __get, consider adding a @property annotation.
Loading history...
171
                    'short_name'                   => $contact->short_name ?? null,
0 ignored issues
show
Bug Best Practice introduced by
The property short_name does not exist on TarfinLabs\Parasut\Models\Contact. Since you implemented __get, consider adding a @property annotation.
Loading history...
172
                    'balance'                      => $contact->balance ?? '0.0',
0 ignored issues
show
Bug Best Practice introduced by
The property balance does not exist on TarfinLabs\Parasut\Models\Contact. Since you implemented __get, consider adding a @property annotation.
Loading history...
173
                    'trl_balance'                  => $contact->trl_balance ?? '0.0',
0 ignored issues
show
Bug Best Practice introduced by
The property trl_balance does not exist on TarfinLabs\Parasut\Models\Contact. Since you implemented __get, consider adding a @property annotation.
Loading history...
174
                    'usd_balance'                  => $contact->usd_balance ?? '0.0',
0 ignored issues
show
Bug Best Practice introduced by
The property usd_balance does not exist on TarfinLabs\Parasut\Models\Contact. Since you implemented __get, consider adding a @property annotation.
Loading history...
175
                    'eur_balance'                  => $contact->eur_balance ?? '0.0',
0 ignored issues
show
Bug Best Practice introduced by
The property eur_balance does not exist on TarfinLabs\Parasut\Models\Contact. Since you implemented __get, consider adding a @property annotation.
Loading history...
176
                    'gbp_balance'                  => $contact->gbp_balance ?? '0.0',
0 ignored issues
show
Bug Best Practice introduced by
The property gbp_balance does not exist on TarfinLabs\Parasut\Models\Contact. Since you implemented __get, consider adding a @property annotation.
Loading history...
177
                    'tax_number'                   => $contact->tax_number ?? null,
0 ignored issues
show
Bug Best Practice introduced by
The property tax_number does not exist on TarfinLabs\Parasut\Models\Contact. Since you implemented __get, consider adding a @property annotation.
Loading history...
178
                    'tax_office'                   => $contact->tax_office ?? null,
0 ignored issues
show
Bug Best Practice introduced by
The property tax_office does not exist on TarfinLabs\Parasut\Models\Contact. Since you implemented __get, consider adding a @property annotation.
Loading history...
179
                    'archived'                     => $contact->archived ?? false,
0 ignored issues
show
Bug Best Practice introduced by
The property archived does not exist on TarfinLabs\Parasut\Models\Contact. Since you implemented __get, consider adding a @property annotation.
Loading history...
180
                    'account_type'                 => $contact->account_type ?? $faker->randomElement(['customer', 'supplier']),
0 ignored issues
show
Bug Best Practice introduced by
The property account_type does not exist on TarfinLabs\Parasut\Models\Contact. Since you implemented __get, consider adding a @property annotation.
Loading history...
181
                    'city'                         => $contact->city ?? null,
0 ignored issues
show
Bug Best Practice introduced by
The property city does not exist on TarfinLabs\Parasut\Models\Contact. Since you implemented __get, consider adding a @property annotation.
Loading history...
182
                    'district'                     => $contact->district ?? null,
0 ignored issues
show
Bug Best Practice introduced by
The property district does not exist on TarfinLabs\Parasut\Models\Contact. Since you implemented __get, consider adding a @property annotation.
Loading history...
183
                    'address'                      => $contact->address ?? null,
0 ignored issues
show
Bug Best Practice introduced by
The property address does not exist on TarfinLabs\Parasut\Models\Contact. Since you implemented __get, consider adding a @property annotation.
Loading history...
184
                    'phone'                        => $contact->phone ?? null,
0 ignored issues
show
Bug Best Practice introduced by
The property phone does not exist on TarfinLabs\Parasut\Models\Contact. Since you implemented __get, consider adding a @property annotation.
Loading history...
185
                    'fax'                          => $contact->fax ?? null,
0 ignored issues
show
Bug Best Practice introduced by
The property fax does not exist on TarfinLabs\Parasut\Models\Contact. Since you implemented __get, consider adding a @property annotation.
Loading history...
186
                    'is_abroad'                    => $contact->is_abroad ?? false,
0 ignored issues
show
Bug Best Practice introduced by
The property is_abroad does not exist on TarfinLabs\Parasut\Models\Contact. Since you implemented __get, consider adding a @property annotation.
Loading history...
187
                    'term_days'                    => $contact->term_days ?? null,
0 ignored issues
show
Bug Best Practice introduced by
The property term_days does not exist on TarfinLabs\Parasut\Models\Contact. Since you implemented __get, consider adding a @property annotation.
Loading history...
188
                    'invoicing_preferences'        => $contact->invoicing_preferences ?? [],
0 ignored issues
show
Bug Best Practice introduced by
The property invoicing_preferences does not exist on TarfinLabs\Parasut\Models\Contact. Since you implemented __get, consider adding a @property annotation.
Loading history...
189
                    'sharings_count'               => $contact->sharings_count ?? 0,
0 ignored issues
show
Bug Best Practice introduced by
The property sharings_count does not exist on TarfinLabs\Parasut\Models\Contact. Since you implemented __get, consider adding a @property annotation.
Loading history...
190
                    'ibans'                        => $contact->ibans ?? [],
0 ignored issues
show
Bug Best Practice introduced by
The property ibans does not exist on TarfinLabs\Parasut\Models\Contact. Since you implemented __get, consider adding a @property annotation.
Loading history...
191
                    'exchange_rate_type'           => $contact->exchange_rate_type ?? 'buying',
0 ignored issues
show
Bug Best Practice introduced by
The property exchange_rate_type does not exist on TarfinLabs\Parasut\Models\Contact. Since you implemented __get, consider adding a @property annotation.
Loading history...
192
                    'iban'                         => $contact->iban ?? null,
0 ignored issues
show
Bug Best Practice introduced by
The property iban does not exist on TarfinLabs\Parasut\Models\Contact. Since you implemented __get, consider adding a @property annotation.
Loading history...
193
                    'sharing_preview_url'          => 'https://uygulama.parasut.com/'.config('parasut.company_id').'/portal/preview/'.$faker->numberBetween(1000, 9999),
194
                    'sharing_preview_path'         => '/'.config('parasut.company_id').'/portal/preview/'.$faker->numberBetween(1000, 9999),
195
                    'payment_reminder_preview_url' => 'https://uygulama.parasut.com/'.config('parasut.company_id').'/portal/preview/'.$faker->numberBetween(1000, 9999).'/odeme-hatirlat',
196
                ],
197
                'relationships' => [
198
                    'category'          => ['meta' => []],
199
                    'price_list'        => ['meta' => []],
200
                    'contact_portal'    => ['meta' => []],
201
                    'contact_people'    => ['meta' => []],
202
                    'activities'        => ['meta' => []],
203
                    'e_invoice_inboxes' => ['meta' => []],
204
                    'sharings'          => ['meta' => []],
205
                ],
206
                'meta'          => [
207
                    'created_at' => $contact->created_at ?? $faker->iso8601,
208
                    'updated_at' => $contact->updated_at ?? $faker->iso8601,
209
                ],
210
            ],
211
        ];
212
    }
213
214
    public static function createContact(Contact $contact): void
215
    {
216
        self::fakeAuthentication();
217
218
        Http::fake([
219
            self::getResourceUrl('contacts') => Http::response(
220
                self::createContactResponse($contact),
221
                Response::HTTP_OK,
222
                self::getJsonContentType()
223
            ),
224
        ]);
225
    }
226
227
    public static function updateContact(Contact $contact): void
228
    {
229
        self::fakeAuthentication();
230
231
        Http::fake([
232
            self::getResourceUrl('contacts' . '/' . $contact->id) => Http::response(
233
                self::createContactResponse($contact),
234
                Response::HTTP_OK,
235
                self::getJsonContentType()
236
            ),
237
        ]);
238
    }
239
240
    public static function deleteContact(Contact $contact): void
241
    {
242
        self::fakeAuthentication();
243
244
        Http::fake([
245
            self::getResourceUrl('contacts' . '/' . $contact->id) => Http::response(
246
                [[]],
247
                Response::HTTP_NO_CONTENT,
248
                self::getJsonContentType()
249
            ),
250
        ]);
251
    }
252
253
    public static function findContact(): int
254
    {
255
        self::fakeAuthentication();
256
257
        $response = self::createContactResponse(null);
258
259
        Http::fake([
260
            self::getResourceUrl('contacts/'.$response['data']['id']) => Http::response(
261
                $response,
262
                Response::HTTP_OK,
263
                self::getJsonContentType()
264
            ),
265
        ]);
266
267
        return $response['data']['id'];
268
    }
269
}
270