Skip to content

Commit

Permalink
Refactor LazyModule initialization in tests.
Browse files Browse the repository at this point in the history
Replaced the usage of LazyModule::getInstance with direct instantiation of LazyModule for improved readability and maintainability in test files. Removed the unnecessary getInstance method from the LazyModule class, simplifying the code. This change ensures a more straightforward and consistent approach to handling LazyModule objects across the codebase.
  • Loading branch information
koriym committed Nov 29, 2024
1 parent b028815 commit 5de7cf7
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 70 deletions.
21 changes: 0 additions & 21 deletions src/LazyModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,4 @@ public function __invoke(): AbstractModule
{
return $this->module;
}

/**
* Create a lazy module from a callable that returns a module
*/
public static function getInstance(callable $callable): LazyModuleInterface
{
return new class ($callable) implements LazyModuleInterface {
/** @var callable */
private $callable;

public function __construct(callable $callable)
{
$this->callable = $callable;
}

public function __invoke(): AbstractModule
{
return ($this->callable)();
}
};
}
}
4 changes: 1 addition & 3 deletions tests/AssistedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ protected function setUp(): void
{
$this->injector = new CompileInjector(
__DIR__ . '/tmp',
LazyModule::getInstance(static function () {
return new FakeToBindModule();
})
new LazyModule(new FakeToBindModule())
);
}

Expand Down
40 changes: 10 additions & 30 deletions tests/CompileInjectorExtendedScriptInjectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ protected function setUp(): void
@mkdir(__DIR__ . '/tmp');
$this->injector = new CompileInjector(
__DIR__ . '/tmp',
LazyModule::getInstance(static function (): AbstractModule {
return new FakeCarModule();
})
new LazyModule(new FakeCarModule())
);
}

Expand All @@ -38,9 +36,7 @@ public function testGetInstance(): FakeCar
$tmpDir = $this->getTmpDir(__FUNCTION__);
$injector = new CompileInjector(
$tmpDir,
LazyModule::getInstance(static function (): AbstractModule {
return new FakeCarModule();
})
new LazyModule(new FakeCarModule())
);

