Skip to content

Commit 96ad799

Browse files
committed
Add testing classes
1 parent 0993b36 commit 96ad799

File tree

2 files changed

+268
-0
lines changed

2 files changed

+268
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Longman\LaravelLodash\Testing;
6+
7+
use Bus as BusFacade;
8+
use Event as EventFacade;
9+
use Faker\Factory;
10+
use Faker\Generator;
11+
use Http as HttpFacade;
12+
use Illuminate\Database\Connection;
13+
use Illuminate\Filesystem\FilesystemAdapter;
14+
use Illuminate\Foundation\Testing\Concerns\InteractsWithAuthentication;
15+
use Illuminate\Http\Client\Factory as HttpFake;
16+
use Illuminate\Support\Arr;
17+
use Illuminate\Support\Facades\Storage;
18+
use Illuminate\Support\Testing\Fakes\BusFake;
19+
use Illuminate\Support\Testing\Fakes\EventFake;
20+
use Illuminate\Support\Testing\Fakes\MailFake;
21+
use Illuminate\Support\Testing\Fakes\NotificationFake;
22+
use Illuminate\Support\Testing\Fakes\QueueFake;
23+
use Mail as MailFacade;
24+
use Notification as NotificationFacade;
25+
use Queue as QueueFacade;
26+
27+
use function app;
28+
use function config;
29+
use function fake;
30+
31+
abstract readonly class FakeDataProvider
32+
{
33+
use InteractsWithAuthentication;
34+
35+
public static function getFakeBusInstance(): BusFake
36+
{
37+
return BusFacade::fake();
38+
}
39+
40+
public static function getFakeEventInstance(array $eventsToFake = []): EventFake
41+
{
42+
return EventFacade::fake($eventsToFake);
43+
}
44+
45+
public static function getFakeHttpInstance(): HttpFake
46+
{
47+
return HttpFacade::fake();
48+
}
49+
50+
public static function getFakeNotificationInstance(): NotificationFake
51+
{
52+
return NotificationFacade::fake();
53+
}
54+
55+
public static function getFakeMailInstance(): MailFake
56+
{
57+
return MailFacade::fake();
58+
}
59+
60+
public static function getFakeQueueInstance(): QueueFake
61+
{
62+
return QueueFacade::fake();
63+
}
64+
65+
public static function getFakeStorageInstance(?string $disk = null, array $config = []): FilesystemAdapter
66+
{
67+
if (! Arr::exists($config, 'url')) {
68+
Arr::set($config, 'url', config('app.url'));
69+
}
70+
71+
return Storage::fake($disk, $config);
72+
}
73+
74+
public static function getFaker($locale = Factory::DEFAULT_LOCALE): Generator
75+
{
76+
return fake($locale);
77+
}
78+
79+
public static function getDbConnection(?string $name = null): Connection
80+
{
81+
return app('db')->connection($name);
82+
}
83+
}

