20
20
use Square \Labor \Shifts \Requests \UpdateShiftRequest ;
21
21
use Square \Labor \WorkweekConfigs \Requests \ListWorkweekConfigsRequest ;
22
22
use Square \SquareClient ;
23
- use Square \Types \CreateTeamMemberRequest ;
24
23
use Square \Types \BreakType ;
25
24
use Square \Types \Money ;
25
+ use Square \TeamMembers \Requests \SearchTeamMembersRequest ;
26
+ use Square \Types \SearchTeamMembersQuery ;
27
+ use Square \Types \SearchTeamMembersFilter ;
26
28
use Square \Types \Shift ;
29
+ use Square \Types \ShiftQuery ;
30
+ use Square \Types \ShiftFilter ;
27
31
use Square \Types \ShiftWage ;
28
32
use Square \Types \TeamMember ;
29
33
@@ -42,19 +46,35 @@ class LaborTest extends TestCase
42
46
public static function setUpBeforeClass (): void
43
47
{
44
48
self ::$ client = Helpers::createClient ();
45
- self ::$ locationId = Helpers::createLocation (self ::$ client );
46
49
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
+ ],
54
70
]));
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 ' );
58
78
}
59
79
self ::$ memberId = $ memberId ;
60
80
@@ -96,10 +116,12 @@ public static function tearDownAfterClass(): void
96
116
try {
97
117
self ::$ client ->labor ->shifts ->delete (new DeleteShiftsRequest (['id ' => self ::$ shiftId ]));
98
118
} catch (Exception ) {
119
+ // Test may have already deleted the shift
99
120
}
100
121
try {
101
122
self ::$ client ->labor ->breakTypes ->delete (new DeleteBreakTypesRequest (['id ' => self ::$ breakId ]));
102
123
} catch (Exception ) {
124
+ // Test may have already deleted the break
103
125
}
104
126
}
105
127
@@ -207,22 +229,38 @@ public function testUpdateShift(): void
207
229
*/
208
230
public function testDeleteShift (): void
209
231
{
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
+ ]) ,
216
238
]),
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 ' );
218
255
219
- // create shift
256
+ // Create shift
220
257
$ shiftResponse = self ::$ client ->labor ->shifts ->create (new CreateShiftRequest ([
221
258
'idempotencyKey ' => uniqid (),
222
259
'shift ' => new Shift ([
223
- 'startAt ' => self ::formatDateString (new DateTime ()),
260
+ 'startAt ' => self ::formatDateString ($ startTime ),
261
+ 'endAt ' => self ::formatDateString ($ endTime ),
224
262
'locationId ' => self ::$ locationId ,
225
- 'teamMemberId ' => $ teamMemberResponse -> getTeamMember ()?->getId() ,
263
+ 'teamMemberId ' => self :: $ memberId ,
226
264
]),
227
265
]));
228
266
$ shift = $ shiftResponse ->getShift ();
@@ -233,6 +271,10 @@ public function testDeleteShift(): void
233
271
if (!$ shiftId ) {
234
272
throw new RuntimeException ('Shift ID is null. ' );
235
273
}
274
+
275
+ // Add a small delay to ensure the shift is fully created
276
+ sleep (1 );
277
+
236
278
$ response = self ::$ client ->labor ->shifts ->delete (new DeleteShiftsRequest (['id ' => $ shiftId ]));
237
279
$ this ->assertNotNull ($ response );
238
280
}
@@ -284,4 +326,4 @@ private static function formatDateString(DateTime $date): string
284
326
{
285
327
return $ date ->format (DateTimeInterface::ATOM );
286
328
}
287
- }
329
+ }
0 commit comments