Skip to content

Commit

Permalink
🧑‍💻 seed status tags (#2128)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKrisKrisu authored Nov 13, 2023
1 parent f9fcbb7 commit e093023
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
11 changes: 10 additions & 1 deletion database/factories/StatusTagFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Database\Factories;

use App\Enum\StatusTagKey;
use App\Models\Status;
use Illuminate\Database\Eloquent\Factories\Factory;

Expand All @@ -11,7 +12,15 @@ class StatusTagFactory extends Factory
public function definition(): array {
return [
'status_id' => Status::factory(),
'key' => $this->faker->unique()->word,
'key' => $this->faker->randomElement([
StatusTagKey::ROLE->value,
StatusTagKey::SEAT->value,
StatusTagKey::TICKET->value,
StatusTagKey::TRAVEL_CLASS->value,
StatusTagKey::WAGON->value,
StatusTagKey::WAGON_CLASS->value,
StatusTagKey::LOCOMOTIVE_CLASS->value,
]),
'value' => $this->faker->word,
];
}
Expand Down
6 changes: 5 additions & 1 deletion database/seeders/TrainCheckinSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Database\Seeders;

use App\Enum\StatusTagKey;
use App\Http\Controllers\Backend\Transport\TrainCheckinController;
use App\Models\Event;
use App\Models\HafasTrip;
use App\Models\StatusTag;
use App\Models\User;
use Exception;
use Illuminate\Database\Seeder;
Expand All @@ -16,7 +18,7 @@ public function run(): void {
foreach (User::all() as $user) {
$hafasTrip = HafasTrip::all()->random();
try {
TrainCheckinController::checkin(
$trainCheckinResponse = TrainCheckinController::checkin(
user: $user,
hafasTrip: $hafasTrip,
origin: $hafasTrip->originStation, //Checkin from the first station...
Expand All @@ -25,6 +27,8 @@ public function run(): void {
arrival: $hafasTrip->arrival,
event: random_int(0, 1) ? Event::all()->random() : null,
);
$status = $trainCheckinResponse['status'];
StatusTag::factory(['status_id' => $status->id])->create();
} catch (Exception) {
continue;
}
Expand Down
40 changes: 20 additions & 20 deletions tests/Feature/APIv1/StatusTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ class StatusTagTest extends ApiTestCase
use RefreshDatabase;

public function testViewNonExistingTagsOnOwnStatus(): void {
$user = User::factory()->create();
$user = User::factory()->create();
Passport::actingAs($user, ['*']);
$status = Status::factory(['user_id' => $user->id])->create();
$status = Status::factory(['user_id' => $user->id])->create();

$response = $this->get('/api/v1/status/' . $status->id . '/tags');
$response->assertJsonStructure(['data' => []]);
$response->assertJsonCount(0, 'data');
}

public function testViewTagsOnOwnStatusWithDifferentVisibilitiesAndDeleteOne(): void {
$user = User::factory()->create();
$user = User::factory()->create();
Passport::actingAs($user, ['*']);
$status = Status::factory(['user_id' => $user->id])->create();
$tagToDelete = StatusTag::factory(['status_id' => $status->id, 'visibility' => StatusVisibility::PUBLIC->value])->create();
StatusTag::factory(['status_id' => $status->id, 'visibility' => StatusVisibility::FOLLOWERS->value])->create();
StatusTag::factory(['status_id' => $status->id, 'visibility' => StatusVisibility::PRIVATE->value])->create();
StatusTag::factory(['status_id' => $status->id, 'visibility' => StatusVisibility::AUTHENTICATED->value])->create();
$tagToDelete = StatusTag::factory(['status_id' => $status->id, 'key' => 'first', 'visibility' => StatusVisibility::PUBLIC->value])->create();
StatusTag::factory(['status_id' => $status->id, 'key' => 'second', 'visibility' => StatusVisibility::FOLLOWERS->value])->create();
StatusTag::factory(['status_id' => $status->id, 'key' => 'third', 'visibility' => StatusVisibility::PRIVATE->value])->create();
StatusTag::factory(['status_id' => $status->id, 'key' => 'fourth', 'visibility' => StatusVisibility::AUTHENTICATED->value])->create();

$response = $this->get('/api/v1/status/' . $status->id . '/tags');
$response->assertJsonStructure([
Expand All @@ -58,18 +58,18 @@ public function testViewTagsOnOwnStatusWithDifferentVisibilitiesAndDeleteOne():
}

public function testCreateAndUpdateTag(): void {
$user = User::factory()->create();
$user = User::factory()->create();
Passport::actingAs($user, ['*']);
$status = Status::factory(['user_id' => $user->id])->create();
$status = Status::factory(['user_id' => $user->id])->create();

//Create StatusTag
$response = $this->post(
uri: '/api/v1/status/' . $status->id . '/tags',
data: [
'key' => 'test',
'value' => 'test',
'visibility' => StatusVisibility::PUBLIC->value,
],
uri: '/api/v1/status/' . $status->id . '/tags',
data: [
'key' => 'test',
'value' => 'test',
'visibility' => StatusVisibility::PUBLIC->value,
],
);
$response->assertOk();
$response->assertJson([
Expand All @@ -84,11 +84,11 @@ public function testCreateAndUpdateTag(): void {

//Update StatusTag and change key and value
$response = $this->put(
uri: '/api/v1/status/' . $status->id . '/tags/test',
data: [
'key' => 'test2',
'value' => 'test2',
],
uri: '/api/v1/status/' . $status->id . '/tags/test',
data: [
'key' => 'test2',
'value' => 'test2',
],
);
$response->assertOk();
$response->assertJson([
Expand Down

0 comments on commit e093023

Please sign in to comment.