public function testInstantiationWithSingleton() { $this->dependencyContainer->add('fakeDependencyWithouthSingleton', function () { return new FakeDependency('someValue'); }); $this->dependencyContainer->add('anotherDependencyWithouthSingleton', function () { return new AnotherDependency('otherValue'); }); $this->dependencyContainer->add('requiresDependencyInConstructorWithouthSingleton', function () { $injector = new DependencyInjectionService(); return new RequiresAnotherDependency($injector->get('fakeDependencyWithouthSingleton'), $injector->get('anotherDependencyWithouthSingleton')); }); foreach ([5000, 15000, 25000, 50000] as $cycles) { $start = microtime(true); for ($i = $cycles; $i > 0; $i--) { $this->dependencyContainer->get('requiresDependencyInConstructorWithouthSingleton'); } echo "\n{$cycles} cycles executed in " . (microtime(true) - $start) . " microseconds with singleton\n"; } }
public function testInjection() { $this->dependencyContainer->register('fakeDependency', function () { return new FakeDependency('someValue'); }); $this->dependencyContainer->register('anotherDependency', function () { return new AnotherDependency('anotherValue'); }); $this->dependencyContainer->register('requiresDependencyInConstructor', function () { $injector = new DependencyInjectionService(); return new RequiresAnotherDependency($injector->get('fakeDependency'), $injector->get('anotherDependency')); }); $instance = $this->dependencyContainer->get('requiresDependencyInConstructor'); $this->assertInstanceOf(FakeDependency::class, $instance->getFakeDependency()); }