|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * PHP Service Bus (publish-subscribe pattern implementation) |
| 5 | + * Supports Saga pattern and Event Sourcing |
| 6 | + * |
| 7 | + * @author Maksim Masiukevich <[email protected]> |
| 8 | + * @license MIT |
| 9 | + * @license https://opensource.org/licenses/MIT |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types = 1); |
| 13 | + |
| 14 | +namespace Desperado\ServiceBus\Tests\Application; |
| 15 | + |
| 16 | +use Desperado\ServiceBus\Application\Bootstrap; |
| 17 | +use function Desperado\ServiceBus\Common\removeDirectory; |
| 18 | +use Desperado\ServiceBus\DependencyInjection\Compiler\ServicesCompilerPass; |
| 19 | +use Desperado\ServiceBus\DependencyInjection\Extensions\ServiceBusExtension; |
| 20 | +use Desperado\ServiceBus\Storage\SQL\DoctrineDBAL\DoctrineDBALAdapter; |
| 21 | +use PHPUnit\Framework\TestCase; |
| 22 | + |
| 23 | +/** |
| 24 | + * |
| 25 | + */ |
| 26 | +final class BootstrapTest extends TestCase |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @var string |
| 30 | + */ |
| 31 | + private $cacheDirectory; |
| 32 | + |
| 33 | + /** |
| 34 | + * @inheritdoc |
| 35 | + */ |
| 36 | + protected function setUp(): void |
| 37 | + { |
| 38 | + parent::setUp(); |
| 39 | + |
| 40 | + $this->cacheDirectory = \sys_get_temp_dir() . '/bootstrap_test'; |
| 41 | + |
| 42 | + if(false === \file_exists($this->cacheDirectory)) |
| 43 | + { |
| 44 | + \mkdir($this->cacheDirectory); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @inheritdoc |
| 50 | + */ |
| 51 | + protected function tearDown(): void |
| 52 | + { |
| 53 | + parent::tearDown(); |
| 54 | + |
| 55 | + removeDirectory($this->cacheDirectory); |
| 56 | + |
| 57 | + unset($this->cacheDirectory); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @test |
| 62 | + * |
| 63 | + * @return void |
| 64 | + */ |
| 65 | + public function withDotEnv(): void |
| 66 | + { |
| 67 | + $bootstrap = Bootstrap::withDotEnv(__DIR__ . '/valid_dot_env_file.env'); |
| 68 | + |
| 69 | + $bootstrap->useCustomCacheDirectory($this->cacheDirectory); |
| 70 | + $bootstrap->addExtensions(new ServiceBusExtension()); |
| 71 | + $bootstrap->addCompilerPasses(new ServicesCompilerPass()); |
| 72 | + $bootstrap->importParameters(['qwerty' => 'root']); |
| 73 | + |
| 74 | + $bootstrap->useSqlStorage(DoctrineDBALAdapter::class, \getenv('DATABASE_CONNECTION_DSN')); |
| 75 | + $bootstrap->useAmqpExtTransport(\getenv('TRANSPORT_CONNECTION_DSN')); |
| 76 | + |
| 77 | + /** @var \Symfony\Component\DependencyInjection\ContainerInterface $container */ |
| 78 | + $container = $bootstrap->boot(); |
| 79 | + |
| 80 | + static::assertTrue($container->hasParameter('qwerty')); |
| 81 | + static::assertEquals('root', $container->getParameter('qwerty')); |
| 82 | + |
| 83 | + static::assertEquals(\getenv('APP_ENVIRONMENT'), $container->getParameter('service_bus.environment')); |
| 84 | + static::assertEquals(\getenv('APP_ENTRY_POINT_NAME'), $container->getParameter('service_bus.entry_point')); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @test |
| 89 | + * |
| 90 | + * @return void |
| 91 | + */ |
| 92 | + public function withEnvironmentValues(): void |
| 93 | + { |
| 94 | + \putenv('APP_ENVIRONMENT=test'); |
| 95 | + \putenv('APP_ENTRY_POINT_NAME=phpunit'); |
| 96 | + |
| 97 | + $bootstrap = Bootstrap::withEnvironmentValues(); |
| 98 | + |
| 99 | + $bootstrap->useCustomCacheDirectory($this->cacheDirectory); |
| 100 | + $bootstrap->addExtensions(new ServiceBusExtension()); |
| 101 | + $bootstrap->addCompilerPasses(new ServicesCompilerPass()); |
| 102 | + $bootstrap->importParameters(['qwerty1' => 'root1']); |
| 103 | + |
| 104 | + $bootstrap->useSqlStorage(DoctrineDBALAdapter::class, \getenv('DATABASE_CONNECTION_DSN')); |
| 105 | + $bootstrap->useAmqpExtTransport(\getenv('TRANSPORT_CONNECTION_DSN')); |
| 106 | + |
| 107 | + /** @var \Symfony\Component\DependencyInjection\ContainerInterface $container */ |
| 108 | + $container = $bootstrap->boot(); |
| 109 | + |
| 110 | + static::assertTrue($container->hasParameter('qwerty1')); |
| 111 | + static::assertEquals('root1', $container->getParameter('qwerty1')); |
| 112 | + |
| 113 | + static::assertEquals(\getenv('APP_ENVIRONMENT'), $container->getParameter('service_bus.environment')); |
| 114 | + static::assertEquals(\getenv('APP_ENTRY_POINT_NAME'), $container->getParameter('service_bus.entry_point')); |
| 115 | + } |
| 116 | +} |
0 commit comments