src/Lodash/Testing/Response.php

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Longman\LaravelLodash\Testing;
6+
7+
use Arr;
8+
use Illuminate\Testing\Assert;
9+
use Illuminate\Testing\TestResponse;
10+
use PHPUnit\Framework\Assert as PHPUnit;
11+
12+
use function __;
13+
use function json_decode;
14+
15+
class Response extends TestResponse
16+
{
17+
protected static array $successResponseStructure = [];
18+
protected static array $errorResponseStructure = [];
19+
protected static array $pagerMetaStructure = [
20+
'total',
21+
'count',
22+
'perPage',
23+
'currentPage',
24+
'totalPages',
25+
'links',
26+
];
27+
28+
public static function setSuccessResponseStructure(array $structure): void
29+
{
30+
self::$successResponseStructure = $structure;
31+
}
32+
33+
public static function setErrorResponseStructure(array $structure): void
34+
{
35+
self::$errorResponseStructure = $structure;
36+
}
37+
38+
public function assertJsonDataCount(int $count): self
39+
{
40+
$response = $this->getDecodedContent();
41+
42+
PHPUnit::assertCount($count, $response['data'] ?? []);
43+
44+
return $this;
45+
}
46+
47+
public function assertJsonDataPagination(array $data): self
48+
{
49+
$response = $this->getDecodedContent();
50+
51+
PHPUnit::assertEquals($data['page'], $response['meta']['pagination']['currentPage']);
52+
PHPUnit::assertEquals($data['perPage'], $response['meta']['pagination']['perPage']);
53+
PHPUnit::assertEquals($data['count'], $response['meta']['pagination']['count']);
54+
PHPUnit::assertEquals($data['total'], $response['meta']['pagination']['total']);
55+
56+
return $this;
57+
}
58+
59+
public function assertJsonDataCollectionStructure(array $data, bool $includePagerMeta = true): self
60+
{
61+
$struct = self::$successResponseStructure;
62+
$struct['data'] = [$data];
63+
64+
if ($includePagerMeta) {
65+
$struct['meta'] = [
66+
'pagination' => self::$pagerMetaStructure,
67+
];
68+
}
69+
70+
$this->assertJsonStructure($struct);
71+
72+
return $this;
73+
}
74+
75+
public function assertJsonDataItemStructure(array $data): self
76+
{
77+
$struct = ['data' => $data];
78+
79+
$this->assertJsonStructure($struct);
80+
81+
return $this;
82+
}
83+
84+
public function assertJsonErrorStructure(): self
85+
{
86+
$this->assertJsonStructure(self::$errorResponseStructure);
87+
88+
return $this;
89+
}
90+
91+
public function assertJsonSuccessStructure(string $message = 'ok'): self
92+
{
93+
$this->assertJsonStructure(self::$successResponseStructure);
94+
$this->assertJson(['status' => $message]);
95+
96+
return $this;
97+
}
98+
99+
public function getDecodedContent(): array
100+
{
101+
$content = $this->getContent();
102+
103+
return json_decode($content, true);
104+
}
105+
106+
public function assertForbidden(): Response
107+
{
108+
parent::assertForbidden();
109+
110+
//$this->assertJsonErrorStructure();
111+
//$this->assertJson(['message' => 'This action is unauthorized.']);
112+
113+
return $this;
114+
}
115+
116+
public function assertNotFound(): Response
117+
{
118+
parent::assertNotFound();
119+
120+
//$this->assertJsonErrorStructure();
121+
$this->assertJson(['status' => 'error', 'message' => __('app.item_not_found')]);
122+
123+
return $this;
124+
}
125+
126+
public function assertIsInvalidItem(): Response
127+
{
128+
Assert::assertTrue(
129+
$this->isInvalidData(),
130+
'Response status code [' . $this->getStatusCode() . '] is not a invalid data status code.',
131+
);
132+
133+
return $this;
134+
}
135+
136+
public function assertInvalidData(): Response
137+
{
138+
Assert::assertTrue(
139+
$this->isInvalidData(),
140+
'Response status code [' . $this->getStatusCode() . '] is not a invalid data status code.',
141+
);
142+
$this->assertJsonErrorStructure();
143+
144+
return $this;
145+
}
146+
147+
public function isInvalidData(): bool
148+
{
149+
return $this->getStatusCode() === 422;
150+
}
151+
152+
public function assertIsError(): void
153+
{
154+
$this->assertJsonStructure(self::$errorResponseStructure);
155+
$this->assertJson(['status' => 'error']);
156+
}
157+
158+
public function assertIsOk(string $message = 'ok', bool $includeMeta = false): void
159+
{
160+
$structure = self::$successResponseStructure;
161+
if (! $includeMeta) {
162+
$structure = Arr::except($structure, 'meta');
163+
}
164+
$this->assertJsonStructure($structure);
165+
$this->assertJson(['status' => $message]);
166+
}
167+
168+
public function assertOk(): Response
169+
{
170+
parent::assertOk();
171+
172+
$this->assertJsonSuccessStructure();
173+
174+
return $this;
175+
}
176+
177+
public function assertCreated(): Response
178+
{
179+
parent::assertCreated();
180+
181+
$this->assertJsonSuccessStructure();
182+
183+
return $this;
184+
}
185+
}

0 commit comments

Comments
 (0)