2020use Square \Labor \Shifts \Requests \UpdateShiftRequest ;
2121use Square \Labor \WorkweekConfigs \Requests \ListWorkweekConfigsRequest ;
2222use Square \SquareClient ;
23- use Square \Types \CreateTeamMemberRequest ;
2423use Square \Types \BreakType ;
2524use Square \Types \Money ;
25+ use Square \TeamMembers \Requests \SearchTeamMembersRequest ;
26+ use Square \Types \SearchTeamMembersQuery ;
27+ use Square \Types \SearchTeamMembersFilter ;
2628use Square \Types \Shift ;
29+ use Square \Types \ShiftQuery ;
30+ use Square \Types \ShiftFilter ;
2731use Square \Types \ShiftWage ;
2832use 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+ $ filter = new SearchTeamMembersFilter ([
64+ 'locationIds ' => [self ::$ locationId ],
65+ 'status ' => 'ACTIVE ' ,
66+ ]);
67+ $ query = new SearchTeamMembersQuery (['filter ' => $ filter ]);
68+ $ teamMembersResponse = self ::$ client ->teamMembers ->search (new SearchTeamMembersRequest ([
69+ 'query ' => $ query ,
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