Reflection.getParameterDefaultValue.phpt 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * Test: Nette\Utils\Reflection::getParameterDefaultValue()
  4. */
  5. declare(strict_types=1);
  6. use Nette\Utils\Reflection;
  7. use Tester\Assert;
  8. require __DIR__ . '/../bootstrap.php';
  9. require __DIR__ . '/fixtures.reflection/defaultValue.php';
  10. Assert::exception(
  11. fn() => Reflection::getParameterDefaultValue(new ReflectionParameter(['NS\Foo', 'method'], 'a')),
  12. ReflectionException::class,
  13. );
  14. Assert::same('abc', Reflection::getParameterDefaultValue(new ReflectionParameter(['NS\Foo', 'method'], 'b')));
  15. Assert::same('abc', Reflection::getParameterDefaultValue(new ReflectionParameter(['NS\Foo', 'method'], 'c')));
  16. Assert::same('abc', Reflection::getParameterDefaultValue(new ReflectionParameter(['NS\Foo', 'method'], 'd')));
  17. Assert::same('abc', Reflection::getParameterDefaultValue(new ReflectionParameter(['NS\Foo', 'method'], 'e')));
  18. Assert::same('abc', Reflection::getParameterDefaultValue(new ReflectionParameter(['NS\Foo', 'method'], 'f')));
  19. Assert::same('abc', Reflection::getParameterDefaultValue(new ReflectionParameter(['NS\Foo', 'method'], 'g')));
  20. Assert::same('abc', Reflection::getParameterDefaultValue(new ReflectionParameter(['NS\Foo', 'method'], 'h')));
  21. Assert::same('xyz', Reflection::getParameterDefaultValue(new ReflectionParameter(['NS\Foo', 'method'], 'p')));
  22. Assert::exception(
  23. fn() => Reflection::getParameterDefaultValue(new ReflectionParameter(['NS\Foo', 'method'], 'i')),
  24. ReflectionException::class,
  25. 'Unable to resolve constant self::UNDEFINED used as default value of $i in NS\Foo::method().',
  26. );
  27. Assert::exception(
  28. fn() => Reflection::getParameterDefaultValue(new ReflectionParameter(['NS\Foo', 'method'], 'j')),
  29. ReflectionException::class,
  30. 'Unable to resolve constant NS\\Foo::UNDEFINED used as default value of $j in NS\Foo::method().',
  31. );
  32. Assert::same(NS\Bar::DEFINED, Reflection::getParameterDefaultValue(new ReflectionParameter(['NS\Foo', 'method'], 'k')));
  33. Assert::exception(
  34. fn() => Reflection::getParameterDefaultValue(new ReflectionParameter(['NS\Foo', 'method'], 'l')),
  35. ReflectionException::class,
  36. 'Unable to resolve constant NS\Undefined::ANY used as default value of $l in NS\Foo::method().',
  37. );
  38. Assert::same(DEFINED, Reflection::getParameterDefaultValue(new ReflectionParameter(['NS\Foo', 'method'], 'm')));
  39. Assert::exception(
  40. fn() => Reflection::getParameterDefaultValue(new ReflectionParameter(['NS\Foo', 'method'], 'n')),
  41. ReflectionException::class,
  42. 'Unable to resolve constant NS\UNDEFINED used as default value of $n in NS\Foo::method().',
  43. );
  44. Assert::same(NS\NS_DEFINED, Reflection::getParameterDefaultValue(new ReflectionParameter(['NS\Foo', 'method'], 'o')));