Skip to content

Commit a8de7f8

Browse files
authored
Merge pull request #84 from visto9259/fix-25
Fix #25 assertModulesLoaded not matching all loaded Modules
2 parents 607c55c + a16757f commit a8de7f8

File tree

8 files changed

+84
-450
lines changed

8 files changed

+84
-450
lines changed

psalm-baseline.xml

-437
Large diffs are not rendered by default.

psalm.xml

+8
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,15 @@
2929
<errorLevel type="suppress">
3030
<referencedMethod name="PHPUnit\Framework\MockObject\Builder\InvocationMocker::with"/>
3131
</errorLevel>
32+
<errorLevel type="suppress">
33+
<referencedMethod name="PHPUnit\Framework\ExpectationFailedException::__construct"/>
34+
</errorLevel>
3235
</InternalMethod>
36+
<InternalClass>
37+
<errorLevel type="suppress">
38+
<referencedClass name="PHPUnit\Framework\ExpectationFailedException" />
39+
</errorLevel>
40+
</InternalClass>
3341
</issueHandlers>
3442
<plugins>
3543
<pluginClass class="Psalm\PhpUnitPlugin\Plugin"/>

src/PHPUnit/Controller/AbstractControllerTestCase.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Laminas\EventManager\StaticEventManager;
1010
use Laminas\Http\Request as HttpRequest;
1111
use Laminas\Http\Response;
12+
use Laminas\ModuleManager\ModuleManager;
1213
use Laminas\Mvc\Application;
1314
use Laminas\Mvc\ApplicationInterface;
1415
use Laminas\Mvc\Controller\ControllerManager;
@@ -30,6 +31,7 @@
3031
use function array_diff;
3132
use function array_intersect;
3233
use function array_key_exists;
34+
use function array_keys;
3335
use function array_merge;
3436
use function assert;
3537
use function class_exists;
@@ -381,7 +383,8 @@ public function triggerApplicationEvent($eventName)
381383
public function assertModulesLoaded(array $modules)
382384
{
383385
$moduleManager = $this->getApplicationServiceLocator()->get('ModuleManager');
384-
$modulesLoaded = $moduleManager->getModules();
386+
assert($moduleManager instanceof ModuleManager);
387+
$modulesLoaded = array_keys($moduleManager->getLoadedModules());
385388
$list = array_diff($modules, $modulesLoaded);
386389
if ($list) {
387390
throw new ExpectationFailedException($this->createFailureMessage(

test/PHPUnit/ModuleDependenciesTest.php

+23-5
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ public function testDependenciesModules(): void
2121
include __DIR__ . '/../_files/application.config.with.dependencies.php'
2222
);
2323
$sm = $this->getApplicationServiceLocator();
24-
$this->assertEquals(true, $sm->has('FooObject'));
25-
$this->assertEquals(true, $sm->has('BarObject'));
24+
$this->assertTrue($sm->has('FooObject'));
25+
$this->assertTrue($sm->has('BarObject'));
2626

2727
$this->assertModulesLoaded(['Foo', 'Bar']);
2828
$this->expectedException(ExpectationFailedException::class);
29-
$this->assertModulesLoaded(['Foo', 'Bar', 'Unknow']);
29+
$this->assertModulesLoaded(['Foo', 'Bar', 'Unknown']);
3030
}
3131

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

4141
$this->assertNotModulesLoaded(['Foo']);
4242
$this->expectedException(ExpectationFailedException::class);
4343
$this->assertNotModulesLoaded(['Foo', 'Bar']);
4444
}
45+
46+
/**
47+
* Test that 'assertModulesLoaded()' can detect modules that are loaded in a module's init() method
48+
*/
49+
public function testLoadedModulesUsingModuleInit(): void
50+
{
51+
$this->setApplicationConfig(
52+
include __DIR__ . '/../_files/application.config.with.modules.init.php'
53+
);
54+
$sm = $this->getApplicationServiceLocator();
55+
56+
// Check that modules loaded and created the below services
57+
$this->assertTrue($sm->has('FooObject'));
58+
$this->assertTrue($sm->has('QuxObject'));
59+
60+
// Assert for the module loaded via Module init() method
61+
$this->assertModulesLoaded(['Foo']);
62+
}
4563
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
return [
6+
'modules' => [
7+
'Laminas\Router',
8+
'Qux', // Qux will load Foo during module init event
9+
],
10+
'module_listener_options' => [
11+
'config_static_paths' => [],
12+
'module_paths' => [
13+
'Qux' => __DIR__ . '/modules-path/with-subdir/Qux',
14+
'Foo' => __DIR__ . '/modules-path/with-subdir/Foo',
15+
],
16+
],
17+
];

test/_files/modules-path/with-subdir/Bar/Module.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Bar;
66

77
use Laminas\Loader\StandardAutoloader;
8+
use stdClass;
89

910
class Module
1011
{
@@ -33,12 +34,7 @@ public function getServiceConfig()
3334
// Legacy Zend Framework aliases
3435
'aliases' => [],
3536
'factories' => [
36-
'BarObject' => static function ($sm) {
37-
$foo = $sm->get('FooObject');
38-
$foo->bar = 'baz';
39-
40-
return $foo;
41-
},
37+
'BarObject' => static fn(): stdClass => new stdClass(),
4238
],
4339
];
4440
}

test/_files/modules-path/with-subdir/Foo/Module.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function getServiceConfig()
3434
// Legacy Zend Framework aliases
3535
'aliases' => [],
3636
'factories' => [
37-
'FooObject' => static fn($sm): stdClass => new stdClass(),
37+
'FooObject' => static fn(): stdClass => new stdClass(),
3838
],
3939
];
4040
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Qux;
6+
7+
use Laminas\ModuleManager\Feature\InitProviderInterface;
8+
use Laminas\ModuleManager\ModuleManagerInterface;
9+
use stdClass;
10+
11+
/** @psalm-suppress UnusedClass */
12+
class Module implements InitProviderInterface
13+
{
14+
public function getConfig(): array
15+
{
16+
return [
17+
'service_manager' => [
18+
'factories' => [
19+
'QuxObject' => static fn(): stdClass => new stdClass(),
20+
],
21+
],
22+
];
23+
}
24+
25+
public function init(ModuleManagerInterface $manager): void
26+
{
27+
$manager->loadModule('Foo');
28+
}
29+
}

0 commit comments

Comments
 (0)