public function __call($method, $arguments) { if (is_callable([$this->object, $method])) { $annotations = ReflectionHelper::get_method_annotations($this->object, $method); if (count($annotations)) { $this->handle_annotations($annotations); } return call_user_func_array([$this->object, $method], $arguments); } else { $msg = get_class($this->object) . " has no method {$method}"; throw new NotExistedMethodException($msg); } }
/** * @covers common\classes\Application::handle_annotations */ public function test_handle_annotations() { $obj = new AnnotationDecorated(); $annotations = ReflectionHelper::get_class_annotations($obj); $method = new ReflectionMethod(Application::class, 'handle_annotations'); $method->setAccessible(true); self::assertTrue($method->invoke(null, $obj, $annotations) instanceof AnnotationsDecorator); $obj = new NotDecorated(); $annotations = ReflectionHelper::get_class_annotations($obj); self::assertTrue($method->invoke(null, $obj, $annotations) instanceof NotDecorated); $obj = new DumbDecorated(); $annotations = ReflectionHelper::get_class_annotations($obj); self::assertTrue($method->invoke(null, $obj, $annotations) instanceof DumbDecorated); }
/** * @param $name * @param array $params * @return mixed * @throws \InvalidArgumentException */ public static function get_class($name, array $params = []) { if (class_exists($name)) { if (!array_key_exists($name, static::$instances)) { $reflection = new \ReflectionClass($name); static::$instances[$name] = $reflection->newInstanceArgs($params); } $annotations = ReflectionHelper::get_class_annotations(static::$instances[$name]); if (count($annotations)) { $obj = static::handle_annotations(static::$instances[$name], $annotations); static::$instances[$name] = $obj; } return static::$instances[$name]; } else { throw new \InvalidArgumentException("class {$name} does not exists"); } }
/** * @covers common\helpers\ReflectionHelper::get_method_annotations * @covers common\helpers\ReflectionHelper::parse_doc_comment */ public function test_get_method_annotations() { self::assertEquals(ReflectionHelper::get_method_annotations(Annotated::class, 'method'), [['name' => 'credentials', 'value' => 'admin']]); }
/** * @covers common\decorators\AnnotationsDecorator::handle_annotations * @expectedException \common\exceptions\WrongRequestMethodException */ public function test_handle_annotations_not_ajax() { $method = new ReflectionMethod($this->obj, 'handle_annotations'); $method->setAccessible(true); $annotations = ReflectionHelper::get_method_annotations('ObjToTestAnnotations', 'add_test'); /** * @var $ext User */ $ext = Application::get_class('User'); $mapper = $ext->get_mapper(); $user = $mapper->find_where(['credentials' => ['=', User::credentials_admin]])->one(); if (empty($user)) { $user = $mapper->find_where(['credentials' => ['=', User::credentials_super_admin]])->one(); } $_COOKIE['user'] = $user->remember_hash; $method->invoke($this->obj, $annotations); }