Skip to content

Commit

Permalink
tests: align addModule tests
Browse files Browse the repository at this point in the history
  • Loading branch information
luislard committed May 6, 2024
1 parent 6fc4bb2 commit 74f85f1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
16 changes: 16 additions & 0 deletions tests/example/cases/ProjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,39 @@ public function executeProject(): void
fwrite(STDOUT, print_r(app()->resolve(Logger::class)->allLogs(), true));
}

protected function checkLateModule(bool $expectToBeThere = false): void
{
if (!$expectToBeThere) {
static::assertTrue(app()->status()->isBooted());
static::assertNull(app()->resolve('dummy-test'));
return;
}
static::assertNotNull(app()->resolve('dummy-test'));
static::assertEquals(app()->resolve('dummy-test')->text(), 'Hello World');
}

/**
* @return void
*/
protected function onBeforePlugins(): void
{
$this->checkLateModule();
}

/**
* @return void
*/
protected function onAfterPlugins(): void
{
$this->checkLateModule();
}

/**
* @return void
*/
protected function onAfterTheme(): void
{
$this->checkLateModule();
}

/**
Expand All @@ -61,6 +75,8 @@ protected function onAfterInit(): void
static::assertTrue($logger->hasLog("{$pre} 'Lorem Ipsum'"));
static::assertTrue($logger->hasLog("{$pre} 'Dolor Sit Amet'"));
static::assertFalse($logger->hasLog("{$pre} '[From Plugin 2] Plugin Two is Good For You'"));

$this->checkLateModule(true);
}

/**
Expand Down
19 changes: 19 additions & 0 deletions tests/example/sources/libraries/modularity-lib/src/LateModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Inpsyde\App\Tests\Project\ModularityLib;

use Inpsyde\App\Tests\Project\ModularityPlugin3\Calc;
use Inpsyde\Modularity\Module\ServiceModule;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

Expand All @@ -28,4 +29,22 @@ static function () use ($container): void {

return parent::run($container);
}

public function services(): array
{
return array_merge(
parent::services(),
[
// phpcs:ignore Inpsyde.CodeQuality.ReturnTypeDeclaration.NoReturnType
'dummy-test' => static function () {
return new class {
public function text(): string
{
return 'Hello World';
}
};
},
]
);
}
}
21 changes: 20 additions & 1 deletion tests/unit/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Inpsyde\App\Tests;

use Inpsyde\App\App;
use Inpsyde\App\AppStatus;
use Inpsyde\App\CompositeContainer;
use Inpsyde\Modularity\Module\ServiceModule;
use Inpsyde\Modularity\Package;
Expand All @@ -25,6 +26,8 @@ class AppTest extends TestCase
private $appHandleModularityBoot;
/** @var \ReflectionMethod */
private $appSyncModularityStatus;
/** @var \ReflectionClass */
private $appStatusReflection;

protected function setUp(): void
{
Expand All @@ -41,6 +44,9 @@ protected function setUp(): void
$this->appBootQueueProp->setAccessible(true);
$this->appHandleModularityBoot = $reflectedApp->getMethod('handleModularityBoot');
$this->appSyncModularityStatus = $reflectedApp->getMethod('syncModularityStatus');

$this->appStatusReflection = new \ReflectionClass(AppStatus::class);

parent::setUp();
}

Expand Down Expand Up @@ -112,6 +118,11 @@ public function testAddEarlyModule()
static::assertInstanceOf(\ArrayObject::class, $app->resolve($moduleServiceId));
}

/**
*
* @return void
* @throws \ReflectionException
*/
public function testAddModule()
{
$context = WpContext::new()->force(WpContext::CORE);
Expand All @@ -123,8 +134,16 @@ public function testAddModule()
$app->addModule($module);
// We expect the service is not resolvable if the App Container is not booted
static::assertEquals(null, $app->resolve($moduleServiceId));

// we have to force the internal status of the AppStatus
// we need $lastRun to be true when calling isThemeStep inside boot
$appStatusInternalStatusProp = $this->appStatusReflection->getProperty('status');
$appStatusInternalStatusProp->setValue(
$this->appStatusProp->getValue($app),
AppStatus::REGISTERING_THEMES
);

$app->boot();
// TODO: check how addModule was meant to work
static::assertInstanceOf(\ArrayObject::class, $app->resolve($moduleServiceId));
}

Expand Down

0 comments on commit 74f85f1

Please sign in to comment.