|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature\Assets\Api; |
| 4 | + |
| 5 | +use App\Models\Asset; |
| 6 | +use App\Models\AssetModel; |
| 7 | +use App\Models\Statuslabel; |
| 8 | +use App\Models\User; |
| 9 | +use PHPUnit\Framework\Attributes\Test; |
| 10 | +use Tests\TestCase; |
| 11 | + |
| 12 | +/** |
| 13 | + * You could argue that this should go somewhere else - that'd be fair. |
| 14 | + * But, as of now, the only way to properly ensure that the counters are set properly |
| 15 | + * is to directly hit the app. So that's what this does - via API. |
| 16 | + */ |
| 17 | +class CheckinCheckoutCounters extends TestCase |
| 18 | +{ |
| 19 | + #[Test] |
| 20 | + function counters() |
| 21 | + { |
| 22 | + //make an admin who can check in and out stuff |
| 23 | + $admin = User::factory()->superuser()->create(); |
| 24 | + |
| 25 | + //make a user |
| 26 | + $user = User::factory()->create(); |
| 27 | + |
| 28 | + //need a model for the asset |
| 29 | + $model = AssetModel::factory()->create(); |
| 30 | + |
| 31 | + //need a status for the asset, too |
| 32 | + $status = Statuslabel::factory()->readyToDeploy()->create(); |
| 33 | + |
| 34 | + |
| 35 | + //make an asset using the API (this is for the API after all!) |
| 36 | + $response = $this->actingAsForApi($admin) |
| 37 | + ->postJson(route('api.assets.store'), [ |
| 38 | + 'asset_tag' => 'random_string', |
| 39 | + 'model_id' => $model->id, |
| 40 | + 'status_id' => $status->id, |
| 41 | + ])->assertOk() |
| 42 | + ->assertStatusMessageIs('success') |
| 43 | + ->json(); |
| 44 | + \Log::error(print_r($response, true)); |
| 45 | + |
| 46 | + //check the counters |
| 47 | + $asset = Asset::find($response['payload']['id']); |
| 48 | + $this->assertEquals(0, $asset->checkin_counter); |
| 49 | + $this->assertEquals(0, $asset->checkout_counter); |
| 50 | + |
| 51 | + //do a checkout |
| 52 | + $this->actingAsForApi($admin) |
| 53 | + ->postJson(route('api.asset.checkout', $asset), [ |
| 54 | + 'checkout_to_type' => 'user', |
| 55 | + 'assigned_user' => $user->id, |
| 56 | + 'checkout_at' => '2024-04-01', |
| 57 | + 'expected_checkin' => '2024-04-08', |
| 58 | + 'name' => 'Changed Name', |
| 59 | + 'note' => 'Here is a cool note!', |
| 60 | + ]) |
| 61 | + ->assertOk(); |
| 62 | + |
| 63 | + $asset->refresh(); |
| 64 | + //check the counters. both. |
| 65 | + $this->assertEquals(0, $asset->checkin_counter); |
| 66 | + $this->assertEquals(1, $asset->checkout_counter); //why does _this_ fail?! |
| 67 | + |
| 68 | + //do a checkin |
| 69 | + $this->actingAsForApi(User::factory()->checkinAssets()->create()) |
| 70 | + ->postJson(route('api.asset.checkin', $asset), [ |
| 71 | + 'name' => 'Changed Name', |
| 72 | + 'status_id' => $status->id, |
| 73 | + ]) |
| 74 | + ->assertOk(); |
| 75 | + |
| 76 | + //check the counters, again. |
| 77 | + $asset->refresh(); |
| 78 | + $this->assertEquals(1, $asset->checkin_counter); //wait, _this_ fails too?! WTH? |
| 79 | + $this->assertEquals(1, $asset->checkout_counter); //okay, _nothing_ works. Now I'm confused. |
| 80 | + } |
| 81 | +} |
0 commit comments