|
| 1 | +<?php namespace Tests\Unit\Services; |
| 2 | +/** |
| 3 | + * Copyright 2026 OpenStack Foundation |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + **/ |
| 14 | + |
| 15 | +use App\Models\Foundation\Summit\Repositories\ISummitOrderRepository; |
| 16 | +use App\Services\Model\AbstractTask; |
| 17 | +use App\Services\Model\ReserveOrderTask; |
| 18 | +use App\Services\Model\Saga; |
| 19 | +use libs\utils\ITransactionService; |
| 20 | +use Mockery; |
| 21 | +use models\main\Member; |
| 22 | +use models\summit\Summit; |
| 23 | +use models\summit\SummitAttendee; |
| 24 | +use models\summit\SummitAttendeeTicket; |
| 25 | +use models\summit\SummitOrder; |
| 26 | +use PHPUnit\Framework\TestCase; |
| 27 | + |
| 28 | +/** |
| 29 | + * Class SagaCompensationTest |
| 30 | + * |
| 31 | + * Regression tests for the saga reorder introduced by the domain-authorized |
| 32 | + * promo code feature (ApplyPromoCodeTask moved after ReserveOrderTask). Two |
| 33 | + * concerns are exercised: |
| 34 | + * |
| 35 | + * 1. If any task downstream of ReserveOrderTask throws, Saga::abort() invokes |
| 36 | + * undo() on previously-run tasks in reverse order. ReserveOrderTask::undo() |
| 37 | + * must therefore remove the persisted order and its tickets. |
| 38 | + * 2. Tasks that have not yet run must not be undone. |
| 39 | + * |
| 40 | + * Uses Mockery on concrete classes; no Laravel, DB, or Redis required. The |
| 41 | + * actual dispatch of CreatedSummitRegistrationOrder after a successful |
| 42 | + * SummitOrderService::reserve() is covered by |
| 43 | + * OAuth2SummitPromoCodesApiTest/OAuth2SummitOrdersApiTest integration tests. |
| 44 | + * |
| 45 | + * @package Tests\Unit\Services |
| 46 | + */ |
| 47 | +class SagaCompensationTest extends TestCase |
| 48 | +{ |
| 49 | + protected function setUp(): void |
| 50 | + { |
| 51 | + parent::setUp(); |
| 52 | + // Minimal container so the Log/App facades the code under test touches |
| 53 | + // resolve to no-ops. No DB, no full Laravel bootstrap. |
| 54 | + $container = new \Illuminate\Container\Container(); |
| 55 | + $container->instance('app', $container); |
| 56 | + $container->instance('log', new class { |
| 57 | + public function __call($name, $args) { /* silently swallow log calls */ } |
| 58 | + }); |
| 59 | + \Illuminate\Support\Facades\Facade::setFacadeApplication($container); |
| 60 | + } |
| 61 | + |
| 62 | + protected function tearDown(): void |
| 63 | + { |
| 64 | + \Illuminate\Support\Facades\Facade::clearResolvedInstances(); |
| 65 | + \Illuminate\Support\Facades\Facade::setFacadeApplication(null); |
| 66 | + Mockery::close(); |
| 67 | + parent::tearDown(); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * ReserveOrderTask::undo() with no order in formerState is a safe no-op |
| 72 | + * (run() may have thrown before persisting the order). |
| 73 | + */ |
| 74 | + public function testUndoIsNoOpWhenOrderWasNotPersisted(): void |
| 75 | + { |
| 76 | + $tx_service = Mockery::mock(ITransactionService::class); |
| 77 | + // transaction() must NOT be called — nothing to compensate. |
| 78 | + $tx_service->shouldNotReceive('transaction'); |
| 79 | + |
| 80 | + $task = $this->buildTask( |
| 81 | + $tx_service, |
| 82 | + Mockery::mock(Summit::class), |
| 83 | + Mockery::mock(Member::class) |
| 84 | + ); |
| 85 | + |
| 86 | + // formerState deliberately missing the 'order' key |
| 87 | + $this->invokeUndo($task, []); |
| 88 | + |
| 89 | + // Assertion is implicit via Mockery expectations |
| 90 | + $this->addToAssertionCount(1); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * When ReserveOrderTask::run() persisted an order, undo() must: |
| 95 | + * - detach each ticket from its attendee owner (so stale references don't linger) |
| 96 | + * - remove the order from the summit |
| 97 | + * - delete the order via the repository (cascade removes tickets via orphanRemoval) |
| 98 | + */ |
| 99 | + public function testUndoDeletesOrderAndDetachesTicketsFromAttendees(): void |
| 100 | + { |
| 101 | + $attendee1 = Mockery::mock(SummitAttendee::class); |
| 102 | + $attendee2 = Mockery::mock(SummitAttendee::class); |
| 103 | + |
| 104 | + $ticket1 = Mockery::mock(SummitAttendeeTicket::class); |
| 105 | + $ticket1->shouldReceive('getOwner')->andReturn($attendee1); |
| 106 | + $ticket2 = Mockery::mock(SummitAttendeeTicket::class); |
| 107 | + $ticket2->shouldReceive('getOwner')->andReturn($attendee2); |
| 108 | + // Unassigned ticket: getOwner may return null, undo must not explode |
| 109 | + $ticket3 = Mockery::mock(SummitAttendeeTicket::class); |
| 110 | + $ticket3->shouldReceive('getOwner')->andReturn(null); |
| 111 | + |
| 112 | + $attendee1->shouldReceive('removeTicket')->once()->with($ticket1); |
| 113 | + $attendee2->shouldReceive('removeTicket')->once()->with($ticket2); |
| 114 | + |
| 115 | + $order = Mockery::mock(SummitOrder::class); |
| 116 | + $order->shouldReceive('getId')->andReturn(9001); |
| 117 | + $order->shouldReceive('getNumber')->andReturn('ORD-TEST-0001'); |
| 118 | + $order->shouldReceive('getTickets')->andReturn([$ticket1, $ticket2, $ticket3]); |
| 119 | + |
| 120 | + $summit = Mockery::mock(Summit::class); |
| 121 | + $summit->shouldReceive('removeOrder')->once()->with($order); |
| 122 | + |
| 123 | + $owner = Mockery::mock(Member::class); |
| 124 | + |
| 125 | + $order_repo = Mockery::mock(ISummitOrderRepository::class); |
| 126 | + $order_repo->shouldReceive('delete')->once()->with($order); |
| 127 | + |
| 128 | + // Bind the repo into the container so App::make() inside undo() resolves it. |
| 129 | + $container = \Illuminate\Support\Facades\Facade::getFacadeApplication(); |
| 130 | + $container->instance(ISummitOrderRepository::class, $order_repo); |
| 131 | + |
| 132 | + $tx_service = Mockery::mock(ITransactionService::class); |
| 133 | + $tx_service->shouldReceive('transaction')->once()->andReturnUsing(function ($fn) { |
| 134 | + return $fn(); |
| 135 | + }); |
| 136 | + |
| 137 | + $task = $this->buildTask($tx_service, $summit, $owner); |
| 138 | + $this->invokeUndo($task, ['order' => $order]); |
| 139 | + |
| 140 | + $this->addToAssertionCount(1); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Integration-ish: drive a real Saga whose last task throws and verify |
| 145 | + * that a preceding "ReserveOrder-like" task has its undo() invoked exactly |
| 146 | + * once, in reverse order. |
| 147 | + */ |
| 148 | + public function testSagaAbortCallsUndoInReverseOrder(): void |
| 149 | + { |
| 150 | + $order_of_calls = []; |
| 151 | + |
| 152 | + $first = new RecordingTask('first', $order_of_calls); |
| 153 | + $second = new RecordingTask('second', $order_of_calls); |
| 154 | + $failing = new class extends AbstractTask { |
| 155 | + public function run(array $formerState): array |
| 156 | + { |
| 157 | + throw new \RuntimeException('downstream failure'); |
| 158 | + } |
| 159 | + public function undo() { /* never runs — it threw in run() */ } |
| 160 | + }; |
| 161 | + |
| 162 | + $saga = Saga::start() |
| 163 | + ->addTask($first) |
| 164 | + ->addTask($second) |
| 165 | + ->addTask($failing); |
| 166 | + |
| 167 | + try { |
| 168 | + $saga->run(); |
| 169 | + $this->fail('Expected saga to propagate the downstream exception'); |
| 170 | + } catch (\RuntimeException $ex) { |
| 171 | + $this->assertSame('downstream failure', $ex->getMessage()); |
| 172 | + } |
| 173 | + |
| 174 | + // run: first, second, (failing throws); undo: second, first |
| 175 | + $this->assertSame( |
| 176 | + ['run:first', 'run:second', 'undo:second', 'undo:first'], |
| 177 | + $order_of_calls |
| 178 | + ); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Construct a ReserveOrderTask with only the fields undo() needs. run() is |
| 183 | + * not exercised here, so most collaborators can be plain Mockery doubles. |
| 184 | + */ |
| 185 | + private function buildTask(ITransactionService $tx, Summit $summit, Member $owner): ReserveOrderTask |
| 186 | + { |
| 187 | + $reflector = new \ReflectionClass(ReserveOrderTask::class); |
| 188 | + /** @var ReserveOrderTask $task */ |
| 189 | + $task = $reflector->newInstanceWithoutConstructor(); |
| 190 | + |
| 191 | + $this->setPrivate($task, 'tx_service', $tx); |
| 192 | + $this->setPrivate($task, 'summit', $summit); |
| 193 | + $this->setPrivate($task, 'owner', $owner); |
| 194 | + |
| 195 | + return $task; |
| 196 | + } |
| 197 | + |
| 198 | + private function setPrivate(object $instance, string $property, $value): void |
| 199 | + { |
| 200 | + $r = new \ReflectionClass($instance); |
| 201 | + $p = $r->getProperty($property); |
| 202 | + $p->setAccessible(true); |
| 203 | + $p->setValue($instance, $value); |
| 204 | + } |
| 205 | + |
| 206 | + private function invokeUndo(ReserveOrderTask $task, array $formerState): void |
| 207 | + { |
| 208 | + $this->setPrivate($task, 'formerState', $formerState); |
| 209 | + $task->undo(); |
| 210 | + } |
| 211 | +} |
| 212 | + |
| 213 | +/** |
| 214 | + * Minimal AbstractTask implementation that records run/undo invocation order. |
| 215 | + * Declared at file scope (not inside the TestCase) so PHP can resolve the |
| 216 | + * AbstractTask parent at class-load time without coupling to test lifecycle. |
| 217 | + */ |
| 218 | +final class RecordingTask extends AbstractTask |
| 219 | +{ |
| 220 | + private $label; |
| 221 | + private $log; |
| 222 | + |
| 223 | + public function __construct(string $label, array &$log) |
| 224 | + { |
| 225 | + $this->label = $label; |
| 226 | + $this->log = &$log; |
| 227 | + } |
| 228 | + |
| 229 | + public function run(array $formerState): array |
| 230 | + { |
| 231 | + $this->log[] = 'run:' . $this->label; |
| 232 | + return $formerState; |
| 233 | + } |
| 234 | + |
| 235 | + public function undo() |
| 236 | + { |
| 237 | + $this->log[] = 'undo:' . $this->label; |
| 238 | + } |
| 239 | +} |
0 commit comments