ControllerFactoryTest.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.3.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Controller;
  17. use Cake\Controller\Component\FlashComponent;
  18. use Cake\Controller\ComponentRegistry;
  19. use Cake\Controller\ControllerFactory;
  20. use Cake\Controller\Exception\InvalidParameterException;
  21. use Cake\Core\Container;
  22. use Cake\Http\Exception\MissingControllerException;
  23. use Cake\Http\Response;
  24. use Cake\Http\ServerRequest;
  25. use Cake\TestSuite\TestCase;
  26. use Company\TestPluginThree\Controller\OvensController;
  27. use stdClass;
  28. use TestApp\Controller\Admin\PostsController;
  29. use TestApp\Controller\Admin\Sub\PostsController as SubPostsController;
  30. use TestApp\Controller\CakesController;
  31. use TestApp\Controller\DependenciesController;
  32. use TestPlugin\Controller\Admin\CommentsController;
  33. use TestPlugin\Controller\TestPluginController;
  34. /**
  35. * Test case for ControllerFactory.
  36. */
  37. class ControllerFactoryTest extends TestCase
  38. {
  39. /**
  40. * @var \Cake\Controller\ControllerFactory
  41. */
  42. protected $factory;
  43. /**
  44. * @var \Cake\Core\Container
  45. */
  46. protected $container;
  47. /**
  48. * Setup
  49. */
  50. public function setUp(): void
  51. {
  52. parent::setUp();
  53. static::setAppNamespace();
  54. $this->container = new Container();
  55. $this->factory = new ControllerFactory($this->container);
  56. }
  57. /**
  58. * Test building an application controller
  59. */
  60. public function testApplicationController(): void
  61. {
  62. $request = new ServerRequest([
  63. 'url' => 'cakes/index',
  64. 'params' => [
  65. 'controller' => 'Cakes',
  66. 'action' => 'index',
  67. ],
  68. ]);
  69. $result = $this->factory->create($request);
  70. $this->assertInstanceOf(CakesController::class, $result);
  71. $this->assertSame($request, $result->getRequest());
  72. }
  73. /**
  74. * Test building a prefixed app controller.
  75. */
  76. public function testPrefixedAppController(): void
  77. {
  78. $request = new ServerRequest([
  79. 'url' => 'admin/posts/index',
  80. 'params' => [
  81. 'prefix' => 'Admin',
  82. 'controller' => 'Posts',
  83. 'action' => 'index',
  84. ],
  85. ]);
  86. $result = $this->factory->create($request);
  87. $this->assertInstanceOf(
  88. PostsController::class,
  89. $result
  90. );
  91. $this->assertSame($request, $result->getRequest());
  92. }
  93. /**
  94. * Test building a nested prefix app controller
  95. */
  96. public function testNestedPrefixedAppController(): void
  97. {
  98. $request = new ServerRequest([
  99. 'url' => 'admin/sub/posts/index',
  100. 'params' => [
  101. 'prefix' => 'Admin/Sub',
  102. 'controller' => 'Posts',
  103. 'action' => 'index',
  104. ],
  105. ]);
  106. $result = $this->factory->create($request);
  107. $this->assertInstanceOf(
  108. SubPostsController::class,
  109. $result
  110. );
  111. $this->assertSame($request, $result->getRequest());
  112. }
  113. /**
  114. * Test building a plugin controller
  115. */
  116. public function testPluginController(): void
  117. {
  118. $request = new ServerRequest([
  119. 'url' => 'test_plugin/test_plugin/index',
  120. 'params' => [
  121. 'plugin' => 'TestPlugin',
  122. 'controller' => 'TestPlugin',
  123. 'action' => 'index',
  124. ],
  125. ]);
  126. $result = $this->factory->create($request);
  127. $this->assertInstanceOf(
  128. TestPluginController::class,
  129. $result
  130. );
  131. $this->assertSame($request, $result->getRequest());
  132. }
  133. /**
  134. * Test building a vendored plugin controller.
  135. */
  136. public function testVendorPluginController(): void
  137. {
  138. $request = new ServerRequest([
  139. 'url' => 'test_plugin_three/ovens/index',
  140. 'params' => [
  141. 'plugin' => 'Company/TestPluginThree',
  142. 'controller' => 'Ovens',
  143. 'action' => 'index',
  144. ],
  145. ]);
  146. $result = $this->factory->create($request);
  147. $this->assertInstanceOf(
  148. OvensController::class,
  149. $result
  150. );
  151. $this->assertSame($request, $result->getRequest());
  152. }
  153. /**
  154. * Test building a prefixed plugin controller
  155. */
  156. public function testPrefixedPluginController(): void
  157. {
  158. $request = new ServerRequest([
  159. 'url' => 'test_plugin/admin/comments',
  160. 'params' => [
  161. 'prefix' => 'Admin',
  162. 'plugin' => 'TestPlugin',
  163. 'controller' => 'Comments',
  164. 'action' => 'index',
  165. ],
  166. ]);
  167. $result = $this->factory->create($request);
  168. $this->assertInstanceOf(
  169. CommentsController::class,
  170. $result
  171. );
  172. $this->assertSame($request, $result->getRequest());
  173. }
  174. public function testAbstractClassFailure(): void
  175. {
  176. $this->expectException(MissingControllerException::class);
  177. $this->expectExceptionMessage('Controller class `Abstract` could not be found.');
  178. $request = new ServerRequest([
  179. 'url' => 'abstract/index',
  180. 'params' => [
  181. 'controller' => 'Abstract',
  182. 'action' => 'index',
  183. ],
  184. ]);
  185. $this->factory->create($request);
  186. }
  187. public function testInterfaceFailure(): void
  188. {
  189. $this->expectException(MissingControllerException::class);
  190. $this->expectExceptionMessage('Controller class `Interface` could not be found.');
  191. $request = new ServerRequest([
  192. 'url' => 'interface/index',
  193. 'params' => [
  194. 'controller' => 'Interface',
  195. 'action' => 'index',
  196. ],
  197. ]);
  198. $this->factory->create($request);
  199. }
  200. public function testMissingClassFailure(): void
  201. {
  202. $this->expectException(MissingControllerException::class);
  203. $this->expectExceptionMessage('Controller class `Invisible` could not be found.');
  204. $request = new ServerRequest([
  205. 'url' => 'interface/index',
  206. 'params' => [
  207. 'controller' => 'Invisible',
  208. 'action' => 'index',
  209. ],
  210. ]);
  211. $this->factory->create($request);
  212. }
  213. public function testSlashedControllerFailure(): void
  214. {
  215. $this->expectException(MissingControllerException::class);
  216. $this->expectExceptionMessage('Controller class `Admin/Posts` could not be found.');
  217. $request = new ServerRequest([
  218. 'url' => 'admin/posts/index',
  219. 'params' => [
  220. 'controller' => 'Admin/Posts',
  221. 'action' => 'index',
  222. ],
  223. ]);
  224. $this->factory->create($request);
  225. }
  226. public function testAbsoluteReferenceFailure(): void
  227. {
  228. $this->expectException(MissingControllerException::class);
  229. $this->expectExceptionMessage('Controller class `TestApp\Controller\CakesController` could not be found.');
  230. $request = new ServerRequest([
  231. 'url' => 'interface/index',
  232. 'params' => [
  233. 'controller' => CakesController::class,
  234. 'action' => 'index',
  235. ],
  236. ]);
  237. $this->factory->create($request);
  238. }
  239. /**
  240. * Test create() injecting dependencies on defined controllers.
  241. */
  242. public function testCreateWithContainerDependenciesNoController(): void
  243. {
  244. $this->container->add(stdClass::class, json_decode('{"key":"value"}'));
  245. $request = new ServerRequest([
  246. 'url' => 'test_plugin_three/dependencies/index',
  247. 'params' => [
  248. 'plugin' => null,
  249. 'controller' => 'Dependencies',
  250. 'action' => 'index',
  251. ],
  252. ]);
  253. $controller = $this->factory->create($request);
  254. $this->assertNull($controller->inject);
  255. }
  256. /**
  257. * Test create() injecting dependencies on defined controllers.
  258. */
  259. public function testCreateWithContainerDependenciesWithController(): void
  260. {
  261. $request = new ServerRequest([
  262. 'url' => 'test_plugin_three/dependencies/index',
  263. 'params' => [
  264. 'plugin' => null,
  265. 'controller' => 'Dependencies',
  266. 'action' => 'index',
  267. ],
  268. ]);
  269. $this->container->add(stdClass::class, json_decode('{"key":"value"}'));
  270. $this->container->add(ServerRequest::class, $request);
  271. $this->container->add(DependenciesController::class)
  272. ->addArgument(ServerRequest::class)
  273. ->addArgument(null)
  274. ->addArgument(null)
  275. ->addArgument(stdClass::class);
  276. $controller = $this->factory->create($request);
  277. $this->assertInstanceOf(DependenciesController::class, $controller);
  278. $this->assertSame($controller->inject, $this->container->get(stdClass::class));
  279. }
  280. /**
  281. * Test building controller name when passing no controller name
  282. */
  283. public function testGetControllerClassNoControllerName(): void
  284. {
  285. $request = new ServerRequest([
  286. 'url' => 'test_plugin_three/ovens/index',
  287. 'params' => [
  288. 'plugin' => 'Company/TestPluginThree',
  289. 'controller' => 'Ovens',
  290. 'action' => 'index',
  291. ],
  292. ]);
  293. $result = $this->factory->getControllerClass($request);
  294. $this->assertSame(OvensController::class, $result);
  295. }
  296. /**
  297. * Test invoke with autorender
  298. */
  299. public function testInvokeAutoRender(): void
  300. {
  301. $request = new ServerRequest([
  302. 'url' => 'posts',
  303. 'params' => [
  304. 'controller' => 'Posts',
  305. 'action' => 'index',
  306. 'pass' => [],
  307. ],
  308. ]);
  309. $controller = $this->factory->create($request);
  310. $result = $this->factory->invoke($controller);
  311. $this->assertInstanceOf(Response::class, $result);
  312. $this->assertStringContainsString('posts index', (string)$result->getBody());
  313. }
  314. /**
  315. * Test dispatch with autorender=false
  316. */
  317. public function testInvokeAutoRenderFalse(): void
  318. {
  319. $request = new ServerRequest([
  320. 'url' => 'posts',
  321. 'params' => [
  322. 'controller' => 'Cakes',
  323. 'action' => 'noRender',
  324. 'pass' => [],
  325. ],
  326. ]);
  327. $controller = $this->factory->create($request);
  328. $result = $this->factory->invoke($controller);
  329. $this->assertInstanceOf(Response::class, $result);
  330. $this->assertStringContainsString('autoRender false body', (string)$result->getBody());
  331. }
  332. /**
  333. * Ensure that a controller's startup event can stop the request.
  334. */
  335. public function testStartupProcessAbort(): void
  336. {
  337. $request = new ServerRequest([
  338. 'url' => 'cakes/index',
  339. 'params' => [
  340. 'plugin' => null,
  341. 'controller' => 'Cakes',
  342. 'action' => 'index',
  343. 'stop' => 'startup',
  344. 'pass' => [],
  345. ],
  346. ]);
  347. $controller = $this->factory->create($request);
  348. $result = $this->factory->invoke($controller);
  349. $this->assertSame('startup stop', (string)$result->getBody());
  350. }
  351. /**
  352. * Ensure that a controllers startup process can emit a response
  353. */
  354. public function testShutdownProcessResponse(): void
  355. {
  356. $request = new ServerRequest([
  357. 'url' => 'cakes/index',
  358. 'params' => [
  359. 'plugin' => null,
  360. 'controller' => 'Cakes',
  361. 'action' => 'index',
  362. 'stop' => 'shutdown',
  363. 'pass' => [],
  364. ],
  365. ]);
  366. $controller = $this->factory->create($request);
  367. $result = $this->factory->invoke($controller);
  368. $this->assertSame('shutdown stop', (string)$result->getBody());
  369. }
  370. /**
  371. * Ensure that a controllers startup process can emit a response
  372. */
  373. public function testInvokeInjectOptionalParameterDefined(): void
  374. {
  375. $this->container->add(stdClass::class, json_decode('{"key":"value"}'));
  376. $request = new ServerRequest([
  377. 'url' => 'test_plugin_three/dependencies/optionalDep',
  378. 'params' => [
  379. 'plugin' => null,
  380. 'controller' => 'Dependencies',
  381. 'action' => 'optionalDep',
  382. ],
  383. ]);
  384. $controller = $this->factory->create($request);
  385. $result = $this->factory->invoke($controller);
  386. $data = json_decode((string)$result->getBody());
  387. $this->assertNotNull($data);
  388. $this->assertNull($data->any);
  389. $this->assertNull($data->str);
  390. $this->assertSame('value', $data->dep->key);
  391. }
  392. /**
  393. * Ensure that a controllers startup process can emit a response
  394. */
  395. public function testInvokeInjectParametersOptionalNotDefined(): void
  396. {
  397. $request = new ServerRequest([
  398. 'url' => 'test_plugin_three/dependencies/index',
  399. 'params' => [
  400. 'plugin' => null,
  401. 'controller' => 'Dependencies',
  402. 'action' => 'optionalDep',
  403. ],
  404. ]);
  405. $controller = $this->factory->create($request);
  406. $result = $this->factory->invoke($controller);
  407. $data = json_decode((string)$result->getBody());
  408. $this->assertNotNull($data);
  409. $this->assertNull($data->any);
  410. $this->assertNull($data->str);
  411. $this->assertNull($data->dep);
  412. }
  413. /**
  414. * Test invoke passing basic typed data from pass parameters.
  415. */
  416. public function testInvokeInjectParametersOptionalWithPassedParameters(): void
  417. {
  418. $this->container->add(stdClass::class, json_decode('{"key":"value"}'));
  419. $request = new ServerRequest([
  420. 'url' => 'test_plugin_three/dependencies/optionalDep',
  421. 'params' => [
  422. 'plugin' => null,
  423. 'controller' => 'Dependencies',
  424. 'action' => 'optionalDep',
  425. 'pass' => ['one', 'two'],
  426. ],
  427. ]);
  428. $controller = $this->factory->create($request);
  429. $result = $this->factory->invoke($controller);
  430. $data = json_decode((string)$result->getBody());
  431. $this->assertNotNull($data);
  432. $this->assertSame($data->any, 'one');
  433. $this->assertSame($data->str, 'two');
  434. $this->assertSame('value', $data->dep->key);
  435. }
  436. /**
  437. * Test invoke() injecting dependencies that exist in passed params as objects.
  438. * The accepted types of `params.pass` was never enforced and userland code has
  439. * creative uses of this previously unspecified behavior.
  440. */
  441. public function testCreateWithContainerDependenciesWithObjectRouteParam(): void
  442. {
  443. $inject = new stdClass();
  444. $inject->id = uniqid();
  445. $request = new ServerRequest([
  446. 'url' => 'test_plugin_three/dependencies/index',
  447. 'params' => [
  448. 'plugin' => null,
  449. 'controller' => 'Dependencies',
  450. 'action' => 'requiredDep',
  451. 'pass' => [$inject],
  452. ],
  453. ]);
  454. $controller = $this->factory->create($request);
  455. $response = $this->factory->invoke($controller);
  456. $data = json_decode((string)$response->getBody());
  457. $this->assertNotNull($data);
  458. $this->assertEquals($data->dep->id, $inject->id);
  459. }
  460. public function testCreateWithNonStringScalarRouteParam(): void
  461. {
  462. $request = new ServerRequest([
  463. 'url' => 'test_plugin_three/dependencies/required_typed',
  464. 'params' => [
  465. 'plugin' => null,
  466. 'controller' => 'Dependencies',
  467. 'action' => 'requiredTyped',
  468. 'pass' => [1.1, 2, true, ['foo' => 'bar']],
  469. ],
  470. ]);
  471. $controller = $this->factory->create($request);
  472. $response = $this->factory->invoke($controller);
  473. $expected = ['one' => 1.1, 'two' => 2, 'three' => true, 'four' => ['foo' => 'bar']];
  474. $data = json_decode((string)$response->getBody(), true);
  475. $this->assertSame($expected, $data);
  476. }
  477. /**
  478. * Ensure that a controllers startup process can emit a response
  479. */
  480. public function testInvokeInjectParametersRequiredDefined(): void
  481. {
  482. $this->container->add(stdClass::class, json_decode('{"key":"value"}'));
  483. $request = new ServerRequest([
  484. 'url' => 'test_plugin_three/dependencies/requiredDep',
  485. 'params' => [
  486. 'plugin' => null,
  487. 'controller' => 'Dependencies',
  488. 'action' => 'requiredDep',
  489. ],
  490. ]);
  491. $controller = $this->factory->create($request);
  492. $result = $this->factory->invoke($controller);
  493. $data = json_decode((string)$result->getBody());
  494. $this->assertNotNull($data);
  495. $this->assertNull($data->any);
  496. $this->assertNull($data->str);
  497. $this->assertSame('value', $data->dep->key);
  498. }
  499. /**
  500. * Ensure that a controllers startup process can emit a response
  501. */
  502. public function testInvokeInjectParametersRequiredNotDefined(): void
  503. {
  504. $request = new ServerRequest([
  505. 'url' => 'test_plugin_three/dependencies/index',
  506. 'params' => [
  507. 'plugin' => null,
  508. 'controller' => 'Dependencies',
  509. 'action' => 'requiredDep',
  510. ],
  511. ]);
  512. $controller = $this->factory->create($request);
  513. $this->expectException(InvalidParameterException::class);
  514. $this->expectExceptionMessage(
  515. 'Failed to inject dependency from service container for parameter `dep` with type `stdClass` in action `Dependencies::requiredDep()`'
  516. );
  517. $this->factory->invoke($controller);
  518. }
  519. public function testInvokeInjectParametersRequiredMissingUntyped(): void
  520. {
  521. $request = new ServerRequest([
  522. 'url' => 'test_plugin_three/dependencies/requiredParam',
  523. 'params' => [
  524. 'plugin' => null,
  525. 'controller' => 'Dependencies',
  526. 'action' => 'requiredParam',
  527. ],
  528. ]);
  529. $controller = $this->factory->create($request);
  530. $this->expectException(InvalidParameterException::class);
  531. $this->expectExceptionMessage('Missing passed parameter for `one` in action `Dependencies::requiredParam()`');
  532. $this->factory->invoke($controller);
  533. }
  534. public function testInvokeInjectParametersRequiredUntyped(): void
  535. {
  536. $request = new ServerRequest([
  537. 'url' => 'test_plugin_three/dependencies/requiredParam',
  538. 'params' => [
  539. 'plugin' => null,
  540. 'controller' => 'Dependencies',
  541. 'action' => 'requiredParam',
  542. 'pass' => ['one'],
  543. ],
  544. ]);
  545. $controller = $this->factory->create($request);
  546. $result = $this->factory->invoke($controller);
  547. $data = json_decode((string)$result->getBody());
  548. $this->assertNotNull($data);
  549. $this->assertSame($data->one, 'one');
  550. }
  551. public function testInvokeInjectParametersRequiredWithPassedParameters(): void
  552. {
  553. $this->container->add(stdClass::class, json_decode('{"key":"value"}'));
  554. $request = new ServerRequest([
  555. 'url' => 'test_plugin_three/dependencies/requiredDep',
  556. 'params' => [
  557. 'plugin' => null,
  558. 'controller' => 'Dependencies',
  559. 'action' => 'requiredDep',
  560. 'pass' => ['one', 'two'],
  561. ],
  562. ]);
  563. $controller = $this->factory->create($request);
  564. $result = $this->factory->invoke($controller);
  565. $data = json_decode((string)$result->getBody());
  566. $this->assertNotNull($data);
  567. $this->assertSame($data->any, 'one');
  568. $this->assertSame($data->str, 'two');
  569. $this->assertSame('value', $data->dep->key);
  570. }
  571. /**
  572. * Test that routing parameters are passed into variadic controller functions
  573. */
  574. public function testInvokeInjectPassedParametersVariadic(): void
  575. {
  576. $request = new ServerRequest([
  577. 'url' => 'test_plugin_three/dependencies/variadic',
  578. 'params' => [
  579. 'plugin' => null,
  580. 'controller' => 'Dependencies',
  581. 'action' => 'variadic',
  582. 'pass' => ['one', 'two'],
  583. ],
  584. ]);
  585. $controller = $this->factory->create($request);
  586. $result = $this->factory->invoke($controller);
  587. $data = json_decode((string)$result->getBody());
  588. $this->assertNotNull($data);
  589. $this->assertSame(['one', 'two'], $data->args);
  590. }
  591. /**
  592. * Test that routing parameters are passed into controller action using spread operator
  593. */
  594. public function testInvokeInjectPassedParametersSpread(): void
  595. {
  596. $request = new ServerRequest([
  597. 'url' => 'test_plugin_three/dependencies/spread',
  598. 'params' => [
  599. 'plugin' => null,
  600. 'controller' => 'Dependencies',
  601. 'action' => 'spread',
  602. 'pass' => ['one', 'two'],
  603. ],
  604. ]);
  605. $controller = $this->factory->create($request);
  606. $result = $this->factory->invoke($controller);
  607. $data = json_decode((string)$result->getBody());
  608. $this->assertNotNull($data);
  609. $this->assertSame(['one', 'two'], $data->args);
  610. }
  611. /**
  612. * Test that routing parameters are passed into controller action using spread operator
  613. */
  614. public function testInvokeInjectPassedParametersSpreadNoParams(): void
  615. {
  616. $request = new ServerRequest([
  617. 'url' => 'test_plugin_three/dependencies/spread',
  618. 'params' => [
  619. 'plugin' => null,
  620. 'controller' => 'Dependencies',
  621. 'action' => 'spread',
  622. 'pass' => [],
  623. ],
  624. ]);
  625. $controller = $this->factory->create($request);
  626. $result = $this->factory->invoke($controller);
  627. $data = json_decode((string)$result->getBody());
  628. $this->assertNotNull($data);
  629. $this->assertSame([], $data->args);
  630. }
  631. /**
  632. * Test that default parameters work for controller methods
  633. */
  634. public function testInvokeOptionalStringParam(): void
  635. {
  636. $request = new ServerRequest([
  637. 'url' => 'test_plugin_three/dependencies/optionalString',
  638. 'params' => [
  639. 'plugin' => null,
  640. 'controller' => 'Dependencies',
  641. 'action' => 'optionalString',
  642. ],
  643. ]);
  644. $controller = $this->factory->create($request);
  645. $result = $this->factory->invoke($controller);
  646. $data = json_decode((string)$result->getBody());
  647. $this->assertNotNull($data);
  648. $this->assertSame('default val', $data->str);
  649. }
  650. /**
  651. * Test that required strings a default value.
  652. */
  653. public function testInvokeRequiredStringParam(): void
  654. {
  655. $request = new ServerRequest([
  656. 'url' => 'test_plugin_three/dependencies/requiredString',
  657. 'params' => [
  658. 'plugin' => null,
  659. 'controller' => 'Dependencies',
  660. 'action' => 'requiredString',
  661. ],
  662. ]);
  663. $controller = $this->factory->create($request);
  664. $this->expectException(InvalidParameterException::class);
  665. $this->expectExceptionMessage('Missing passed parameter for `str` in action `Dependencies::requiredString()`');
  666. $this->factory->invoke($controller);
  667. }
  668. /**
  669. * Test that coercing string to float, int and bool params
  670. */
  671. public function testInvokePassedParametersCoercion(): void
  672. {
  673. $request = new ServerRequest([
  674. 'url' => 'test_plugin_three/dependencies/requiredTyped',
  675. 'params' => [
  676. 'plugin' => null,
  677. 'controller' => 'Dependencies',
  678. 'action' => 'requiredTyped',
  679. 'pass' => ['1.0', '2', '0', '8,9'],
  680. ],
  681. ]);
  682. $controller = $this->factory->create($request);
  683. $result = $this->factory->invoke($controller);
  684. $data = json_decode((string)$result->getBody(), true);
  685. $this->assertSame(['one' => 1.0, 'two' => 2, 'three' => false, 'four' => ['8', '9']], $data);
  686. $request = new ServerRequest([
  687. 'url' => 'test_plugin_three/dependencies/requiredTyped',
  688. 'params' => [
  689. 'plugin' => null,
  690. 'controller' => 'Dependencies',
  691. 'action' => 'requiredTyped',
  692. 'pass' => ['1.0', '0', '0', ''],
  693. ],
  694. ]);
  695. $controller = $this->factory->create($request);
  696. $result = $this->factory->invoke($controller);
  697. $data = json_decode((string)$result->getBody(), true);
  698. $this->assertSame(['one' => 1.0, 'two' => 0, 'three' => false, 'four' => []], $data);
  699. $request = new ServerRequest([
  700. 'url' => 'test_plugin_three/dependencies/requiredTyped',
  701. 'params' => [
  702. 'plugin' => null,
  703. 'controller' => 'Dependencies',
  704. 'action' => 'requiredTyped',
  705. 'pass' => ['1.0', '-1', '0', ''],
  706. ],
  707. ]);
  708. $controller = $this->factory->create($request);
  709. $result = $this->factory->invoke($controller);
  710. $data = json_decode((string)$result->getBody(), true);
  711. $this->assertSame(['one' => 1.0, 'two' => -1, 'three' => false, 'four' => []], $data);
  712. }
  713. /**
  714. * Test that default values work for typed parameters
  715. */
  716. public function testInvokeOptionalTypedParam(): void
  717. {
  718. $request = new ServerRequest([
  719. 'url' => 'test_plugin_three/dependencies/optionalTyped',
  720. 'params' => [
  721. 'plugin' => null,
  722. 'controller' => 'Dependencies',
  723. 'action' => 'optionalTyped',
  724. 'pass' => ['1.0'],
  725. ],
  726. ]);
  727. $controller = $this->factory->create($request);
  728. $result = $this->factory->invoke($controller);
  729. $data = json_decode((string)$result->getBody(), true);
  730. $this->assertSame(['one' => 1.0, 'two' => 2, 'three' => true], $data);
  731. }
  732. /**
  733. * Test using invalid value for supported type
  734. */
  735. public function testInvokePassedParametersUnsupportedFloatCoercion(): void
  736. {
  737. $request = new ServerRequest([
  738. 'url' => 'test_plugin_three/dependencies/requiredTyped',
  739. 'params' => [
  740. 'plugin' => null,
  741. 'controller' => 'Dependencies',
  742. 'action' => 'requiredTyped',
  743. 'pass' => ['true', '2', '1'],
  744. ],
  745. ]);
  746. $controller = $this->factory->create($request);
  747. $this->expectException(InvalidParameterException::class);
  748. $this->expectExceptionMessage('Unable to coerce `true` to `float` for `one` in action `Dependencies::requiredTyped()`');
  749. $this->factory->invoke($controller);
  750. }
  751. /**
  752. * Test using invalid value for supported type
  753. */
  754. public function testInvokePassedParametersUnsupportedIntCoercion(): void
  755. {
  756. $request = new ServerRequest([
  757. 'url' => 'test_plugin_three/dependencies/requiredTyped',
  758. 'params' => [
  759. 'plugin' => null,
  760. 'controller' => 'Dependencies',
  761. 'action' => 'requiredTyped',
  762. 'pass' => ['1', '2.0', '1'],
  763. ],
  764. ]);
  765. $controller = $this->factory->create($request);
  766. $this->expectException(InvalidParameterException::class);
  767. $this->expectExceptionMessage('Unable to coerce `2.0` to `int` for `two` in action `Dependencies::requiredTyped()`');
  768. $this->factory->invoke($controller);
  769. }
  770. /**
  771. * Test using invalid value for supported type
  772. */
  773. public function testInvokePassedParametersUnsupportedBoolCoercion(): void
  774. {
  775. $request = new ServerRequest([
  776. 'url' => 'test_plugin_three/dependencies/requiredTyped',
  777. 'params' => [
  778. 'plugin' => null,
  779. 'controller' => 'Dependencies',
  780. 'action' => 'requiredTyped',
  781. 'pass' => ['1', '1', 'true'],
  782. ],
  783. ]);
  784. $controller = $this->factory->create($request);
  785. $this->expectException(InvalidParameterException::class);
  786. $this->expectExceptionMessage('Unable to coerce `true` to `bool` for `three` in action `Dependencies::requiredTyped()`');
  787. $this->factory->invoke($controller);
  788. }
  789. /**
  790. * Test using an unsupported type.
  791. */
  792. public function testInvokePassedParamUnsupportedType(): void
  793. {
  794. $request = new ServerRequest([
  795. 'url' => 'test_plugin_three/dependencies/unsupportedTyped',
  796. 'params' => [
  797. 'plugin' => null,
  798. 'controller' => 'Dependencies',
  799. 'action' => 'unsupportedTyped',
  800. 'pass' => ['test'],
  801. ],
  802. ]);
  803. $controller = $this->factory->create($request);
  804. $this->expectException(InvalidParameterException::class);
  805. $this->expectExceptionMessage('Unable to coerce `test` to `iterable` for `one` in action `Dependencies::unsupportedTyped()`');
  806. $this->factory->invoke($controller);
  807. }
  808. /**
  809. * Test using an unsupported reflection type.
  810. */
  811. public function testInvokePassedParamUnsupportedReflectionType(): void
  812. {
  813. $request = new ServerRequest([
  814. 'url' => 'test_plugin_three/dependencies/unsupportedTypedUnion',
  815. 'params' => [
  816. 'plugin' => null,
  817. 'controller' => 'Dependencies',
  818. 'action' => 'typedUnion',
  819. 'pass' => ['1'],
  820. ],
  821. ]);
  822. $controller = $this->factory->create($request);
  823. $result = $this->factory->invoke($controller);
  824. $data = json_decode((string)$result->getBody(), true);
  825. $this->assertSame(['one' => '1'], $data);
  826. }
  827. /**
  828. * Test that default values work for typed parameters
  829. */
  830. public function testInvokeComponentFromContainer(): void
  831. {
  832. $this->container->add(FlashComponent::class, function (ComponentRegistry $registry, array $config) {
  833. return new FlashComponent($registry, $config);
  834. })
  835. ->addArgument(ComponentRegistry::class)
  836. ->addArgument(['key' => 'customFlash']);
  837. $request = new ServerRequest([
  838. 'url' => 'test_plugin_three/component-test/flash',
  839. 'params' => [
  840. 'plugin' => null,
  841. 'controller' => 'ComponentTest',
  842. 'action' => 'flash',
  843. 'pass' => [],
  844. ],
  845. ]);
  846. $controller = $this->factory->create($request);
  847. $result = $this->factory->invoke($controller);
  848. $data = json_decode((string)$result->getBody(), true);
  849. $this->assertSame(['flashKey' => 'customFlash'], $data);
  850. }
  851. public function testMiddleware(): void
  852. {
  853. $request = new ServerRequest([
  854. 'url' => 'posts',
  855. 'params' => [
  856. 'controller' => 'Posts',
  857. 'action' => 'index',
  858. 'pass' => [],
  859. ],
  860. ]);
  861. $controller = $this->factory->create($request);
  862. $this->factory->invoke($controller);
  863. $request = $controller->getRequest();
  864. $this->assertTrue($request->getAttribute('for-all'));
  865. $this->assertTrue($request->getAttribute('index-only'));
  866. $this->assertNull($request->getAttribute('all-except-index'));
  867. $request = new ServerRequest([
  868. 'url' => 'posts/get',
  869. 'params' => [
  870. 'controller' => 'Posts',
  871. 'action' => 'get',
  872. 'pass' => [],
  873. ],
  874. ]);
  875. $controller = $this->factory->create($request);
  876. $this->factory->invoke($controller);
  877. $request = $controller->getRequest();
  878. $this->assertTrue($request->getAttribute('for-all'));
  879. $this->assertNull($request->getAttribute('index-only'));
  880. $this->assertTrue($request->getAttribute('all-except-index'));
  881. }
  882. }