diff --git a/infection.json b/infection.json index 287af6a..230574a 100644 --- a/infection.json +++ b/infection.json @@ -9,8 +9,8 @@ ] }, "timeout": 10, - "minMsi": 90, - "minCoveredMsi": 90, + "minMsi": 98, + "minCoveredMsi": 98, "testFramework": "testo", "tmpDir": "runtime", "logs": { diff --git a/tests/Acceptance/Common/ActiveRecordTest.php b/tests/Acceptance/Common/ActiveRecordTest.php index ed411cf..58278bc 100644 --- a/tests/Acceptance/Common/ActiveRecordTest.php +++ b/tests/Acceptance/Common/ActiveRecordTest.php @@ -232,19 +232,19 @@ public function groupActionsInStrictModeOutsideTransactionThrows(): never #[Isolate(Isolation::None)] public function groupActionsInIgnoreModeOutsideTransactionSucceeds(): void { - // When outside a transaction, TransactionMode::Ignore (with strict: false) should work fine. - // The runner will ignore the lack of a transaction. - // If mutated to strict: true, this would throw RunnerException because there's no active transaction. - $result = ActiveRecord::groupActions(static function () use (&$created, &$deleted): string { - // Create and immediately delete an entity to generate commands without leaving side effects + # Two separate groups: saving and deleting in one group cancel out and run no command at all. + $user = ActiveRecord::groupActions(static function (): User { $user = new User('TestUser'); - $created = $user->save(); // Must succeed without transaction - $deleted = $user->delete(); // Must also succeed - return 'success'; + $user->save(); + return $user; }, TransactionMode::Ignore); - Assert::same($result, 'success'); - Assert::true($created); - Assert::true($deleted); + try { + Assert::notNull(User::query()->where('name', 'TestUser')->fetchOne()); + } finally { + ActiveRecord::groupActions(static fn(): bool => $user->delete(), TransactionMode::Ignore); + } + + Assert::null(User::query()->where('name', 'TestUser')->fetchOne()); } public function deletesMultipleEntitiesInGroupActions(): void diff --git a/tests/Unit/FacadeTest.php b/tests/Unit/FacadeTest.php index 3eb9043..4ada636 100644 --- a/tests/Unit/FacadeTest.php +++ b/tests/Unit/FacadeTest.php @@ -12,6 +12,8 @@ use Cycle\Testing\ORM\Memory; use Cycle\Tests\Unit\Stub\Container\ConfigurableContainer; use Cycle\Tests\Unit\Stub\Container\ServiceNotFoundException; +use Cycle\Transaction\Internal\TransactionImpl; +use Cycle\Transaction\Transaction; use Testo\Assert; use Testo\Codecov\Covers; use Testo\Expect; @@ -130,6 +132,20 @@ public function cachesDatabaseManagerAfterFirstCall(): void Assert::count($container->requested, 1); } + public function cachesTransactionAfterFirstCall(): void + { + $transaction = new TransactionImpl($this->buildOrm(), DatabaseMemory::manager()); + $container = ConfigurableContainer::returning(Transaction::class, $transaction); + Facade::setContainer($container); + + $first = Facade::getTransaction(); + $second = Facade::getTransaction(); + + Assert::same($first, $transaction); + Assert::same($second, $transaction); + Assert::same($container->requested, [Transaction::class]); + } + private function buildOrm(): ORMInterface { return Memory::orm();