1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
<?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.',
);
|