|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Test: ComponentReflection::combineArgs() |
| 5 | + * @phpVersion 7.1 |
| 6 | + */ |
| 7 | + |
| 8 | +use Nette\Application\UI\ComponentReflection as Reflection; |
| 9 | +use Nette\Application\BadRequestException; |
| 10 | +use Tester\Assert; |
| 11 | + |
| 12 | +require __DIR__ . '/../bootstrap.php'; |
| 13 | + |
| 14 | + |
| 15 | +class MyPresenter |
| 16 | +{ |
| 17 | + |
| 18 | + public function params($int, $bool, $str, $arr) |
| 19 | + { |
| 20 | + } |
| 21 | + |
| 22 | + public function hints(int $int, bool $bool, string $str, array $arr) |
| 23 | + { |
| 24 | + } |
| 25 | + |
| 26 | + public function hintsNulls(?int $int, ?bool $bool, ?string $str, ?array $arr) |
| 27 | + { |
| 28 | + } |
| 29 | + |
| 30 | + public function hintsDefaults(int $int = 0, bool $bool = FALSE, string $str = '', array $arr = []) |
| 31 | + { |
| 32 | + } |
| 33 | + |
| 34 | + public function defaults($int = 0, $bool = FALSE, $str = '', $arr = []) |
| 35 | + { |
| 36 | + } |
| 37 | + |
| 38 | + public function objects(stdClass $req, ?stdClass $opt) |
| 39 | + { |
| 40 | + } |
| 41 | + |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | +test(function () { |
| 46 | + $method = new ReflectionMethod('MyPresenter', 'params'); |
| 47 | + |
| 48 | + Assert::same([NULL, NULL, NULL, NULL], Reflection::combineArgs($method, [])); |
| 49 | + Assert::same([NULL, NULL, NULL, NULL], Reflection::combineArgs($method, ['int' => NULL, 'bool' => NULL, 'str' => NULL, 'arr' => NULL])); |
| 50 | + Assert::same([1, TRUE, 'abc', '1'], Reflection::combineArgs($method, ['int' => 1, 'bool' => TRUE, 'str' => 'abc', 'arr' => '1'])); |
| 51 | + Assert::same([0, FALSE, '', ''], Reflection::combineArgs($method, ['int' => 0, 'bool' => FALSE, 'str' => '', 'arr' => ''])); |
| 52 | + Assert::equal([NULL, NULL, NULL, new stdClass], Reflection::combineArgs($method, ['arr' => new stdClass])); |
| 53 | + |
| 54 | + Assert::exception(function () use ($method) { |
| 55 | + Reflection::combineArgs($method, ['int' => []]); |
| 56 | + }, BadRequestException::class, 'Argument $int passed to MyPresenter::params() must be scalar, array given.'); |
| 57 | +}); |
| 58 | + |
| 59 | + |
| 60 | +test(function () { |
| 61 | + $method = new ReflectionMethod('MyPresenter', 'hints'); |
| 62 | + |
| 63 | + Assert::same([1, TRUE, 'abc', [1]], Reflection::combineArgs($method, ['int' => '1', 'bool' => '1', 'str' => 'abc', 'arr' => [1]])); |
| 64 | + Assert::same([0, FALSE, '', []], Reflection::combineArgs($method, ['int' => 0, 'bool' => FALSE, 'str' => ''])); // missing 'arr' |
| 65 | + |
| 66 | + Assert::exception(function () use ($method) { |
| 67 | + Reflection::combineArgs($method, []); |
| 68 | + }, BadRequestException::class, 'Missing parameter $int required by MyPresenter::hints()'); |
| 69 | + |
| 70 | + Assert::exception(function () use ($method) { |
| 71 | + Reflection::combineArgs($method, ['int' => '']); |
| 72 | + }, BadRequestException::class, 'Argument $int passed to MyPresenter::hints() must be int, string given.'); |
| 73 | + |
| 74 | + Assert::exception(function () use ($method) { |
| 75 | + Reflection::combineArgs($method, ['int' => NULL]); |
| 76 | + }, BadRequestException::class, 'Missing parameter $int required by MyPresenter::hints()'); |
| 77 | + |
| 78 | + Assert::exception(function () use ($method) { |
| 79 | + Reflection::combineArgs($method, ['int' => new stdClass]); |
| 80 | + }, BadRequestException::class, 'Argument $int passed to MyPresenter::hints() must be int, stdClass given.'); |
| 81 | + |
| 82 | + Assert::exception(function () use ($method) { |
| 83 | + Reflection::combineArgs($method, ['int' => []]); |
| 84 | + }, BadRequestException::class, 'Argument $int passed to MyPresenter::hints() must be int, array given.'); |
| 85 | + |
| 86 | + Assert::exception(function () use ($method) { |
| 87 | + Reflection::combineArgs($method, ['int' => '1', 'bool' => '']); |
| 88 | + }, BadRequestException::class, 'Argument $bool passed to MyPresenter::hints() must be bool, string given.'); |
| 89 | + |
| 90 | + Assert::exception(function () use ($method) { |
| 91 | + Reflection::combineArgs($method, ['int' => '1', 'bool' => '1', 'str' => '', 'arr' => '']); |
| 92 | + }, BadRequestException::class, 'Argument $arr passed to MyPresenter::hints() must be array, string given.'); |
| 93 | +}); |
| 94 | + |
| 95 | + |
| 96 | +test(function () { |
| 97 | + $method = new ReflectionMethod('MyPresenter', 'hintsNulls'); |
| 98 | + |
| 99 | + Assert::same([NULL, NULL, NULL, NULL], Reflection::combineArgs($method, [])); |
| 100 | + Assert::same([NULL, NULL, NULL, NULL], Reflection::combineArgs($method, ['int' => NULL, 'bool' => NULL, 'str' => NULL, 'arr' => NULL])); |
| 101 | + Assert::same([1, TRUE, 'abc', [1]], Reflection::combineArgs($method, ['int' => '1', 'bool' => '1', 'str' => 'abc', 'arr' => [1]])); |
| 102 | + Assert::same([0, FALSE, '', []], Reflection::combineArgs($method, ['int' => 0, 'bool' => FALSE, 'str' => '', 'arr' => []])); |
| 103 | + |
| 104 | + Assert::exception(function () use ($method) { |
| 105 | + Reflection::combineArgs($method, ['int' => '']); |
| 106 | + }, BadRequestException::class, 'Argument $int passed to MyPresenter::hintsNulls() must be int, string given.'); |
| 107 | + |
| 108 | + Assert::exception(function () use ($method) { |
| 109 | + Reflection::combineArgs($method, ['int' => new stdClass]); |
| 110 | + }, BadRequestException::class, 'Argument $int passed to MyPresenter::hintsNulls() must be int, stdClass given.'); |
| 111 | + |
| 112 | + Assert::exception(function () use ($method) { |
| 113 | + Reflection::combineArgs($method, ['int' => []]); |
| 114 | + }, BadRequestException::class, 'Argument $int passed to MyPresenter::hintsNulls() must be int, array given.'); |
| 115 | + |
| 116 | + Assert::exception(function () use ($method) { |
| 117 | + Reflection::combineArgs($method, ['int' => '1', 'bool' => '']); |
| 118 | + }, BadRequestException::class, 'Argument $bool passed to MyPresenter::hintsNulls() must be bool, string given.'); |
| 119 | + |
| 120 | + Assert::exception(function () use ($method) { |
| 121 | + Reflection::combineArgs($method, ['int' => '1', 'bool' => '1', 'str' => '', 'arr' => '']); |
| 122 | + }, BadRequestException::class, 'Argument $arr passed to MyPresenter::hintsNulls() must be array, string given.'); |
| 123 | +}); |
| 124 | + |
| 125 | + |
| 126 | +test(function () { |
| 127 | + $method = new ReflectionMethod('MyPresenter', 'hintsDefaults'); |
| 128 | + |
| 129 | + Assert::same([0, FALSE, '', []], Reflection::combineArgs($method, [])); |
| 130 | + Assert::same([0, FALSE, '', []], Reflection::combineArgs($method, ['int' => NULL, 'bool' => NULL, 'str' => NULL, 'arr' => NULL])); |
| 131 | + Assert::same([1, TRUE, 'abc', [1]], Reflection::combineArgs($method, ['int' => '1', 'bool' => '1', 'str' => 'abc', 'arr' => [1]])); |
| 132 | + Assert::same([0, FALSE, '', []], Reflection::combineArgs($method, ['int' => 0, 'bool' => FALSE, 'str' => '', 'arr' => []])); |
| 133 | + |
| 134 | + Assert::exception(function () use ($method) { |
| 135 | + Reflection::combineArgs($method, ['int' => '']); |
| 136 | + }, BadRequestException::class, 'Argument $int passed to MyPresenter::hintsDefaults() must be int, string given.'); |
| 137 | + |
| 138 | + Assert::exception(function () use ($method) { |
| 139 | + Reflection::combineArgs($method, ['int' => new stdClass]); |
| 140 | + }, BadRequestException::class, 'Argument $int passed to MyPresenter::hintsDefaults() must be int, stdClass given.'); |
| 141 | + |
| 142 | + Assert::exception(function () use ($method) { |
| 143 | + Reflection::combineArgs($method, ['int' => []]); |
| 144 | + }, BadRequestException::class, 'Argument $int passed to MyPresenter::hintsDefaults() must be int, array given.'); |
| 145 | + |
| 146 | + Assert::exception(function () use ($method) { |
| 147 | + Reflection::combineArgs($method, ['int' => '1', 'bool' => '']); |
| 148 | + }, BadRequestException::class, 'Argument $bool passed to MyPresenter::hintsDefaults() must be bool, string given.'); |
| 149 | + |
| 150 | + Assert::exception(function () use ($method) { |
| 151 | + Reflection::combineArgs($method, ['int' => '1', 'bool' => '1', 'str' => '', 'arr' => '']); |
| 152 | + }, BadRequestException::class, 'Argument $arr passed to MyPresenter::hintsDefaults() must be array, string given.'); |
| 153 | +}); |
| 154 | + |
| 155 | + |
| 156 | +test(function () { |
| 157 | + $method = new ReflectionMethod('MyPresenter', 'defaults'); |
| 158 | + |
| 159 | + Assert::same([0, FALSE, '', []], Reflection::combineArgs($method, [])); |
| 160 | + Assert::same([0, FALSE, '', []], Reflection::combineArgs($method, ['int' => NULL, 'bool' => NULL, 'str' => NULL, 'arr' => NULL])); |
| 161 | + Assert::same([1, TRUE, 'abc', [1]], Reflection::combineArgs($method, ['int' => '1', 'bool' => '1', 'str' => 'abc', 'arr' => [1]])); |
| 162 | + Assert::same([0, FALSE, '', []], Reflection::combineArgs($method, ['int' => 0, 'bool' => FALSE, 'str' => '', 'arr' => []])); |
| 163 | + |
| 164 | + Assert::exception(function () use ($method) { |
| 165 | + Reflection::combineArgs($method, ['int' => '']); |
| 166 | + }, BadRequestException::class, 'Argument $int passed to MyPresenter::defaults() must be integer, string given.'); |
| 167 | + |
| 168 | + Assert::exception(function () use ($method) { |
| 169 | + Reflection::combineArgs($method, ['int' => new stdClass]); |
| 170 | + }, BadRequestException::class, 'Argument $int passed to MyPresenter::defaults() must be integer, stdClass given.'); |
| 171 | + |
| 172 | + Assert::exception(function () use ($method) { |
| 173 | + Reflection::combineArgs($method, ['int' => []]); |
| 174 | + }, BadRequestException::class, 'Argument $int passed to MyPresenter::defaults() must be integer, array given.'); |
| 175 | + |
| 176 | + Assert::exception(function () use ($method) { |
| 177 | + Reflection::combineArgs($method, ['int' => '1', 'bool' => '']); |
| 178 | + }, BadRequestException::class, 'Argument $bool passed to MyPresenter::defaults() must be boolean, string given.'); |
| 179 | + |
| 180 | + Assert::exception(function () use ($method) { |
| 181 | + Reflection::combineArgs($method, ['int' => '1', 'bool' => '1', 'str' => '', 'arr' => '']); |
| 182 | + }, BadRequestException::class, 'Argument $arr passed to MyPresenter::defaults() must be array, string given.'); |
| 183 | +}); |
| 184 | + |
| 185 | + |
| 186 | +test(function () { |
| 187 | + $method = new ReflectionMethod('MyPresenter', 'objects'); |
| 188 | + |
| 189 | + Assert::equal([new stdClass, new stdClass], Reflection::combineArgs($method, ['req' => new stdClass, 'opt' => new stdClass])); |
| 190 | + |
| 191 | + Assert::exception(function () use ($method) { |
| 192 | + Reflection::combineArgs($method, []); |
| 193 | + }, Nette\InvalidArgumentException::class, 'Missing parameter $req required by MyPresenter::objects()'); |
| 194 | + |
| 195 | + Assert::exception(function () use ($method) { |
| 196 | + Reflection::combineArgs($method, ['req' => NULL, 'opt' => NULL]); |
| 197 | + }, Nette\InvalidArgumentException::class, 'Missing parameter $req required by MyPresenter::objects()'); |
| 198 | + |
| 199 | + Assert::exception(function () use ($method) { |
| 200 | + Reflection::combineArgs($method, ['req' => $method, 'opt' => NULL]); |
| 201 | + }, Nette\InvalidArgumentException::class, 'Argument $req passed to MyPresenter::objects() must be stdClass, ReflectionMethod given.'); |
| 202 | + |
| 203 | + Assert::exception(function () use ($method) { |
| 204 | + Reflection::combineArgs($method, ['req' => [], 'opt' => NULL]); |
| 205 | + }, Nette\InvalidArgumentException::class, 'Argument $req passed to MyPresenter::objects() must be stdClass, array given.'); |
| 206 | +}); |
0 commit comments