Skip to content

Commit

Permalink
chore: enhancing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
luislard committed May 4, 2024
1 parent a2b2788 commit 9dd5ead
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 53 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"minimum-stability": "stable",
"scripts": {
"cs": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcs",
"fix:cs": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcbf",
"psalm": "@php ./vendor/vimeo/psalm/psalm --no-cache --output-format=compact",
"tests": "@php ./vendor/phpunit/phpunit/phpunit",
"tests:no-cov": "@php ./vendor/phpunit/phpunit/phpunit --no-coverage",
Expand Down
3 changes: 3 additions & 0 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,9 @@ public function sharePackage(Modularity\Package $package, string ...$contexts):
}

/**
* If context is correct
* If package is booted
* Adds the Package Container to the App Container
* @param Modularity\Package $package
* @param string ...$contexts
* @return App
Expand Down
10 changes: 5 additions & 5 deletions tests/src/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,19 @@ protected function mockModule(string $id = 'module', string ...$interfaces): Mod
$stub = \Mockery::mock(...$interfaces);
$stub->allows('id')->andReturn($id);

if (in_array(ServiceModule::class, $interfaces, true) ) {
if (in_array(ServiceModule::class, $interfaces, true)) {
$stub->allows('services')->byDefault()->andReturn([]);
}

if (in_array(FactoryModule::class, $interfaces, true) ) {
if (in_array(FactoryModule::class, $interfaces, true)) {
$stub->allows('factories')->byDefault()->andReturn([]);
}

if (in_array(ExtendingModule::class, $interfaces, true) ) {
if (in_array(ExtendingModule::class, $interfaces, true)) {
$stub->allows('extensions')->byDefault()->andReturn([]);
}

if (in_array(ExecutableModule::class, $interfaces, true) ) {
if (in_array(ExecutableModule::class, $interfaces, true)) {
$stub->allows('run')->byDefault()->andReturn(false);
}

Expand All @@ -151,7 +151,7 @@ protected function stubServices(string ...$ids): array
{
$services = [];
foreach ($ids as $id) {
$services[$id] = static function () use ($id) {
$services[$id] = static function () use ($id): \ArrayObject {
return new \ArrayObject(['id' => $id]);
};
}
Expand Down
137 changes: 89 additions & 48 deletions tests/unit/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Inpsyde\App\App;
use Inpsyde\App\CompositeContainer;
use Inpsyde\Modularity\Container\ReadOnlyContainer;
use Inpsyde\Modularity\Module\ServiceModule;
use Inpsyde\Modularity\Package;
use Inpsyde\WpContext;
Expand All @@ -15,9 +16,14 @@

class AppTest extends TestCase
{
private \ReflectionProperty $containerProp;
private \ReflectionProperty $mapProp;
private \ReflectionProperty $appStatusProp;
/** @var \ReflectionProperty */
private $containerProp;
/** @var \ReflectionProperty */
private $mapProp;
/** @var \ReflectionProperty */
private $appStatusProp;
/** @var \ReflectionProperty */
private $appBootQueueProp;

protected function setUp(): void
{
Expand All @@ -29,24 +35,13 @@ protected function setUp(): void
$reflectedContainer = new \ReflectionClass(CompositeContainer::class);
$this->mapProp = $reflectedContainer->getProperty('map');
$this->mapProp->setAccessible(true);
$this->appBootQueueProp = $reflectedApp->getProperty('bootQueue');
$this->appBootQueueProp->setAccessible(true);
parent::setUp();
}

public function testNewWithNoContainer()
{

$context = WpContext::new()->force(WpContext::CORE);
$app = App::new(null, null, $context);
static::assertInstanceOf(
CompositeContainer::class,
$this->containerProp->getValue($app)
);
static::assertTrue($this->appStatusProp->getValue($app)->isIdle());
}

