Skip to content

Commit f8d18a9

Browse files
authored
[12.x] Fix pending attributes in schedule group (#57156)
* use copy instead of reference * style fix
1 parent e20db50 commit f8d18a9

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/Illuminate/Console/Scheduling/Schedule.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,8 @@ public function group(Closure $events)
312312
throw new RuntimeException('Invoke an attribute method such as Schedule::daily() before defining a schedule group.');
313313
}
314314

315-
$this->groupStack[] = clone $this->attributes;
315+
$this->groupStack[] = $this->attributes;
316+
$this->attributes = null;
316317

317318
$events($this);
318319

@@ -327,17 +328,17 @@ public function group(Closure $events)
327328
*/
328329
protected function mergePendingAttributes(Event $event)
329330
{
330-
if (isset($this->attributes)) {
331-
$this->attributes->mergeAttributes($event);
332-
333-
$this->attributes = null;
334-
}
335-
336331
if (! empty($this->groupStack)) {
337332
$group = array_last($this->groupStack);
338333

339334
$group->mergeAttributes($event);
340335
}
336+
337+
if (isset($this->attributes)) {
338+
$this->attributes->mergeAttributes($event);
339+
340+
$this->attributes = null;
341+
}
341342
}
342343

343344
/**
@@ -476,7 +477,7 @@ public function __call($method, $parameters)
476477
}
477478

478479
if (method_exists(PendingEventAttributes::class, $method)) {
479-
$this->attributes ??= array_last($this->groupStack) ?: new PendingEventAttributes($this);
480+
$this->attributes ??= $this->groupStack ? clone array_last($this->groupStack) : new PendingEventAttributes($this);
480481

481482
return $this->attributes->$method(...$parameters);
482483
}

tests/Integration/Console/Scheduling/ScheduleGroupTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,19 @@ public static function scheduleTestCases()
180180
],
181181
];
182182
}
183+
184+
public function testGroupedPendingEventAttribute()
185+
{
186+
$schedule = new ScheduleClass;
187+
$schedule->weekdays()->group(function ($schedule) {
188+
$schedule->command('inspire')->at('00:00'); // this is event, not pending attribute
189+
$schedule->at('01:00')->command('inspire'); // this is pending attribute
190+
$schedule->command('inspire'); // this goes back to group pending attribute
191+
});
192+
193+
$events = $schedule->events();
194+
$this->assertSame('0 0 * * 1-5', $events[0]->expression);
195+
$this->assertSame('0 1 * * 1-5', $events[1]->expression);
196+
$this->assertSame('* * * * 1-5', $events[2]->expression);
197+
}
183198
}

0 commit comments

Comments
 (0)