$car = $injector->getInstance(FakeCarInterface::class);
Expand All @@ -66,9 +62,7 @@ public function testToPrototype(): void
$tmpDir = $this->getTmpDir(__FUNCTION__);
$injector = new CompileInjector(
$tmpDir,
LazyModule::getInstance(static function (): AbstractModule {
return new FakeToBindPrototypeModule();
})
new LazyModule(new FakeToBindPrototypeModule())
);
$instance1 = $injector->getInstance(FakeRobotInterface::class);
$instance2 = $injector->getInstance(FakeRobotInterface::class);
Expand All @@ -80,9 +74,7 @@ public function testToSingleton(): void
$tmpDir = $this->getTmpDir(__FUNCTION__);
$injector = new CompileInjector(
$tmpDir,
LazyModule::getInstance(static function (): AbstractModule {
return new FakeToBindSingletonModule();
})
new LazyModule(new FakeToBindSingletonModule())
);
$instance1 = $injector->getInstance(FakeRobotInterface::class);
$instance2 = $injector->getInstance(FakeRobotInterface::class);
Expand All @@ -94,9 +86,7 @@ public function testToProviderPrototype(): void
$tmpDir = $this->getTmpDir(__FUNCTION__);
$injector = new CompileInjector(
$tmpDir,
LazyModule::getInstance(static function (): AbstractModule {
return new FakeToProviderPrototypeModule();
})
new LazyModule(new FakeToProviderPrototypeModule())
);
$instance1 = $injector->getInstance(FakeRobotInterface::class);
$instance2 = $injector->getInstance(FakeRobotInterface::class);
Expand All @@ -108,9 +98,7 @@ public function testToProviderSingleton(): void
$tmpDir = $this->getTmpDir(__FUNCTION__);
$injector = new CompileInjector(
$tmpDir,
LazyModule::getInstance(static function (): AbstractModule {
return new FakeToProviderSingletonModule();
})
new LazyModule(new FakeToProviderSingletonModule())
);
$instance1 = $injector->getInstance(FakeRobotInterface::class);
$instance2 = $injector->getInstance(FakeRobotInterface::class);
Expand All @@ -122,9 +110,7 @@ public function testToInstancePrototype(): void
$tmpDir = $this->getTmpDir(__FUNCTION__);
$injector = new CompileInjector(
$tmpDir,
LazyModule::getInstance(static function (): AbstractModule {
return new FakeToInstancePrototypeModule();
})
new LazyModule(new FakeToInstancePrototypeModule())
);
$instance1 = $injector->getInstance(FakeRobotInterface::class);
$instance2 = $injector->getInstance(FakeRobotInterface::class);
Expand All @@ -136,9 +122,7 @@ public function testToInstanceSingleton(): void
$tmpDir = $this->getTmpDir(__FUNCTION__);
$injector = new CompileInjector(
$tmpDir,
LazyModule::getInstance(static function (): AbstractModule {
return new FakeToInstanceSingletonModule();
})
new LazyModule(new FakeToInstanceSingletonModule())
);
$instance1 = $injector->getInstance(FakeRobotInterface::class);
$instance2 = $injector->getInstance(FakeRobotInterface::class);
Expand All @@ -165,9 +149,7 @@ public function testAop(): void
$tmpDir = $this->getTmpDir(__FUNCTION__);
$injector = new CompileInjector(
$tmpDir,
LazyModule::getInstance(static function (): AbstractModule {
return new FakeCarModule();
})
new LazyModule(new FakeCarModule())
);
$instance1 = $injector->getInstance(FakeCarInterface::class);
$instance2 = $injector->getInstance(FakeCar::class);
Expand All @@ -183,9 +165,7 @@ public function testOnDemandSingleton(): void
$tmpDir = $this->getTmpDir(__FUNCTION__);
$injector = new CompileInjector(
$tmpDir,
LazyModule::getInstance(static function (): AbstractModule {
return new FakeToBindSingletonModule();
})
new LazyModule(new FakeToBindSingletonModule())
);
$dependSingleton1 = $injector->getInstance(FakeDependSingleton::class);
$dependSingleton2 = $injector->getInstance(FakeDependSingleton::class);
Expand Down
4 changes: 1 addition & 3 deletions tests/ContextBindingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ public function setUp(): void
deleteFiles(__DIR__ . '/tmp');
$this->injector = new CompileInjector(
__DIR__ . '/tmp',
LazyModule::getInstance(static function () {
return new FakeDependContextualRobotModule('');
})
new LazyModule(new FakeDependContextualRobotModule(''))
);
}

Expand Down
7 changes: 4 additions & 3 deletions tests/MultiBindingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ class MultiBindingTest extends TestCase
protected function setUp(): void
{
@mkdir(__DIR__ . '/tmp/mulit-bindings');
$this->injector = new CompileInjector(__DIR__ . '/tmp/mulit-bindings', LazyModule::getInstance(static function () {
return new FakeMultiBindingsModule();
}));
$this->injector = new CompileInjector(
__DIR__ . '/tmp/mulit-bindings',
new LazyModule(new FakeMultiBindingsModule())
);
}

/** @return Map<FakeEngineInterface> */
Expand Down
6 changes: 1 addition & 5 deletions tests/ScriptInjectorNullObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ public function testNullObjectCompile(): CompileInjector

$injector = new CompileInjector(
__DIR__ . '/tmp/null_object',
LazyModule::getInstance(
static function () {
return new FakeNullObjectModule();
}
)
new LazyModule(new FakeNullObjectModule())
);
$instance = $injector->getInstance(FakeTyreInterface::class);
$this->assertInstanceOf(FakeTyreInterface::class, $instance);
Expand Down
6 changes: 1 addition & 5 deletions tests/script/null_object.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@

$injector = new CompileInjector(
dirname(__DIR__) . '/tmp/null_object',
LazyModule::getInstance(
static function () {
return new FakeNullObjectModule();
}
)
new LazyModule(new FakeNullObjectModule())
);
$instance = $injector->getInstance(FakeTyreInterface::class);

0 comments on commit 5de7cf7

Please sign in to comment.