public function testSharePackageToBootShouldAddPackageServicesToContainerIfPackageIsBooted()
private function prepareShareToPackageCommon(): array
{

\Brain\Monkey\Functions\stubs([
'plugins_url' => static function (): string {
return content_url() . '/plugins/fake';
Expand Down Expand Up @@ -74,57 +69,103 @@ public function testSharePackageToBootShouldAddPackageServicesToContainerIfPacka

$package = Package::new($this->mockProperties())->addModule($module);

static::assertTrue($package->boot());
static::assertInstanceOf(App::class, $app->sharePackageToBoot($package));
$container = $this->containerProp->getValue($app);

static::assertTrue($container->has($packageServiceId));
return [
'app' => $app,
'appModuleId' => $appModuleId,
'containerServiceId' => $containerServiceId,
'moduleId' => $moduleId,
'packageServiceId' => $packageServiceId,
'package' => $package,
];
}

public function testSharePackageToBootShouldAddPackageServicesToContainerAfterBootIfPackageIsNotBooted()
public function testNewWithNoContainer()
{

\Brain\Monkey\Functions\stubs([
'plugins_url' => static function (): string {
return content_url() . '/plugins/fake';
},
]);

$context = WpContext::new()->force(WpContext::CORE);
$app = App::new(null, null, $context);
static::assertInstanceOf(
CompositeContainer::class,
$this->containerProp->getValue($app)
);
$appModuleId = 'app-module-id';
$containerServiceId = 'cont-service-id';
static::assertTrue($this->appStatusProp->getValue($app)->isIdle());
}

$appModule = $this->mockModule($appModuleId, ServiceModule::class);
$appModule->expects('services')->andReturn($this->stubServices($containerServiceId));
$app->addModule($appModule);
public function testSharePackageToBootShouldAddPackageServicesToContainerIfPackageIsBooted()
{
[
'app' => $app,
'containerServiceId' => $containerServiceId,
'packageServiceId' => $packageServiceId,
'package' => $package
] = $this->prepareShareToPackageCommon();

// When package is booted
static::assertTrue($package->boot());

$moduleId = 'my-service-module';
$packageServiceId = 'service-id';
// When we call sharePackageToBoot and pass a booted package
static::assertInstanceOf(App::class, $app->sharePackageToBoot($package));

$module = $this->mockModule($moduleId, ServiceModule::class);
$module->expects('services')->andReturn($this->stubServices($packageServiceId));
$container = $this->containerProp->getValue($app);

$package = Package::new($this->mockProperties())->addModule($module);
// The Services from the Package are shared to the App Container
static::assertTrue($container->has($packageServiceId));

// But, the services from the App Container are NOT added to the Package since the container is ReadOnly
static::assertInstanceOf(ReadOnlyContainer::class, $package->container());
static::assertFalse($package->container()->has($containerServiceId));
}

public function testSharePackageToBootShouldAddPackageServicesToContainerAfterBootIfPackageIsNotBooted()
{

[
'app' => $app,
'containerServiceId' => $containerServiceId,
'packageServiceId' => $packageServiceId,
'package' => $package
] = $this->prepareShareToPackageCommon();

Monkey\Functions\when('remove_all_actions')->justReturn();

// When we call sharePackageToBoot passing a NOT booted package
static::assertInstanceOf(App::class, $app->sharePackageToBoot($package));

// we don't expect the services from the Package to be in the App Container before booting the App Container
$container = $this->containerProp->getValue($app);
Monkey\Functions\when('remove_all_actions')->justReturn();
static::assertFalse($container->has($packageServiceId));

// we don't expect the package to have container to be accesible because is not booted nor built.
static::assertFalse($package->statusIs(Package::STATUS_BOOTED));
try {
$package->container();
} catch (\Exception $exception) {
static::assertStringContainsString(
'Can\'t obtain the container',
$exception->getMessage()
);
}

// It is only connected with a new Package that is booted already
static::assertEquals([ "inpsyde-wp-app" => true ], $package->connectedPackages());

// we boot the app container
// We expect the Package to be added to the boot queue
/** @var \SplObjectStorage $currentQueue */
$currentQueue = $this->appBootQueueProp->getValue($app);
static::assertTrue($currentQueue->contains($package));

// if we boot the App container
$app->boot();
$container = $this->containerProp->getValue($app);

// We expect the App Container to have the Package services
static::assertTrue($container->has($packageServiceId));
static::assertEquals([ "inpsyde-wp-app" => true ], $package->connectedPackages());

/**
* The following assert is failing
*/
// static::assertTrue($package->container()->has($containerServiceId));
}
// We expect the Package to be booted
static::assertTrue($package->statusIs(Package::STATUS_BOOTED));

}
// we expect the App Container services to be in the Package
// sharePackageToBoot is meant to boot the Package
static::assertFalse($package->container()->has($containerServiceId));
}
}

0 comments on commit 9dd5ead

Please sign in to comment.