Skip to content

Commit f6071a8

Browse files
committed
Refactor test
1 parent 2571b8d commit f6071a8

File tree

1 file changed

+65
-23
lines changed

1 file changed

+65
-23
lines changed

tests/Integration/LaborTest.php

Lines changed: 65 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@
2020
use Square\Labor\Shifts\Requests\UpdateShiftRequest;
2121
use Square\Labor\WorkweekConfigs\Requests\ListWorkweekConfigsRequest;
2222
use Square\SquareClient;
23-
use Square\Types\CreateTeamMemberRequest;
2423
use Square\Types\BreakType;
2524
use Square\Types\Money;
25+
use Square\TeamMembers\Requests\SearchTeamMembersRequest;
26+
use Square\Types\SearchTeamMembersQuery;
27+
use Square\Types\SearchTeamMembersFilter;
2628
use Square\Types\Shift;
29+
use Square\Types\ShiftQuery;
30+
use Square\Types\ShiftFilter;
2731
use Square\Types\ShiftWage;
2832
use Square\Types\TeamMember;
2933

@@ -42,19 +46,35 @@ class LaborTest extends TestCase
4246
public static function setUpBeforeClass(): void
4347
{
4448
self::$client = Helpers::createClient();
45-
self::$locationId = Helpers::createLocation(self::$client );
4649

47-
// Create team member for testing
48-
$teamResponse = self::$client->teamMembers->create(new CreateTeamMemberRequest([
49-
'idempotencyKey' => uniqid(),
50-
'teamMember' => new TeamMember([
51-
'givenName' => 'Sherlock',
52-
'familyName' => 'Holmes',
53-
]),
50+
// Get first available location
51+
$locations = self::$client->locations->list();
52+
$locationsList = $locations->getLocations();
53+
if ($locationsList === null || count($locationsList) === 0) {
54+
throw new RuntimeException('No locations available for testing');
55+
}
56+
$locationId = $locationsList[0]->getId();
57+
if ($locationId === null) {
58+
throw new RuntimeException('Location ID not present');
59+
}
60+
self::$locationId = $locationId;
61+
62+
// Get first available team member at this location
63+
$teamMembersResponse = self::$client->teamMembers->search(new SearchTeamMembersRequest([
64+
'query' => [
65+
'filter' => [
66+
'locationIds' => [self::$locationId],
67+
'status' => 'ACTIVE',
68+
],
69+
],
5470
]));
55-
$memberId = $teamResponse->getTeamMember()?->getId();
56-
if(!$memberId) {
57-
throw new RuntimeException('Failed to create team member.');
71+
$teamMembers = $teamMembersResponse->getTeamMembers();
72+
if ($teamMembers === null || count($teamMembers) === 0) {
73+
throw new RuntimeException('No team members available at location ' . self::$locationId);
74+
}
75+
$memberId = $teamMembers[0]->getId();
76+
if ($memberId === null) {
77+
throw new RuntimeException('Team member ID not present');
5878
}
5979
self::$memberId = $memberId;
6080

@@ -96,10 +116,12 @@ public static function tearDownAfterClass(): void
96116
try {
97117
self::$client->labor->shifts->delete(new DeleteShiftsRequest(['id' => self::$shiftId]));
98118
} catch (Exception) {
119+
// Test may have already deleted the shift
99120
}
100121
try {
101122
self::$client->labor->breakTypes->delete(new DeleteBreakTypesRequest(['id' => self::$breakId]));
102123
} catch (Exception) {
124+
// Test may have already deleted the break
103125
}
104126
}
105127

@@ -207,22 +229,38 @@ public function testUpdateShift(): void
207229
*/
208230
public function testDeleteShift(): void
209231
{
210-
// create team member
211-
$teamMemberResponse = self::$client->teamMembers->create(new CreateTeamMemberRequest([
212-
'idempotencyKey' => uniqid(),
213-
'teamMember' => new TeamMember([
214-
'givenName' => 'Sherlock',
215-
'familyName' => 'Holmes',
232+
// First search for existing shifts for this team member
233+
$searchRequest = new SearchShiftsRequest([
234+
'query' => new ShiftQuery([
235+
'filter' => new ShiftFilter([
236+
'teamMemberIds' => [self::$memberId],
237+
]),
216238
]),
217-
]));
239+
'limit' => 100,
240+
]);
241+
$existingShifts = self::$client->labor->shifts->search($searchRequest);
242+
243+
// Delete any existing shifts
244+
if ($existingShifts->getShifts()) {
245+
foreach ($existingShifts->getShifts() as $shift) {
246+
if ($shift->getId()) {
247+
self::$client->labor->shifts->delete(new DeleteShiftsRequest(['id' => $shift->getId()]));
248+
}
249+
}
250+
}
251+
252+
// Start the shift 10 seconds from now and end it 20 seconds from now
253+
$startTime = new DateTime('+10 seconds');
254+
$endTime = new DateTime('+20 seconds');
218255

219-
// create shift
256+
// Create shift
220257
$shiftResponse = self::$client->labor->shifts->create(new CreateShiftRequest([
221258
'idempotencyKey' => uniqid(),
222259
'shift' => new Shift([
223-
'startAt' => self::formatDateString(new DateTime()),
260+
'startAt' => self::formatDateString($startTime),
261+
'endAt' => self::formatDateString($endTime),
224262
'locationId' => self::$locationId,
225-
'teamMemberId' => $teamMemberResponse->getTeamMember()?->getId(),
263+
'teamMemberId' => self::$memberId,
226264
]),
227265
]));
228266
$shift = $shiftResponse->getShift();
@@ -233,6 +271,10 @@ public function testDeleteShift(): void
233271
if(!$shiftId) {
234272
throw new RuntimeException('Shift ID is null.');
235273
}
274+
275+
// Add a small delay to ensure the shift is fully created
276+
sleep(1);
277+
236278
$response = self::$client->labor->shifts->delete(new DeleteShiftsRequest(['id' => $shiftId]));
237279
$this->assertNotNull($response);
238280
}
@@ -284,4 +326,4 @@ private static function formatDateString(DateTime $date): string
284326
{
285327
return $date->format(DateTimeInterface::ATOM);
286328
}
287-
}
329+
}

0 commit comments

Comments
 (0)