Esempio n. 1
0
 public function test_factory_with_array_syntax_with_static_method()
 {
     $container = new Container();
     $container->set(IImplementation::class, [self::class, 'staticMethodFactory']);
     $value = $container->get(IImplementation::class, ['foo' => 'bar']);
     $this->assertTrue($value instanceof SimpleImplementation);
 }
Esempio n. 2
0
 public function test_get_value()
 {
     $container = new Container();
     $container->set('foo', 'bar');
     $value = $container->get('foo');
     $this->assertEquals('bar', $value);
 }
Esempio n. 3
0
 public function test_container_can_toggle_strict_mode()
 {
     $container = new Container(false);
     $this->assertFalse($container->isInStrictMode());
     $container->setStrictMode(true);
     $this->assertTrue($container->isInStrictMode());
 }
 public function test_invoker_puts_provider_instance_into_the_container()
 {
     $container = new Container();
     $invoker = new ProviderInvoker($container);
     $provider = $invoker->create(ComplexProvider::class, new Dictionary());
     $this->assertTrue($container->has(ComplexProvider::class));
     $this->assertTrue($provider === $container->get(ComplexProvider::class));
 }
 public function test_get_repository_with_loader()
 {
     $container = new Container();
     $om = new FakeObjectManager();
     $loader = new DoctrineRepositoriesLoader($container, $om);
     $loader->enable();
     $repository = $container->get(FakeRepository::class);
     $this->assertTrue($repository instanceof FakeRepository);
     $anotherRepository = $container->get(FakeRepository::class);
     $this->assertTrue($repository === $anotherRepository);
 }
 public function test_instantiate_ignores_singletons()
 {
     $container = new Container();
     $simple = new SimpleClass();
     $container->set(ComplexClass::class, function () use($simple) {
         return new ComplexClass($simple, 'factory');
     })->singleton();
     $instance1 = $container->get(ComplexClass::class);
     $instance2 = $container->get(ComplexClass::class);
     $this->assertTrue($instance1 === $instance2);
     $this->assertEquals('factory', $instance1->foo);
     $this->assertEquals('factory', $instance2->foo);
     $this->assertTrue($instance1->simple === $instance2->simple);
     $instance3 = $container->instantiate(ComplexClass::class, ['foo' => 'instance']);
     $this->assertEquals('instance', $instance3->foo);
     $this->assertFalse($instance3 === $instance1);
     $this->assertFalse($instance3->simple === $instance1->simple);
 }
Esempio n. 7
0
 public function test_get_interface_with_instance_as_singleton()
 {
     $container = new Container();
     $container->set(IImplementation::class, new SimpleImplementation());
     $this->assertTrue($container->get(IImplementation::class) === $container->get(IImplementation::class));
     $container->set(IImplementation::class, new SimpleImplementation())->singleton();
     $this->assertTrue($container->get(IImplementation::class) === $container->get(IImplementation::class));
 }
Esempio n. 8
0
 public function test_remove_parent_definition_and_get_alias()
 {
     $container = new Container();
     $container->set([SimpleImplementation::class, IImplementation::class], SimpleImplementation::class);
     $value1 = $container->get(SimpleImplementation::class);
     $value2 = $container->get(IImplementation::class);
     $this->assertTrue($value1 instanceof SimpleImplementation);
     $this->assertTrue($value2 instanceof SimpleImplementation);
     $container->remove(SimpleImplementation::class);
     $this->assertFalse($container->has(SimpleImplementation::class));
     $this->assertTrue($container->has(IImplementation::class));
     $value2 = $container->get(IImplementation::class);
     $this->assertTrue($value2 instanceof SimpleImplementation);
 }
Esempio n. 9
0
 public function test_get_with_wildcard_properly_passes_the_actual_id()
 {
     $container = new Container();
     $container->set('#Class#', function ($abstract) {
         return new ComplexClass(new SimpleClass(), $abstract);
     });
     $value = $container->get(ComplexClass::class);
     $this->assertTrue($value instanceof ComplexClass);
     $this->assertEquals(ComplexClass::class, $value->foo);
 }
Esempio n. 10
0
 public function test_set_definition_by_instance()
 {
     $container = new Container();
     $value = new SimpleImplementation();
     $container->set($value);
     $value2 = $container->get(SimpleImplementation::class);
     $this->assertTrue($value === $value2);
 }
Esempio n. 11
0
 public function test_get_interface_with_interface_without_strict_mode()
 {
     $container = new Container(false);
     $container->set(IImplementation::class, IImplementation::class);
     $this->setExpectedException(ImplementationNotFoundException::class);
     $container->get(IImplementation::class);
 }
Esempio n. 12
0
 public function test_call_with_invalid_callable()
 {
     $container = new Container();
     $this->setExpectedException(InvalidCallableFormatException::class);
     $this->assertEquals(1, $container->call(['foo'], ['array' => [1]]));
 }