Skip to content

Commit 286f787

Browse files
authored
Merge pull request #16980 from uberbrady/add_new_checkin_checkout_counters_tests_rebased
New tests for checkin/checkout counters
2 parents 9788397 + 4b95790 commit 286f787

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Tests\Feature\Assets\Ui;
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+
class CheckinCheckoutCounters extends TestCase
13+
{
14+
#[Test]
15+
function counters()
16+
{
17+
$admin = User::factory()->admin()->create();
18+
$user = User::factory()->create();
19+
20+
// create an asset using the GUI
21+
$this->actingAs($admin)
22+
->post(route('hardware.store'), [
23+
'asset_tags' => ['1' => '1234'],
24+
'model_id' => AssetModel::factory()->create()->id,
25+
'status_id' => Statuslabel::factory()->readyToDeploy()->create()->id,
26+
])->assertRedirect();
27+
28+
$asset = Asset::where('asset_tag', '1234')->sole();
29+
30+
//ensure counters are initialized properly
31+
$this->assertEquals(0,$asset->checkout_counter);
32+
$this->assertEquals(0,$asset->checkin_counter);
33+
34+
//perform a checkout
35+
$this->actingAs($admin)
36+
->post(route('hardware.checkout.store', $asset), [
37+
'checkout_to_type' => 'user',
38+
// overwrite the value from the default fields set above
39+
'assigned_user' => (string) $user->id,
40+
'name' => 'Changed Name',
41+
'checkout_at' => '2024-03-18',
42+
'expected_checkin' => '2024-03-28',
43+
'note' => 'An awesome note',
44+
])->assertRedirect()->assertSessionHasNoErrors();
45+
46+
$asset->refresh();
47+
// dump($asset);
48+
$this->assertEquals(1,$asset->checkout_counter);
49+
$this->assertEquals(0,$asset->checkin_counter);
50+
51+
//perform a check-in
52+
$this->actingAs($admin)
53+
->post(
54+
route('hardware.checkin.store', [$asset]),
55+
[
56+
'name' => 'Changed Name Again',
57+
],
58+
)->assertRedirect()->assertSessionHasNoErrors();
59+
60+
$asset->refresh();
61+
// dump($asset);
62+
$this->assertEquals(1,$asset->checkout_counter);
63+
$this->assertEquals(1,$asset->checkin_counter);
64+
}
65+
}

0 commit comments

Comments
 (0)