/** * @dataProvider providerCast * * @param string $method * @param mixed $result * @param array $arg * * @throws Error\CallableNotFoundException * @throws \Exception */ public function testCast($method, $result, $arg = null) { if (strpos($method, "Hint") && PHP_VERSION_ID < 70000) { $this->markTestSkipped("PHP7 required"); } $method = MethodInfo::scan('Koda\\Samples', $method); if (func_num_args() > 2) { $args = [$arg]; } else { $args = []; } try { $this->assertSame($result, $method->invoke($args)); } catch (InvalidArgumentException $e) { if ($result === null) { return; } else { throw $e; } } if (func_num_args() > 2) { $args = ["val" => $arg]; } else { $args = []; } $this->assertEquals($result, $method->invoke($args)); }
public static function getMethodInfo($cb) { if (is_array($cb)) { return Koda\MethodInfo::scan($cb[0], $cb[1]); } elseif (is_object($cb)) { if ($cb instanceof Closure) { return FunctionInfo::scan($cb); } else { return MethodInfo::scan($cb, "__invoke"); } } else { if (strpos($cb, '::')) { $cb = explode('::', $cb); return MethodInfo::scan($cb[0], $cb[1]); } else { return FunctionInfo::scan($cb); } } }
/** * @param $class_name * @param array $args * @param array $options * * @return mixed * @throws \Koda\Error\CallableNotFoundException * @throws \Koda\Error\InvalidArgumentException */ public static function object($class_name, array $args = [], array $options = []) { if (method_exists($class_name, "__construct")) { $info = Koda\MethodInfo::scan($class_name, "__construct"); if ($info->hasArguments()) { $filter = self::getFilter([$class_name, "__construct"], isset($options['context']) ? $options['context'] : \Koda\Filter::class); if (isset($options["factory"])) { $filter->setFactory($options["factory"]); } if (isset($options["injector"])) { $filter->setInjector($options["injector"]); } $args = $info->filterArgs($args, $filter); return new $class_name(...$args); } else { return new $class_name(); } } else { return new $class_name(); } }
/** * @dataProvider providerInvokeMulti * * @param $nums * @param $result * * @throws \Koda\Error\InvalidArgumentException */ public function testInvokeMulti($nums, $result) { $method = MethodInfo::scan('Koda\\Math', 'avg'); $this->assertEquals($result, $method->invoke(["nums" => $nums])); }