|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Cycle\Transaction\Tests\Unit; |
| 6 | + |
| 7 | +use Cycle\Database\DatabaseProviderInterface; |
| 8 | +use Cycle\ORM\ORMInterface; |
| 9 | +use Cycle\Transaction\Bridge\Laravel\Providers\TransactionServiceProvider; |
| 10 | +use Cycle\Transaction\Internal\TransactionImpl; |
| 11 | +use Cycle\Transaction\Tests\Fixtures\SqliteEnvironment; |
| 12 | +use Cycle\Transaction\Tests\Fixtures\TestEnvironment; |
| 13 | +use Cycle\Transaction\Transaction; |
| 14 | +use Illuminate\Container\Container; |
| 15 | +use Testo\Assert; |
| 16 | +use Testo\Codecov\Covers; |
| 17 | +use Testo\Lifecycle\BeforeTest; |
| 18 | +use Testo\Test; |
| 19 | + |
| 20 | +/** |
| 21 | + * Unit tests for the Laravel {@see TransactionServiceProvider}. |
| 22 | + * |
| 23 | + * Rather than booting a full Laravel application, this checks the provider's contract directly: it |
| 24 | + * binds {@see Transaction} as a singleton resolving to {@see TransactionImpl}, given an |
| 25 | + * {@see ORMInterface} and a {@see DatabaseProviderInterface} are available in the container. |
| 26 | + */ |
| 27 | +#[Test] |
| 28 | +#[Covers(TransactionServiceProvider::class)] |
| 29 | +final class TransactionServiceProviderTest |
| 30 | +{ |
| 31 | + private TestEnvironment $env; |
| 32 | + |
| 33 | + #[BeforeTest] |
| 34 | + public function setUp(): void |
| 35 | + { |
| 36 | + $this->env = SqliteEnvironment::create(); |
| 37 | + } |
| 38 | + |
| 39 | + public function itResolvesTransactionAsASingleton(): void |
| 40 | + { |
| 41 | + $container = new Container(); |
| 42 | + $container->instance(ORMInterface::class, $this->env->orm); |
| 43 | + $container->instance(DatabaseProviderInterface::class, $this->env->dbal); |
| 44 | + |
| 45 | + // ServiceProvider's docblock types $app as the Application contract, but it only uses the |
| 46 | + // container's bind/singleton methods, which Illuminate\Container\Container provides. |
| 47 | + /** @psalm-suppress InvalidArgument */ |
| 48 | + (new TransactionServiceProvider($container))->register(); |
| 49 | + |
| 50 | + $transaction = $container->make(Transaction::class); |
| 51 | + |
| 52 | + Assert::instanceOf($transaction, TransactionImpl::class); |
| 53 | + // A singleton must hand back the very same instance on every resolution. |
| 54 | + Assert::same($container->make(Transaction::class), $transaction); |
| 55 | + } |
| 56 | +} |
0 commit comments