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 \Types \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,34 @@ 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 ' ,
50+ // Get first available location
51+ $ locations = self ::$ client ->locations ->list ();
52+ if (!$ locations ->getLocations () || empty ($ locations ->getLocations ())) {
53+ throw new RuntimeException ('No locations available for testing ' );
54+ }
55+ $ locationId = $ locations ->getLocations ()[0 ]->getId ();
56+ if (!$ locationId ) {
57+ throw new RuntimeException ('Location ID not present ' );
58+ }
59+ self ::$ locationId = $ locationId ;
60+
61+ // Get first available team member at this location
62+ $ teamMembersResponse = self ::$ client ->teamMembers ->search (new SearchTeamMembersRequest ([
63+ 'query ' => new SearchTeamMembersQuery ([
64+ 'filter ' => new SearchTeamMembersFilter ([
65+ 'locationIds ' => [self ::$ locationId ],
66+ 'status ' => 'ACTIVE ' ,
67+ ]),
5368 ]),
5469 ]));
55- $ memberId = $ teamResponse ->getTeamMember ()?->getId();
56- if (!$ memberId ) {
57- throw new RuntimeException ('Failed to create team member. ' );
70+ $ teamMembers = $ teamMembersResponse ->getTeamMembers ();
71+ if (!$ teamMembers || empty ($ teamMembers )) {
72+ throw new RuntimeException ('No team members available at location ' . self ::$ locationId );
73+ }
74+ $ memberId = $ teamMembers [0 ]->getId ();
75+ if (!$ memberId ) {
76+ throw new RuntimeException ('Team member ID not present ' );
5877 }
5978 self ::$ memberId = $ memberId ;
6079
@@ -96,10 +115,12 @@ public static function tearDownAfterClass(): void
96115 try {
97116 self ::$ client ->labor ->shifts ->delete (new DeleteShiftsRequest (['id ' => self ::$ shiftId ]));
98117 } catch (Exception ) {
118+ // Test may have already deleted the shift
99119 }
100120 try {
101121 self ::$ client ->labor ->breakTypes ->delete (new DeleteBreakTypesRequest (['id ' => self ::$ breakId ]));
102122 } catch (Exception ) {
123+ // Test may have already deleted the break
103124 }
104125 }
105126
@@ -207,22 +228,38 @@ public function testUpdateShift(): void
207228 */
208229 public function testDeleteShift (): void
209230 {
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 ' ,
231+ // First search for existing shifts for this team member
232+ $ searchRequest = new SearchShiftsRequest ([
233+ 'query ' => new ShiftQuery ([
234+ ' filter ' => new ShiftFilter ([
235+ ' teamMemberIds ' => [ self :: $ memberId ] ,
236+ ]) ,
216237 ]),
217- ]));
238+ 'limit ' => 100 ,
239+ ]);
240+ $ existingShifts = self ::$ client ->labor ->shifts ->search ($ searchRequest );
241+
242+ // Delete any existing shifts
243+ if ($ existingShifts ->getShifts ()) {
244+ foreach ($ existingShifts ->getShifts () as $ shift ) {
245+ if ($ shift ->getId ()) {
246+ self ::$ client ->labor ->shifts ->delete (new DeleteShiftsRequest (['id ' => $ shift ->getId ()]));
247+ }
248+ }
249+ }
250+
251+ // Start the shift 10 seconds from now and end it 20 seconds from now
252+ $ startTime = new DateTime ('+10 seconds ' );
253+ $ endTime = new DateTime ('+20 seconds ' );
218254
219- // create shift
255+ // Create shift
220256 $ shiftResponse = self ::$ client ->labor ->shifts ->create (new CreateShiftRequest ([
221257 'idempotencyKey ' => uniqid (),
222258 'shift ' => new Shift ([
223- 'startAt ' => self ::formatDateString (new DateTime ()),
259+ 'startAt ' => self ::formatDateString ($ startTime ),
260+ 'endAt ' => self ::formatDateString ($ endTime ),
224261 'locationId ' => self ::$ locationId ,
225- 'teamMemberId ' => $ teamMemberResponse -> getTeamMember ()?->getId() ,
262+ 'teamMemberId ' => self :: $ memberId ,
226263 ]),
227264 ]));
228265 $ shift = $ shiftResponse ->getShift ();
@@ -233,6 +270,10 @@ public function testDeleteShift(): void
233270 if (!$ shiftId ) {
234271 throw new RuntimeException ('Shift ID is null. ' );
235272 }
273+
274+ // Add a small delay to ensure the shift is fully created
275+ sleep (1 );
276+
236277 $ response = self ::$ client ->labor ->shifts ->delete (new DeleteShiftsRequest (['id ' => $ shiftId ]));
237278 $ this ->assertNotNull ($ response );
238279 }
@@ -284,4 +325,4 @@ private static function formatDateString(DateTime $date): string
284325 {
285326 return $ date ->format (DateTimeInterface::ATOM );
286327 }
287- }
328+ }
0 commit comments