Beispiel #1
0
 /**
  * @test
  */
 public function validInvokeWithAutowiredProperty()
 {
     BeanFactory::getInstance()->addBeanClass(Singleton::class, 'SingletonTestService');
     $instance = ClassInvoker::getNewInstance(ClassInvokerFixture::class);
     $refl = new ReflectionProperty(ClassInvokerFixture::class, 'singleton');
     $refl->setAccessible(true);
     $this->assertTrue($refl->getValue($instance) instanceof Singleton);
 }
Beispiel #2
0
 /**
  * Sets up the fixture, for example, opens a network connection.
  * This method is called before a test is executed.
  */
 protected function setUp()
 {
     $instance = new ReflectionProperty(BeanFactory::class, 'instance');
     $instance->setAccessible(true);
     $instance->setValue(null);
     BeanFactory::setAutoLoadSupport(true);
     $this->property = new ReflectionProperty(CollectionFixture::class, 'arrayObject');
 }
Beispiel #3
0
 /**
  * @param Reflector $refl
  * @param object $context
  */
 public function run(Reflector $refl, $context)
 {
     if ($refl instanceof \ReflectionProperty) {
         $type = Helper::getPropertyType($refl);
         $isPrimitiveType = in_array($type, Constants::$php_default_types) || in_array($type, Constants::$php_pseudo_types);
         if (!($refl instanceof ReflectionProperty || method_exists($refl, 'getAnnotation'))) {
             $refl = new ReflectionProperty($refl->getDeclaringClass()->getName(), $refl->getName());
         }
         $serviceName = ($qualifier = $refl->getAnnotation(Qualifier::class)) ? $qualifier->value : null;
         if (($type === null || $isPrimitiveType) && $serviceName === null) {
             throw new RuntimeException("Must set the property type by @var annotation or you must use @Quealifier annotation to define the service");
         }
         if (empty($serviceName)) {
             $serviceName = $type;
         }
         $service = BeanFactory::getInstance()->getBean($serviceName);
         $refl->setAccessible(true);
         $refl->setValue($context, $service);
     }
 }
Beispiel #4
0
 /**
  * @test
  * @ignore
  * @expectedException PhSpring\Service\NoSuchBeanDefinitionException
  */
 public function getMultiple()
 {
     BeanFactory::setAutoLoadSupport(true);
     $this->factory->addBeanClass(FirstImplementationOfInterface::class);
     $this->factory->addBeanClass(SecondImplementationOfInterface::class);
     $service10 = $this->factory->getBean(FirstImplementationOfInterface::class);
     $this->assertNotNull($service10);
     $this->assertInstanceOf(FirstImplementationOfInterface::class, $service10);
     $service11 = $this->factory->getBean('first');
     $this->assertNotNull($service11);
     $this->assertInstanceOf(FirstImplementationOfInterface::class, $service11);
     $service20 = $this->factory->getBean(SecondImplementationOfInterface::class);
     $this->assertNotNull($service20);
     $this->assertInstanceOf(SecondImplementationOfInterface::class, $service20);
     $service21 = $this->factory->getBean(SecondImplementationOfInterface::class);
     $this->assertNotNull($service21);
     $this->assertInstanceOf(SecondImplementationOfInterface::class, $service21);
     $this->assertTrue($service10 !== $service20);
     $this->assertTrue($service10 === $service11);
     $this->assertTrue($service11 !== $service21);
     $this->assertTrue($service20 === $service21);
     $this->factory->getBean(ComponentInterface::class);
 }
 private function setupParameterValue(ReflectionParameter $parameter)
 {
     $pos = $parameter->getPosition();
     if (in_array($pos, $this->handledParams, true)) {
         return;
     }
     $parameterName = $parameter->getName();
     $type = $this->getParameterType($parameter);
     if (array_key_exists($parameterName, $this->args)) {
         $this->invokeParams[$pos] = $this->args[$parameterName];
         return;
     }
     if (array_key_exists($pos, $this->args)) {
         $this->invokeParams[$pos] = $this->args[$pos];
         return;
     }
     if ($parameter->isOptional()) {
         $this->invokeParams[$pos] = $parameter->getDefaultValue();
     }
     if ($this->isPrimitive($type) || $type === null) {
         $this->handleRequestParam($parameter, $this->invokeParams);
     } elseif (!$this->isPrimitive($type) && $type) {
         $this->invokeParams[$pos] = BeanFactory::getInstance()->getBean($type);
     }
 }