| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- <?php
- /**
- * Test: Nette\Utils\ObjectHelpers: strictness
- */
- declare(strict_types=1);
- use Nette\MemberAccessException;
- use Nette\Utils\ObjectHelpers;
- use Tester\Assert;
- require __DIR__ . '/../bootstrap.php';
- class TestClass
- {
- public $public;
- public static $publicStatic;
- protected $protected;
- public function publicMethod()
- {
- }
- public static function publicMethodStatic()
- {
- }
- protected function protectedMethod()
- {
- }
- protected static function protectedMethodS()
- {
- }
- }
- class TestChild extends TestClass
- {
- public function callParent()
- {
- parent::callParent();
- }
- }
- // calling
- Assert::exception(
- fn() => ObjectHelpers::strictCall('TestClass', 'undeclared'),
- MemberAccessException::class,
- 'Call to undefined method TestClass::undeclared().',
- );
- Assert::exception(
- fn() => ObjectHelpers::strictStaticCall('TestClass', 'undeclared'),
- MemberAccessException::class,
- 'Call to undefined static method TestClass::undeclared().',
- );
- Assert::exception(
- fn() => ObjectHelpers::strictCall('TestChild', 'callParent'),
- MemberAccessException::class,
- 'Call to method TestChild::callParent() from global scope.',
- );
- Assert::exception(
- fn() => ObjectHelpers::strictCall('TestClass', 'publicMethodX'),
- MemberAccessException::class,
- 'Call to undefined method TestClass::publicMethodX(), did you mean publicMethod()?',
- );
- Assert::exception(
- fn() => ObjectHelpers::strictCall('TestClass', 'publicMethodStaticX'),
- MemberAccessException::class,
- 'Call to undefined method TestClass::publicMethodStaticX(), did you mean publicMethodStatic()?',
- );
- Assert::exception(
- fn() => ObjectHelpers::strictStaticCall('TestClass', 'publicMethodStaticX'),
- MemberAccessException::class,
- 'Call to undefined static method TestClass::publicMethodStaticX(), did you mean publicMethodStatic()?',
- );
- Assert::exception(
- fn() => ObjectHelpers::strictCall('TestClass', 'protectedMethodX'),
- MemberAccessException::class,
- 'Call to undefined method TestClass::protectedMethodX().',
- );
- // writing
- Assert::exception(
- fn() => ObjectHelpers::strictSet('TestClass', 'undeclared'),
- MemberAccessException::class,
- 'Cannot write to an undeclared property TestClass::$undeclared.',
- );
- Assert::exception(
- fn() => ObjectHelpers::strictSet('TestClass', 'publicX'),
- MemberAccessException::class,
- 'Cannot write to an undeclared property TestClass::$publicX, did you mean $public?',
- );
- Assert::exception(
- fn() => ObjectHelpers::strictSet('TestClass', 'publicStaticX'),
- MemberAccessException::class,
- 'Cannot write to an undeclared property TestClass::$publicStaticX.',
- );
- Assert::exception(
- fn() => ObjectHelpers::strictSet('TestClass', 'protectedX'),
- MemberAccessException::class,
- 'Cannot write to an undeclared property TestClass::$protectedX.',
- );
- // reading
- Assert::exception(
- fn() => ObjectHelpers::strictGet('TestClass', 'undeclared'),
- MemberAccessException::class,
- 'Cannot read an undeclared property TestClass::$undeclared.',
- );
- Assert::exception(
- fn() => ObjectHelpers::strictGet('TestClass', 'publicX'),
- MemberAccessException::class,
- 'Cannot read an undeclared property TestClass::$publicX, did you mean $public?',
- );
- Assert::exception(
- fn() => ObjectHelpers::strictGet('TestClass', 'publicStaticX'),
- MemberAccessException::class,
- 'Cannot read an undeclared property TestClass::$publicStaticX.',
- );
- Assert::exception(
- fn() => ObjectHelpers::strictGet('TestClass', 'protectedX'),
- MemberAccessException::class,
- 'Cannot read an undeclared property TestClass::$protectedX.',
- );
|