Skip to content

Commit

Permalink
Merge pull request #84 from visto9259/fix-25
Browse files Browse the repository at this point in the history
Fix #25  assertModulesLoaded not matching all loaded Modules
  • Loading branch information
Xerkus authored Sep 12, 2024
2 parents 607c55c + a16757f commit a8de7f8
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 450 deletions.
437 changes: 0 additions & 437 deletions psalm-baseline.xml

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,15 @@
<errorLevel type="suppress">
<referencedMethod name="PHPUnit\Framework\MockObject\Builder\InvocationMocker::with"/>
</errorLevel>
<errorLevel type="suppress">
<referencedMethod name="PHPUnit\Framework\ExpectationFailedException::__construct"/>
</errorLevel>
</InternalMethod>
<InternalClass>
<errorLevel type="suppress">
<referencedClass name="PHPUnit\Framework\ExpectationFailedException" />
</errorLevel>
</InternalClass>
</issueHandlers>
<plugins>
<pluginClass class="Psalm\PhpUnitPlugin\Plugin"/>
Expand Down
5 changes: 4 additions & 1 deletion src/PHPUnit/Controller/AbstractControllerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Laminas\EventManager\StaticEventManager;
use Laminas\Http\Request as HttpRequest;
use Laminas\Http\Response;
use Laminas\ModuleManager\ModuleManager;
use Laminas\Mvc\Application;
use Laminas\Mvc\ApplicationInterface;
use Laminas\Mvc\Controller\ControllerManager;
Expand All @@ -30,6 +31,7 @@
use function array_diff;
use function array_intersect;
use function array_key_exists;
use function array_keys;
use function array_merge;
use function assert;
use function class_exists;
Expand Down Expand Up @@ -381,7 +383,8 @@ public function triggerApplicationEvent($eventName)
public function assertModulesLoaded(array $modules)
{
$moduleManager = $this->getApplicationServiceLocator()->get('ModuleManager');
$modulesLoaded = $moduleManager->getModules();
assert($moduleManager instanceof ModuleManager);
$modulesLoaded = array_keys($moduleManager->getLoadedModules());
$list = array_diff($modules, $modulesLoaded);
if ($list) {
throw new ExpectationFailedException($this->createFailureMessage(
Expand Down
28 changes: 23 additions & 5 deletions test/PHPUnit/ModuleDependenciesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ public function testDependenciesModules(): void
include __DIR__ . '/../_files/application.config.with.dependencies.php'
);
$sm = $this->getApplicationServiceLocator();
$this->assertEquals(true, $sm->has('FooObject'));
$this->assertEquals(true, $sm->has('BarObject'));
$this->assertTrue($sm->has('FooObject'));
$this->assertTrue($sm->has('BarObject'));

$this->assertModulesLoaded(['Foo', 'Bar']);
$this->expectedException(ExpectationFailedException::class);
$this->assertModulesLoaded(['Foo', 'Bar', 'Unknow']);
$this->assertModulesLoaded(['Foo', 'Bar', 'Unknown']);
}

public function testBadDependenciesModules(): void
Expand All @@ -35,11 +35,29 @@ public function testBadDependenciesModules(): void
include __DIR__ . '/../_files/application.config.with.dependencies.disabled.php'
);
$sm = $this->getApplicationServiceLocator();
$this->assertEquals(false, $sm->has('FooObject'));
$this->assertEquals(true, $sm->has('BarObject'));
$this->assertFalse($sm->has('FooObject'));
$this->assertTrue($sm->has('BarObject'));

$this->assertNotModulesLoaded(['Foo']);
$this->expectedException(ExpectationFailedException::class);
$this->assertNotModulesLoaded(['Foo', 'Bar']);
}

/**
* Test that 'assertModulesLoaded()' can detect modules that are loaded in a module's init() method
*/
public function testLoadedModulesUsingModuleInit(): void
{
$this->setApplicationConfig(
include __DIR__ . '/../_files/application.config.with.modules.init.php'
);
$sm = $this->getApplicationServiceLocator();

// Check that modules loaded and created the below services
$this->assertTrue($sm->has('FooObject'));
$this->assertTrue($sm->has('QuxObject'));

// Assert for the module loaded via Module init() method
$this->assertModulesLoaded(['Foo']);
}
}
17 changes: 17 additions & 0 deletions test/_files/application.config.with.modules.init.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

return [
'modules' => [
'Laminas\Router',
'Qux', // Qux will load Foo during module init event
],
'module_listener_options' => [
'config_static_paths' => [],
'module_paths' => [
'Qux' => __DIR__ . '/modules-path/with-subdir/Qux',
'Foo' => __DIR__ . '/modules-path/with-subdir/Foo',
],
],
];
8 changes: 2 additions & 6 deletions test/_files/modules-path/with-subdir/Bar/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Bar;

use Laminas\Loader\StandardAutoloader;
use stdClass;

class Module
{
Expand Down Expand Up @@ -33,12 +34,7 @@ public function getServiceConfig()
// Legacy Zend Framework aliases
'aliases' => [],
'factories' => [
'BarObject' => static function ($sm) {
$foo = $sm->get('FooObject');
$foo->bar = 'baz';

return $foo;
},
'BarObject' => static fn(): stdClass => new stdClass(),
],
];
}
Expand Down
2 changes: 1 addition & 1 deletion test/_files/modules-path/with-subdir/Foo/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function getServiceConfig()
// Legacy Zend Framework aliases
'aliases' => [],
'factories' => [
'FooObject' => static fn($sm): stdClass => new stdClass(),
'FooObject' => static fn(): stdClass => new stdClass(),
],
];
}
Expand Down
29 changes: 29 additions & 0 deletions test/_files/modules-path/with-subdir/Qux/Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Qux;

use Laminas\ModuleManager\Feature\InitProviderInterface;
use Laminas\ModuleManager\ModuleManagerInterface;
use stdClass;

/** @psalm-suppress UnusedClass */
class Module implements InitProviderInterface
{
public function getConfig(): array
{
return [
'service_manager' => [
'factories' => [
'QuxObject' => static fn(): stdClass => new stdClass(),
],
],
];
}

public function init(ModuleManagerInterface $manager): void
{
$manager->loadModule('Foo');
}
}

0 comments on commit a8de7f8

Please sign in to comment.