ObjectHelpers.strictness.phpt 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * Test: Nette\Utils\ObjectHelpers: strictness
  4. */
  5. declare(strict_types=1);
  6. use Nette\MemberAccessException;
  7. use Nette\Utils\ObjectHelpers;
  8. use Tester\Assert;
  9. require __DIR__ . '/../bootstrap.php';
  10. class TestClass
  11. {
  12. public $public;
  13. public static $publicStatic;
  14. protected $protected;
  15. public function publicMethod()
  16. {
  17. }
  18. public static function publicMethodStatic()
  19. {
  20. }
  21. protected function protectedMethod()
  22. {
  23. }
  24. protected static function protectedMethodS()
  25. {
  26. }
  27. }
  28. class TestChild extends TestClass
  29. {
  30. public function callParent()
  31. {
  32. parent::callParent();
  33. }
  34. }
  35. // calling
  36. Assert::exception(
  37. fn() => ObjectHelpers::strictCall('TestClass', 'undeclared'),
  38. MemberAccessException::class,
  39. 'Call to undefined method TestClass::undeclared().',
  40. );
  41. Assert::exception(
  42. fn() => ObjectHelpers::strictStaticCall('TestClass', 'undeclared'),
  43. MemberAccessException::class,
  44. 'Call to undefined static method TestClass::undeclared().',
  45. );
  46. Assert::exception(
  47. fn() => ObjectHelpers::strictCall('TestChild', 'callParent'),
  48. MemberAccessException::class,
  49. 'Call to method TestChild::callParent() from global scope.',
  50. );
  51. Assert::exception(
  52. fn() => ObjectHelpers::strictCall('TestClass', 'publicMethodX'),
  53. MemberAccessException::class,
  54. 'Call to undefined method TestClass::publicMethodX(), did you mean publicMethod()?',
  55. );
  56. Assert::exception(
  57. fn() => ObjectHelpers::strictCall('TestClass', 'publicMethodStaticX'),
  58. MemberAccessException::class,
  59. 'Call to undefined method TestClass::publicMethodStaticX(), did you mean publicMethodStatic()?',
  60. );
  61. Assert::exception(
  62. fn() => ObjectHelpers::strictStaticCall('TestClass', 'publicMethodStaticX'),
  63. MemberAccessException::class,
  64. 'Call to undefined static method TestClass::publicMethodStaticX(), did you mean publicMethodStatic()?',
  65. );
  66. Assert::exception(
  67. fn() => ObjectHelpers::strictCall('TestClass', 'protectedMethodX'),
  68. MemberAccessException::class,
  69. 'Call to undefined method TestClass::protectedMethodX().',
  70. );
  71. // writing
  72. Assert::exception(
  73. fn() => ObjectHelpers::strictSet('TestClass', 'undeclared'),
  74. MemberAccessException::class,
  75. 'Cannot write to an undeclared property TestClass::$undeclared.',
  76. );
  77. Assert::exception(
  78. fn() => ObjectHelpers::strictSet('TestClass', 'publicX'),
  79. MemberAccessException::class,
  80. 'Cannot write to an undeclared property TestClass::$publicX, did you mean $public?',
  81. );
  82. Assert::exception(
  83. fn() => ObjectHelpers::strictSet('TestClass', 'publicStaticX'),
  84. MemberAccessException::class,
  85. 'Cannot write to an undeclared property TestClass::$publicStaticX.',
  86. );
  87. Assert::exception(
  88. fn() => ObjectHelpers::strictSet('TestClass', 'protectedX'),
  89. MemberAccessException::class,
  90. 'Cannot write to an undeclared property TestClass::$protectedX.',
  91. );
  92. // reading
  93. Assert::exception(
  94. fn() => ObjectHelpers::strictGet('TestClass', 'undeclared'),
  95. MemberAccessException::class,
  96. 'Cannot read an undeclared property TestClass::$undeclared.',
  97. );
  98. Assert::exception(
  99. fn() => ObjectHelpers::strictGet('TestClass', 'publicX'),
  100. MemberAccessException::class,
  101. 'Cannot read an undeclared property TestClass::$publicX, did you mean $public?',
  102. );
  103. Assert::exception(
  104. fn() => ObjectHelpers::strictGet('TestClass', 'publicStaticX'),
  105. MemberAccessException::class,
  106. 'Cannot read an undeclared property TestClass::$publicStaticX.',
  107. );
  108. Assert::exception(
  109. fn() => ObjectHelpers::strictGet('TestClass', 'protectedX'),
  110. MemberAccessException::class,
  111. 'Cannot read an undeclared property TestClass::$protectedX.',
  112. );