From 5de7cf75a197ac8eb97223e5f152a91da4af2519 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Fri, 29 Nov 2024 20:26:14 +0900 Subject: [PATCH] Refactor LazyModule initialization in tests. 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. --- src/LazyModule.php | 21 ---------- tests/AssistedTest.php | 4 +- ...pileInjectorExtendedScriptInjectorTest.php | 40 +++++-------------- tests/ContextBindingTest.php | 4 +- tests/MultiBindingTest.php | 7 ++-- tests/ScriptInjectorNullObjectTest.php | 6 +-- tests/script/null_object.php | 6 +-- 7 files changed, 18 insertions(+), 70 deletions(-) diff --git a/src/LazyModule.php b/src/LazyModule.php index f7cfe09..f007c35 100644 --- a/src/LazyModule.php +++ b/src/LazyModule.php @@ -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)(); - } - }; - } } diff --git a/tests/AssistedTest.php b/tests/AssistedTest.php index a873ef1..a5225e8 100644 --- a/tests/AssistedTest.php +++ b/tests/AssistedTest.php @@ -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()) ); } diff --git a/tests/CompileInjectorExtendedScriptInjectorTest.php b/tests/CompileInjectorExtendedScriptInjectorTest.php index 1459522..94cb8fc 100644 --- a/tests/CompileInjectorExtendedScriptInjectorTest.php +++ b/tests/CompileInjectorExtendedScriptInjectorTest.php @@ -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()) ); } @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); diff --git a/tests/ContextBindingTest.php b/tests/ContextBindingTest.php index 5c3fead..6ecf72a 100644 --- a/tests/ContextBindingTest.php +++ b/tests/ContextBindingTest.php @@ -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('')) ); } diff --git a/tests/MultiBindingTest.php b/tests/MultiBindingTest.php index 2be2a39..7def363 100644 --- a/tests/MultiBindingTest.php +++ b/tests/MultiBindingTest.php @@ -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 */ diff --git a/tests/ScriptInjectorNullObjectTest.php b/tests/ScriptInjectorNullObjectTest.php index 5604ee5..fe3c251 100644 --- a/tests/ScriptInjectorNullObjectTest.php +++ b/tests/ScriptInjectorNullObjectTest.php @@ -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); diff --git a/tests/script/null_object.php b/tests/script/null_object.php index 43cf9bd..b8755b0 100644 --- a/tests/script/null_object.php +++ b/tests/script/null_object.php @@ -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);