|
11 | 11 |
|
12 | 12 | use Flarum\Queue\AbstractJob; |
13 | 13 | use Flarum\Testing\unit\TestCase; |
| 14 | +use Illuminate\Container\Container; |
14 | 15 | use PHPUnit\Framework\Attributes\Test; |
15 | 16 |
|
16 | 17 | class AbstractJobTest extends TestCase |
17 | 18 | { |
18 | 19 | protected function tearDown(): void |
19 | 20 | { |
20 | | - AbstractJobTestStub::$onQueue = null; |
21 | | - AbstractJobTestSubclassStub::$onQueue = null; |
| 21 | + Container::setInstance(null); |
22 | 22 |
|
23 | 23 | parent::tearDown(); |
24 | 24 | } |
25 | 25 |
|
| 26 | + /** |
| 27 | + * @param array<class-string, string> $routes |
| 28 | + */ |
| 29 | + private function containerWithRoutes(array $routes = []): void |
| 30 | + { |
| 31 | + $container = new Container(); |
| 32 | + $container->instance('flarum.queue.routes', $routes); |
| 33 | + Container::setInstance($container); |
| 34 | + } |
| 35 | + |
26 | 36 | #[Test] |
27 | 37 | public function defaults_to_no_queue_routing(): void |
28 | 38 | { |
29 | | - $job = new AbstractJobTestStub(); |
| 39 | + $this->containerWithRoutes(); |
| 40 | + |
| 41 | + $this->assertNull((new AbstractJobTestStub())->queue); |
| 42 | + } |
| 43 | + |
| 44 | + #[Test] |
| 45 | + public function no_container_is_harmless(): void |
| 46 | + { |
| 47 | + Container::setInstance(null); |
| 48 | + |
| 49 | + $this->assertNull((new AbstractJobTestStub())->queue); |
| 50 | + } |
| 51 | + |
| 52 | + #[Test] |
| 53 | + public function routes_onto_the_queue_registered_for_its_class(): void |
| 54 | + { |
| 55 | + $this->containerWithRoutes([AbstractJobTestStub::class => 'priority']); |
30 | 56 |
|
31 | | - $this->assertNull($job->queue); |
| 57 | + $this->assertSame('priority', (new AbstractJobTestStub())->queue); |
32 | 58 | } |
33 | 59 |
|
34 | 60 | #[Test] |
35 | | - public function routes_onto_queue_named_by_static_property(): void |
| 61 | + public function an_unrouted_class_is_unaffected_by_other_routes(): void |
36 | 62 | { |
37 | | - AbstractJobTestStub::$onQueue = 'priority'; |
| 63 | + $this->containerWithRoutes([AbstractJobTestOtherStub::class => 'other']); |
| 64 | + |
| 65 | + $this->assertNull((new AbstractJobTestStub())->queue); |
| 66 | + } |
38 | 67 |
|
39 | | - $job = new AbstractJobTestStub(); |
| 68 | + /** |
| 69 | + * The regression test for the shared-static bug: routing several sibling |
| 70 | + * job classes must not collide. A previous implementation stored the queue |
| 71 | + * in a static on AbstractJob, which siblings that did not redeclare it |
| 72 | + * shared — so routing one overwrote the others and, across processes, the |
| 73 | + * same class could even land on different queues. A class-keyed registry |
| 74 | + * gives each class independent routing. |
| 75 | + */ |
| 76 | + #[Test] |
| 77 | + public function sibling_classes_route_independently_in_the_same_process(): void |
| 78 | + { |
| 79 | + $this->containerWithRoutes([ |
| 80 | + AbstractJobTestStub::class => 'realtime', |
| 81 | + AbstractJobTestOtherStub::class => 'gdpr', |
| 82 | + AbstractJobTestSubclassStub::class => 'exports', |
| 83 | + ]); |
40 | 84 |
|
41 | | - $this->assertSame('priority', $job->queue); |
| 85 | + $this->assertSame('realtime', (new AbstractJobTestStub())->queue); |
| 86 | + $this->assertSame('gdpr', (new AbstractJobTestOtherStub())->queue); |
| 87 | + $this->assertSame('exports', (new AbstractJobTestSubclassStub())->queue); |
42 | 88 | } |
43 | 89 |
|
44 | 90 | #[Test] |
45 | | - public function static_property_is_resolved_via_late_static_binding(): void |
| 91 | + public function resolution_is_by_exact_class_not_inherited_from_parent(): void |
46 | 92 | { |
47 | | - AbstractJobTestStub::$onQueue = 'parent-queue'; |
48 | | - AbstractJobTestSubclassStub::$onQueue = 'child-queue'; |
| 93 | + // Routing only the parent does not implicitly route the subclass. |
| 94 | + $this->containerWithRoutes([AbstractJobTestStub::class => 'parent-queue']); |
49 | 95 |
|
50 | 96 | $this->assertSame('parent-queue', (new AbstractJobTestStub())->queue); |
51 | | - $this->assertSame('child-queue', (new AbstractJobTestSubclassStub())->queue); |
| 97 | + $this->assertNull((new AbstractJobTestSubclassStub())->queue); |
52 | 98 | } |
53 | 99 | } |
54 | 100 |
|
55 | 101 | class AbstractJobTestStub extends AbstractJob |
56 | 102 | { |
57 | | - public static ?string $onQueue = null; |
| 103 | +} |
| 104 | + |
| 105 | +class AbstractJobTestOtherStub extends AbstractJob |
| 106 | +{ |
58 | 107 | } |
59 | 108 |
|
60 | 109 | class AbstractJobTestSubclassStub extends AbstractJobTestStub |
61 | 110 | { |
62 | | - public static ?string $onQueue = null; |
63 | 111 | } |
0 commit comments