|
| 1 | +<?php |
| 2 | + |
| 3 | +use App\Exceptions\BadRequestException; |
| 4 | +use App\Exceptions\InternalServerException; |
| 5 | +use App\Exceptions\InvalidApiArgumentException; |
| 6 | +use App\Helpers\MetaFormats\Attributes\Format; |
| 7 | +use App\Helpers\MetaFormats\Attributes\FPath; |
| 8 | +use App\Helpers\MetaFormats\Attributes\FPost; |
| 9 | +use App\Helpers\MetaFormats\Attributes\FQuery; |
| 10 | +use App\Helpers\MetaFormats\Attributes\Path; |
| 11 | +use App\Helpers\MetaFormats\Attributes\Post; |
| 12 | +use App\Helpers\MetaFormats\Attributes\Query; |
| 13 | +use App\Helpers\MetaFormats\FormatCache; |
| 14 | +use App\Helpers\MetaFormats\FormatDefinitions\UserFormat; |
| 15 | +use App\Helpers\MetaFormats\MetaFormat; |
| 16 | +use App\Helpers\MetaFormats\MetaFormatHelper; |
| 17 | +use App\Helpers\MetaFormats\Validators\BaseValidator; |
| 18 | +use App\Helpers\MetaFormats\Validators\VArray; |
| 19 | +use App\Helpers\MetaFormats\Validators\VBool; |
| 20 | +use App\Helpers\MetaFormats\Validators\VDouble; |
| 21 | +use App\Helpers\MetaFormats\Validators\VInt; |
| 22 | +use App\Helpers\MetaFormats\Validators\VMixed; |
| 23 | +use App\Helpers\MetaFormats\Validators\VObject; |
| 24 | +use App\Helpers\MetaFormats\Validators\VString; |
| 25 | +use App\Helpers\MetaFormats\Validators\VTimestamp; |
| 26 | +use App\Helpers\MetaFormats\Validators\VUuid; |
| 27 | +use App\V1Module\Presenters\BasePresenter; |
| 28 | +use Nette\Application\Request; |
| 29 | +use Tester\Assert; |
| 30 | + |
| 31 | +$container = require_once __DIR__ . "/../bootstrap.php"; |
| 32 | + |
| 33 | +#[Format(ValidationTestFormat::class)] |
| 34 | +class PresenterTestFormat extends MetaFormat |
| 35 | +{ |
| 36 | + #[FPost(new VInt())] |
| 37 | + public ?int $post; |
| 38 | + |
| 39 | + #[FPath(new VInt())] |
| 40 | + public ?int $path; |
| 41 | + |
| 42 | + #[FQuery(new VInt())] |
| 43 | + public ?int $query; |
| 44 | + |
| 45 | + public function validateStructure() |
| 46 | + { |
| 47 | + return $this->query == 1; |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +class TestPresenter extends BasePresenter |
| 52 | +{ |
| 53 | + #[Post("post", new VInt())] |
| 54 | + #[Query("query", new VInt())] |
| 55 | + #[Path("path", new VInt())] |
| 56 | + public function actionTestLoose() |
| 57 | + { |
| 58 | + } |
| 59 | + |
| 60 | + #[Format(PresenterTestFormat::class)] |
| 61 | + public function actionTestFormat() |
| 62 | + { |
| 63 | + } |
| 64 | + |
| 65 | + #[Format(PresenterTestFormat::class)] |
| 66 | + #[Post("loose", new VInt())] |
| 67 | + public function actionTestCombined() |
| 68 | + { |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * @testCase |
| 74 | + */ |
| 75 | +class TestBasePresenter extends Tester\TestCase |
| 76 | +{ |
| 77 | + /** @var Nette\DI\Container */ |
| 78 | + protected $container; |
| 79 | + |
| 80 | + public function __construct() |
| 81 | + { |
| 82 | + global $container; |
| 83 | + $this->container = $container; |
| 84 | + } |
| 85 | + |
| 86 | + private static function injectFormat(string $format) |
| 87 | + { |
| 88 | + // initialize the cache |
| 89 | + FormatCache::getFormatToFieldDefinitionsMap(); |
| 90 | + FormatCache::getFormatNamesHashSet(); |
| 91 | + |
| 92 | + // inject the format name |
| 93 | + $hashSetReflector = new ReflectionProperty(FormatCache::class, "formatNamesHashSet"); |
| 94 | + $hashSetReflector->setAccessible(true); |
| 95 | + $formatNamesHashSet = $hashSetReflector->getValue(); |
| 96 | + $formatNamesHashSet[$format] = true; |
| 97 | + $hashSetReflector->setValue(null, $formatNamesHashSet); |
| 98 | + |
| 99 | + // inject the format definitions |
| 100 | + $formatMapReflector = new ReflectionProperty(FormatCache::class, "formatToFieldFormatsMap"); |
| 101 | + $formatMapReflector->setAccessible(true); |
| 102 | + $formatToFieldFormatsMap = $formatMapReflector->getValue(); |
| 103 | + $formatToFieldFormatsMap[$format] = MetaFormatHelper::createNameToFieldDefinitionsMap($format); |
| 104 | + $formatMapReflector->setValue(null, $formatToFieldFormatsMap); |
| 105 | + |
| 106 | + Assert::notNull(FormatCache::getFieldDefinitions($format), "Tests whether a format was injected successfully."); |
| 107 | + } |
| 108 | + |
| 109 | + private static function getMethod(BasePresenter $presenter, string $methodName): ReflectionMethod |
| 110 | + { |
| 111 | + $presenterReflection = new ReflectionObject($presenter); |
| 112 | + $methodReflection = $presenterReflection->getMethod($methodName); |
| 113 | + $methodReflection->setAccessible(true); |
| 114 | + return $methodReflection; |
| 115 | + } |
| 116 | + |
| 117 | + public function testLooseValid() |
| 118 | + { |
| 119 | + $presenter = new TestPresenter(); |
| 120 | + $action = self::getMethod($presenter, "actionTestLoose"); |
| 121 | + $processParams = self::getMethod($presenter, "processParams"); |
| 122 | + |
| 123 | + // create a request object and invoke the actionTestLoose method |
| 124 | + $request = new Request("name", method: "POST", params: ["path" => "1", "query" => "1"], post: ["post" => 1]); |
| 125 | + $processParams->invoke($presenter, $request, $action); |
| 126 | + |
| 127 | + // check that the previous row did not throw |
| 128 | + Assert::true(true); |
| 129 | + } |
| 130 | + |
| 131 | + public function testLooseInvalid() |
| 132 | + { |
| 133 | + $presenter = new TestPresenter(); |
| 134 | + $action = self::getMethod($presenter, "actionTestLoose"); |
| 135 | + $processParams = self::getMethod($presenter, "processParams"); |
| 136 | + |
| 137 | + // set an invalid parameter value and assert that the validation fails |
| 138 | + $request = new Request( |
| 139 | + "name", |
| 140 | + method: "POST", |
| 141 | + params: ["path" => "string", "query" => "1"], |
| 142 | + post: ["post" => 1] |
| 143 | + ); |
| 144 | + Assert::throws( |
| 145 | + function () use ($processParams, $presenter, $request, $action) { |
| 146 | + $processParams->invoke($presenter, $request, $action); |
| 147 | + }, |
| 148 | + InvalidApiArgumentException::class |
| 149 | + ); |
| 150 | + } |
| 151 | + |
| 152 | + public function testFormatValid() |
| 153 | + { |
| 154 | + self::injectFormat(PresenterTestFormat::class); |
| 155 | + $presenter = new TestPresenter(); |
| 156 | + $action = self::getMethod($presenter, "actionTestFormat"); |
| 157 | + $processParams = self::getMethod($presenter, "processParams"); |
| 158 | + |
| 159 | + // create a valid request object |
| 160 | + $request = new Request("name", method: "POST", params: ["path" => "1", "query" => "1"], post: ["post" => 1]); |
| 161 | + $processParams->invoke($presenter, $request, $action); |
| 162 | + |
| 163 | + // the presenter should automatically create a valid format object |
| 164 | + /** @var PresenterTestFormat */ |
| 165 | + $format = $presenter->getFormatInstance(); |
| 166 | + Assert::notNull($format); |
| 167 | + $format->validate(); |
| 168 | + |
| 169 | + // check if the values match |
| 170 | + Assert::equal($format->path, 1); |
| 171 | + Assert::equal($format->query, 1); |
| 172 | + Assert::equal($format->post, 1); |
| 173 | + } |
| 174 | + |
| 175 | + public function testFormatInvalidField() |
| 176 | + { |
| 177 | + self::injectFormat(PresenterTestFormat::class); |
| 178 | + $presenter = new TestPresenter(); |
| 179 | + $action = self::getMethod($presenter, "actionTestFormat"); |
| 180 | + $processParams = self::getMethod($presenter, "processParams"); |
| 181 | + |
| 182 | + // create a request object with invalid fields |
| 183 | + $request = new Request( |
| 184 | + "name", |
| 185 | + method: "POST", |
| 186 | + params: ["path" => "string", "query" => "1"], |
| 187 | + post: ["post" => 1] |
| 188 | + ); |
| 189 | + Assert::throws( |
| 190 | + function () use ($processParams, $presenter, $request, $action) { |
| 191 | + $processParams->invoke($presenter, $request, $action); |
| 192 | + }, |
| 193 | + InvalidApiArgumentException::class |
| 194 | + ); |
| 195 | + } |
| 196 | + |
| 197 | + public function testFormatInvalidStructure() |
| 198 | + { |
| 199 | + self::injectFormat(PresenterTestFormat::class); |
| 200 | + $presenter = new TestPresenter(); |
| 201 | + $action = self::getMethod($presenter, "actionTestFormat"); |
| 202 | + $processParams = self::getMethod($presenter, "processParams"); |
| 203 | + |
| 204 | + // create a request object with invalid structure |
| 205 | + $request = new Request("name", method: "POST", params: ["path" => "1", "query" => "0"], post: ["post" => 1]); |
| 206 | + Assert::throws( |
| 207 | + function () use ($processParams, $presenter, $request, $action) { |
| 208 | + $processParams->invoke($presenter, $request, $action); |
| 209 | + }, |
| 210 | + BadRequestException::class |
| 211 | + ); |
| 212 | + } |
| 213 | + |
| 214 | + public function testCombinedValid() |
| 215 | + { |
| 216 | + self::injectFormat(PresenterTestFormat::class); |
| 217 | + $presenter = new TestPresenter(); |
| 218 | + $action = self::getMethod($presenter, "actionTestCombined"); |
| 219 | + $processParams = self::getMethod($presenter, "processParams"); |
| 220 | + |
| 221 | + // create a valid request object |
| 222 | + $request = new Request( |
| 223 | + "name", |
| 224 | + method: "POST", |
| 225 | + params: ["path" => "1", "query" => "1"], |
| 226 | + post: ["post" => 1, "loose" => 1] |
| 227 | + ); |
| 228 | + $processParams->invoke($presenter, $request, $action); |
| 229 | + |
| 230 | + // the presenter should automatically create a valid format object |
| 231 | + /** @var PresenterTestFormat */ |
| 232 | + $format = $presenter->getFormatInstance(); |
| 233 | + Assert::notNull($format); |
| 234 | + $format->validate(); |
| 235 | + |
| 236 | + // check if the values match |
| 237 | + Assert::equal($format->path, 1); |
| 238 | + Assert::equal($format->query, 1); |
| 239 | + Assert::equal($format->post, 1); |
| 240 | + } |
| 241 | + |
| 242 | + public function testCombinedInvalidFormatFields() |
| 243 | + { |
| 244 | + self::injectFormat(PresenterTestFormat::class); |
| 245 | + $presenter = new TestPresenter(); |
| 246 | + $action = self::getMethod($presenter, "actionTestCombined"); |
| 247 | + $processParams = self::getMethod($presenter, "processParams"); |
| 248 | + |
| 249 | + // create a request object with invalid fields |
| 250 | + $request = new Request( |
| 251 | + "name", |
| 252 | + method: "POST", |
| 253 | + params: ["path" => "string", "query" => "1"], |
| 254 | + post: ["post" => 1, "loose" => 1] |
| 255 | + ); |
| 256 | + Assert::throws( |
| 257 | + function () use ($processParams, $presenter, $request, $action) { |
| 258 | + $processParams->invoke($presenter, $request, $action); |
| 259 | + }, |
| 260 | + InvalidApiArgumentException::class |
| 261 | + ); |
| 262 | + } |
| 263 | + |
| 264 | + public function testCombinedInvalidStructure() |
| 265 | + { |
| 266 | + self::injectFormat(PresenterTestFormat::class); |
| 267 | + $presenter = new TestPresenter(); |
| 268 | + $action = self::getMethod($presenter, "actionTestCombined"); |
| 269 | + $processParams = self::getMethod($presenter, "processParams"); |
| 270 | + |
| 271 | + // create a request object with invalid structure |
| 272 | + $request = new Request( |
| 273 | + "name", |
| 274 | + method: "POST", |
| 275 | + params: ["path" => "1", "query" => "0"], |
| 276 | + post: ["post" => 1, "loose" => 1] |
| 277 | + ); |
| 278 | + Assert::throws( |
| 279 | + function () use ($processParams, $presenter, $request, $action) { |
| 280 | + $processParams->invoke($presenter, $request, $action); |
| 281 | + }, |
| 282 | + BadRequestException::class |
| 283 | + ); |
| 284 | + } |
| 285 | + |
| 286 | + public function testCombinedInvalidLooseParam() |
| 287 | + { |
| 288 | + self::injectFormat(PresenterTestFormat::class); |
| 289 | + $presenter = new TestPresenter(); |
| 290 | + $action = self::getMethod($presenter, "actionTestCombined"); |
| 291 | + $processParams = self::getMethod($presenter, "processParams"); |
| 292 | + |
| 293 | + // create a request object with an invalid loose parameter |
| 294 | + $request = new Request( |
| 295 | + "name", |
| 296 | + method: "POST", |
| 297 | + params: ["path" => "1", "query" => "1"], |
| 298 | + post: ["post" => 1, "loose" => "string"] |
| 299 | + ); |
| 300 | + Assert::throws( |
| 301 | + function () use ($processParams, $presenter, $request, $action) { |
| 302 | + $processParams->invoke($presenter, $request, $action); |
| 303 | + }, |
| 304 | + InvalidApiArgumentException::class |
| 305 | + ); |
| 306 | + } |
| 307 | +} |
| 308 | + |
| 309 | +(new TestBasePresenter())->run(); |
0 commit comments