Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: adding unit testing to App class #21

Draft
wants to merge 7 commits into
base: 3.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 4 additions & 1 deletion 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 Expand Up @@ -475,7 +478,6 @@ private function addModularityModule(

return $this;
}

$this->initializeModularity($early);
$this->ensureWillBoot();

Expand Down Expand Up @@ -634,6 +636,7 @@ private function handleModularityBoot(
: Modularity\Package::MODULE_EXECUTED;
$this->syncModularityStatus($package, $status);

// TODO: this if condition is running twice when called from sharePackage or sharePackageToBoot
if (
$package->statusIs(Modularity\Package::STATUS_BOOTED)
|| ($onPackageReady && $package->statusIs(Modularity\Package::STATUS_INITIALIZED))
Expand Down
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';
}
};
},
]
);
}
}
75 changes: 75 additions & 0 deletions tests/src/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@
namespace Inpsyde\App\Tests;

use Brain\Monkey;
use Inpsyde\Modularity\Module\ExecutableModule;
use Inpsyde\Modularity\Module\ExtendingModule;
use Inpsyde\Modularity\Module\FactoryModule;
use Inpsyde\Modularity\Module\Module;
use Inpsyde\Modularity\Module\ServiceModule;
use Inpsyde\Modularity\Properties\Properties;
use Inpsyde\WpContext;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use Mockery\MockInterface;
use PHPUnit\Framework\TestCase as FrameworkTestCase;

abstract class TestCase extends FrameworkTestCase
Expand Down Expand Up @@ -58,6 +65,9 @@ protected function setUp(): void
'get_stylesheet_directory_uri' => static function (): string {
return get_theme_root_uri() . '/my-theme';
},
'plugins_url' => static function (): string {
return content_url() . '/plugins';
},
]);
}

Expand All @@ -83,4 +93,69 @@ protected function factoryContext(?string $case = null, bool $withCli = false):

return $withCli ? $context->withCli() : $context;
}

/**
* @param string $basename
* @param bool $isDebug
*
* @return Properties|MockInterface
*/
protected function mockProperties(
string $basename = 'basename',
bool $isDebug = false
): Properties {

$stub = \Mockery::mock(Properties::class);
$stub->allows('basename')->andReturn($basename);
$stub->allows('isDebug')->andReturn($isDebug);

return $stub;
}

/**
* @param string $id
* @param class-string ...$interfaces
* @return Module|MockInterface
*/
protected function mockModule(string $id = 'module', string ...$interfaces): Module
{
$interfaces or $interfaces[] = Module::class;

$stub = \Mockery::mock(...$interfaces);
$stub->allows('id')->andReturn($id);

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

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

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

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

return $stub;
}

/**
* @param string ...$ids
* @return array<string, callable>
*/
protected function stubServices(string ...$ids): array
{
$services = [];
foreach ($ids as $id) {
$services[$id] = static function () use ($id): \ArrayObject {
return new \ArrayObject(['id' => $id]);
};
}

return $services;
}
}
Loading
Loading