Skip to content

Commit e66a09b

Browse files
committed
bootstrap tests
1 parent 69f5a2c commit e66a09b

File tree

6 files changed

+159
-8
lines changed

6 files changed

+159
-8
lines changed

phpunit.xml

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
<php>
1313
<ini name="error_reporting" value="-1"/>
1414
<env name="TEST_POSTGRES_DSN" value="pgsql://postgres:123456789@localhost:5432/test"/>
15+
<env name="TRANSPORT_CONNECTION_DSN" value="amqp://user:password@host:port"/>
16+
<env name="DATABASE_CONNECTION_DSN" value="sqlite:///:memory:"/>
1517
</php>
1618

1719
<testsuites>

src/Application/Bootstrap.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,11 @@ private function __construct()
177177
$this->containerBuilder = new ContainerBuilder($entryPoint, Environment::create($envValue));
178178

179179
$this->containerBuilder->addParameters([
180-
'service_bus.environment' => $envValue,
181-
'service_bus.entry_point' => $entryPoint
180+
'service_bus.environment' => $envValue,
181+
'service_bus.entry_point' => $entryPoint,
182+
'service_bus.transport.dsn' => '',
183+
'service_bus.storage.adapter' => '',
184+
'service_bus.storage.dsn' => ''
182185
]);
183186
$this->containerBuilder->addCompilerPasses(new ServicesCompilerPass(), new ServiceLocatorTagPass());
184187
$this->containerBuilder->addExtensions(new ServiceBusExtension());

src/Common/commonFunctions.php

+22
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,25 @@ function uuid(): string
2727
/** @noinspection PhpUnhandledExceptionInspection */
2828
return Uuid::uuid4()->toString();
2929
}
30+
31+
/**
32+
* @param string $path
33+
*
34+
* @return void
35+
*/
36+
function removeDirectory(string $path): void
37+
{
38+
$files = \glob(\preg_replace('/(\*|\?|\[)/', '[$1]', $path) . '/{,.}*', GLOB_BRACE);
39+
40+
foreach($files as $file)
41+
{
42+
if($file === $path . '/.' || $file === $path . '/..')
43+
{
44+
continue;
45+
}
46+
47+
\is_dir($file) ? removeDirectory($file) : unlink($file);
48+
}
49+
50+
\rmdir($path);
51+
}

tests/Application/BootstrapTest.php

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
TRANSPORT_CONNECTION_DSN=amqp://user:password@host:port
2+
TRANSPORT_TOPIC=testing
3+
TRANSPORT_ROUTING_KEY=dev
4+
DATABASE_CONNECTION_DSN=sqlite:///:memory:
5+
SENDER_DESTINATION_TOPIC=testing
6+
SENDER_DESTINATION_TOPIC_ROUTING_KEY=testing
7+
APP_ENVIRONMENT=dev
8+
APP_ENTRY_POINT_NAME=qwerty

tests/DependencyInjection/ContainerBuilder/ContainerBuilderTest.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace Desperado\ServiceBus\Tests\DependencyInjection\ContainerBuilder;
1515

16+
use function Desperado\ServiceBus\Common\removeDirectory;
1617
use Desperado\ServiceBus\DependencyInjection\Compiler\ServicesCompilerPass;
1718
use Desperado\ServiceBus\DependencyInjection\ContainerBuilder\ContainerBuilder;
1819
use Desperado\ServiceBus\DependencyInjection\Extensions\ServiceBusExtension;
@@ -24,7 +25,6 @@
2425
use Desperado\ServiceBus\Tests\DependencyInjection\ContainerBuilder\Stubs\TestCompilerPass;
2526
use Desperado\ServiceBus\Tests\DependencyInjection\ContainerBuilder\Stubs\TestExtension;
2627
use PHPUnit\Framework\TestCase;
27-
use Symfony\Component\DependencyInjection\ContainerInterface;
2828

2929
/**
3030
*
@@ -58,7 +58,7 @@ protected function tearDown(): void
5858
{
5959
parent::tearDown();
6060

61-
@\unlink($this->cacheDirectory);
61+
removeDirectory($this->cacheDirectory);
6262

6363
unset($this->cacheDirectory);
6464
}
@@ -74,7 +74,7 @@ public function successfulBuildWithDefaultData(): void
7474

7575
static::assertFalse($containerBuilder->hasActualContainer());
7676

77-
/** @var ContainerInterface $container */
77+
/** @var \Symfony\Component\DependencyInjection\ContainerInterface $container */
7878
$container = $containerBuilder->build();
7979

8080
static::assertFileExists(\sys_get_temp_dir() . '/containerBuilderTestProdProjectContainer.php');
@@ -107,16 +107,16 @@ public function successfulBuildWithFullConfiguration(): void
107107
$containerBuilder->addParameters([
108108
'testing.class' => \get_class($this),
109109
'service_bus.transport.dsn' => 'amqp://user:password@host:port',
110-
'service_bus.storage.adapter' => '',
111-
'service_bus.storage.dsn' => DoctrineDBALAdapter::class
110+
'service_bus.storage.adapter' => DoctrineDBALAdapter::class,
111+
'service_bus.storage.dsn' => ''
112112
]
113113
);
114114

115115
$containerBuilder->addExtensions(new ServiceBusExtension(), new TestExtension());
116116

117117
$containerBuilder->addCompilerPasses(new TestCompilerPass(), new ServicesCompilerPass());
118118

119-
/** @var ContainerInterface $container */
119+
/** @var \Symfony\Component\DependencyInjection\ContainerInterface $container */
120120
$container = $containerBuilder->build();
121121

122122
static::assertFileExists($this->cacheDirectory . '/containerBuilderTestDevProjectContainer.php');

0 commit comments

Comments
 (0)