ControllerFactoryTest.php 31 KB

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