ControllerFactoryTest.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  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. $this->container->add(stdClass::class, json_decode('{"key":"value"}'));
  254. $this->container->add(DependenciesController::class)
  255. ->addArgument(ServerRequest::class)
  256. ->addArgument(null)
  257. ->addArgument(null)
  258. ->addArgument(null)
  259. ->addArgument(null)
  260. ->addArgument(stdClass::class);
  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. $controller = $this->factory->create($request);
  270. $this->assertInstanceOf(DependenciesController::class, $controller);
  271. $this->assertSame($controller->inject, $this->container->get(stdClass::class));
  272. }
  273. /**
  274. * Test building controller name when passing no controller name
  275. */
  276. public function testGetControllerClassNoControllerName(): void
  277. {
  278. $request = new ServerRequest([
  279. 'url' => 'test_plugin_three/ovens/index',
  280. 'params' => [
  281. 'plugin' => 'Company/TestPluginThree',
  282. 'controller' => 'Ovens',
  283. 'action' => 'index',
  284. ],
  285. ]);
  286. $result = $this->factory->getControllerClass($request);
  287. $this->assertSame('Company\TestPluginThree\Controller\OvensController', $result);
  288. }
  289. /**
  290. * Test invoke with autorender
  291. */
  292. public function testInvokeAutoRender(): void
  293. {
  294. $request = new ServerRequest([
  295. 'url' => 'posts',
  296. 'params' => [
  297. 'controller' => 'Posts',
  298. 'action' => 'index',
  299. 'pass' => [],
  300. ],
  301. ]);
  302. $controller = $this->factory->create($request);
  303. $result = $this->factory->invoke($controller);
  304. $this->assertInstanceOf(Response::class, $result);
  305. $this->assertStringContainsString('posts index', (string)$result->getBody());
  306. }
  307. /**
  308. * Test dispatch with autorender=false
  309. */
  310. public function testInvokeAutoRenderFalse(): void
  311. {
  312. $request = new ServerRequest([
  313. 'url' => 'posts',
  314. 'params' => [
  315. 'controller' => 'Cakes',
  316. 'action' => 'noRender',
  317. 'pass' => [],
  318. ],
  319. ]);
  320. $controller = $this->factory->create($request);
  321. $result = $this->factory->invoke($controller);
  322. $this->assertInstanceOf(Response::class, $result);
  323. $this->assertStringContainsString('autoRender false body', (string)$result->getBody());
  324. }
  325. /**
  326. * Ensure that a controller's startup event can stop the request.
  327. */
  328. public function testStartupProcessAbort(): void
  329. {
  330. $request = new ServerRequest([
  331. 'url' => 'cakes/index',
  332. 'params' => [
  333. 'plugin' => null,
  334. 'controller' => 'Cakes',
  335. 'action' => 'index',
  336. 'stop' => 'startup',
  337. 'pass' => [],
  338. ],
  339. ]);
  340. $controller = $this->factory->create($request);
  341. $result = $this->factory->invoke($controller);
  342. $this->assertSame('startup stop', (string)$result->getBody());
  343. }
  344. /**
  345. * Ensure that a controllers startup process can emit a response
  346. */
  347. public function testShutdownProcessResponse(): void
  348. {
  349. $request = new ServerRequest([
  350. 'url' => 'cakes/index',
  351. 'params' => [
  352. 'plugin' => null,
  353. 'controller' => 'Cakes',
  354. 'action' => 'index',
  355. 'stop' => 'shutdown',
  356. 'pass' => [],
  357. ],
  358. ]);
  359. $controller = $this->factory->create($request);
  360. $result = $this->factory->invoke($controller);
  361. $this->assertSame('shutdown stop', (string)$result->getBody());
  362. }
  363. /**
  364. * Ensure that a controllers startup process can emit a response
  365. */
  366. public function testInvokeInjectOptionalParameterDefined(): void
  367. {
  368. $this->container->add(stdClass::class, json_decode('{"key":"value"}'));
  369. $request = new ServerRequest([
  370. 'url' => 'test_plugin_three/dependencies/optionalDep',
  371. 'params' => [
  372. 'plugin' => null,
  373. 'controller' => 'Dependencies',
  374. 'action' => 'optionalDep',
  375. ],
  376. ]);
  377. $controller = $this->factory->create($request);
  378. $result = $this->factory->invoke($controller);
  379. $data = json_decode((string)$result->getBody());
  380. $this->assertNotNull($data);
  381. $this->assertNull($data->any);
  382. $this->assertNull($data->str);
  383. $this->assertSame('value', $data->dep->key);
  384. }
  385. /**
  386. * Ensure that a controllers startup process can emit a response
  387. */
  388. public function testInvokeInjectParametersOptionalNotDefined(): void
  389. {
  390. $request = new ServerRequest([
  391. 'url' => 'test_plugin_three/dependencies/index',
  392. 'params' => [
  393. 'plugin' => null,
  394. 'controller' => 'Dependencies',
  395. 'action' => 'optionalDep',
  396. ],
  397. ]);
  398. $controller = $this->factory->create($request);
  399. $result = $this->factory->invoke($controller);
  400. $data = json_decode((string)$result->getBody());
  401. $this->assertNotNull($data);
  402. $this->assertNull($data->any);
  403. $this->assertNull($data->str);
  404. $this->assertNull($data->dep);
  405. }
  406. /**
  407. * Test invoke passing basic typed data from pass parameters.
  408. */
  409. public function testInvokeInjectParametersOptionalWithPassedParameters(): void
  410. {
  411. $this->container->add(stdClass::class, json_decode('{"key":"value"}'));
  412. $request = new ServerRequest([
  413. 'url' => 'test_plugin_three/dependencies/optionalDep',
  414. 'params' => [
  415. 'plugin' => null,
  416. 'controller' => 'Dependencies',
  417. 'action' => 'optionalDep',
  418. 'pass' => ['one', 'two'],
  419. ],
  420. ]);
  421. $controller = $this->factory->create($request);
  422. $result = $this->factory->invoke($controller);
  423. $data = json_decode((string)$result->getBody());
  424. $this->assertNotNull($data);
  425. $this->assertSame($data->any, 'one');
  426. $this->assertSame($data->str, 'two');
  427. $this->assertSame('value', $data->dep->key);
  428. }
  429. /**
  430. * Test invoke() injecting dependencies that exist in passed params as objects.
  431. * The accepted types of `params.pass` was never enforced and userland code has
  432. * creative uses of this previously unspecified behavior.
  433. */
  434. public function testCreateWithContainerDependenciesWithObjectRouteParam(): void
  435. {
  436. $inject = new stdClass();
  437. $inject->id = uniqid();
  438. $request = new ServerRequest([
  439. 'url' => 'test_plugin_three/dependencies/index',
  440. 'params' => [
  441. 'plugin' => null,
  442. 'controller' => 'Dependencies',
  443. 'action' => 'requiredDep',
  444. 'pass' => [$inject],
  445. ],
  446. ]);
  447. $controller = $this->factory->create($request);
  448. $response = $this->factory->invoke($controller);
  449. $data = json_decode((string)$response->getBody());
  450. $this->assertNotNull($data);
  451. $this->assertEquals($data->dep->id, $inject->id);
  452. }
  453. public function testCreateWithNonStringScalarRouteParam(): void
  454. {
  455. $request = new ServerRequest([
  456. 'url' => 'test_plugin_three/dependencies/required_typed',
  457. 'params' => [
  458. 'plugin' => null,
  459. 'controller' => 'Dependencies',
  460. 'action' => 'requiredTyped',
  461. 'pass' => [1.1, 2, true, ['foo' => 'bar']],
  462. ],
  463. ]);
  464. $controller = $this->factory->create($request);
  465. $response = $this->factory->invoke($controller);
  466. $expected = ['one' => 1.1, 'two' => 2, 'three' => true, 'four' => ['foo' => 'bar']];
  467. $data = json_decode((string)$response->getBody(), true);
  468. $this->assertSame($expected, $data);
  469. }
  470. /**
  471. * Ensure that a controllers startup process can emit a response
  472. */
  473. public function testInvokeInjectParametersRequiredDefined(): void
  474. {
  475. $this->container->add(stdClass::class, json_decode('{"key":"value"}'));
  476. $request = new ServerRequest([
  477. 'url' => 'test_plugin_three/dependencies/requiredDep',
  478. 'params' => [
  479. 'plugin' => null,
  480. 'controller' => 'Dependencies',
  481. 'action' => 'requiredDep',
  482. ],
  483. ]);
  484. $controller = $this->factory->create($request);
  485. $result = $this->factory->invoke($controller);
  486. $data = json_decode((string)$result->getBody());
  487. $this->assertNotNull($data);
  488. $this->assertNull($data->any);
  489. $this->assertNull($data->str);
  490. $this->assertSame('value', $data->dep->key);
  491. }
  492. /**
  493. * Ensure that a controllers startup process can emit a response
  494. */
  495. public function testInvokeInjectParametersRequiredNotDefined(): void
  496. {
  497. $request = new ServerRequest([
  498. 'url' => 'test_plugin_three/dependencies/index',
  499. 'params' => [
  500. 'plugin' => null,
  501. 'controller' => 'Dependencies',
  502. 'action' => 'requiredDep',
  503. ],
  504. ]);
  505. $controller = $this->factory->create($request);
  506. $this->expectException(InvalidParameterException::class);
  507. $this->expectExceptionMessage(
  508. 'Failed to inject dependency from service container for parameter `dep` with type `stdClass` in action Dependencies::requiredDep()'
  509. );
  510. $this->factory->invoke($controller);
  511. }
  512. public function testInvokeInjectParametersRequiredMissingUntyped(): void
  513. {
  514. $request = new ServerRequest([
  515. 'url' => 'test_plugin_three/dependencies/requiredParam',
  516. 'params' => [
  517. 'plugin' => null,
  518. 'controller' => 'Dependencies',
  519. 'action' => 'requiredParam',
  520. ],
  521. ]);
  522. $controller = $this->factory->create($request);
  523. $this->expectException(InvalidParameterException::class);
  524. $this->expectExceptionMessage('Missing passed parameter for `one` in action Dependencies::requiredParam()');
  525. $this->factory->invoke($controller);
  526. }
  527. public function testInvokeInjectParametersRequiredUntyped(): void
  528. {
  529. $request = new ServerRequest([
  530. 'url' => 'test_plugin_three/dependencies/requiredParam',
  531. 'params' => [
  532. 'plugin' => null,
  533. 'controller' => 'Dependencies',
  534. 'action' => 'requiredParam',
  535. 'pass' => ['one'],
  536. ],
  537. ]);
  538. $controller = $this->factory->create($request);
  539. $result = $this->factory->invoke($controller);
  540. $data = json_decode((string)$result->getBody());
  541. $this->assertNotNull($data);
  542. $this->assertSame($data->one, 'one');
  543. }
  544. public function testInvokeInjectParametersRequiredWithPassedParameters(): void
  545. {
  546. $this->container->add(stdClass::class, json_decode('{"key":"value"}'));
  547. $request = new ServerRequest([
  548. 'url' => 'test_plugin_three/dependencies/requiredDep',
  549. 'params' => [
  550. 'plugin' => null,
  551. 'controller' => 'Dependencies',
  552. 'action' => 'requiredDep',
  553. 'pass' => ['one', 'two'],
  554. ],
  555. ]);
  556. $controller = $this->factory->create($request);
  557. $result = $this->factory->invoke($controller);
  558. $data = json_decode((string)$result->getBody());
  559. $this->assertNotNull($data);
  560. $this->assertSame($data->any, 'one');
  561. $this->assertSame($data->str, 'two');
  562. $this->assertSame('value', $data->dep->key);
  563. }
  564. /**
  565. * Test that routing parameters are passed into variadic controller functions
  566. */
  567. public function testInvokeInjectPassedParametersVariadic(): void
  568. {
  569. $request = new ServerRequest([
  570. 'url' => 'test_plugin_three/dependencies/variadic',
  571. 'params' => [
  572. 'plugin' => null,
  573. 'controller' => 'Dependencies',
  574. 'action' => 'variadic',
  575. 'pass' => ['one', 'two'],
  576. ],
  577. ]);
  578. $controller = $this->factory->create($request);
  579. $result = $this->factory->invoke($controller);
  580. $data = json_decode((string)$result->getBody());
  581. $this->assertNotNull($data);
  582. $this->assertSame(['one', 'two'], $data->args);
  583. }
  584. /**
  585. * Test that routing parameters are passed into controller action using spread operator
  586. */
  587. public function testInvokeInjectPassedParametersSpread(): void
  588. {
  589. $request = new ServerRequest([
  590. 'url' => 'test_plugin_three/dependencies/spread',
  591. 'params' => [
  592. 'plugin' => null,
  593. 'controller' => 'Dependencies',
  594. 'action' => 'spread',
  595. 'pass' => ['one', 'two'],
  596. ],
  597. ]);
  598. $controller = $this->factory->create($request);
  599. $result = $this->factory->invoke($controller);
  600. $data = json_decode((string)$result->getBody());
  601. $this->assertNotNull($data);
  602. $this->assertSame(['one', 'two'], $data->args);
  603. }
  604. /**
  605. * Test that routing parameters are passed into controller action using spread operator
  606. */
  607. public function testInvokeInjectPassedParametersSpreadNoParams(): void
  608. {
  609. $request = new ServerRequest([
  610. 'url' => 'test_plugin_three/dependencies/spread',
  611. 'params' => [
  612. 'plugin' => null,
  613. 'controller' => 'Dependencies',
  614. 'action' => 'spread',
  615. 'pass' => [],
  616. ],
  617. ]);
  618. $controller = $this->factory->create($request);
  619. $result = $this->factory->invoke($controller);
  620. $data = json_decode((string)$result->getBody());
  621. $this->assertNotNull($data);
  622. $this->assertSame([], $data->args);
  623. }
  624. /**
  625. * Test that default parameters work for controller methods
  626. */
  627. public function testInvokeOptionalStringParam(): void
  628. {
  629. $request = new ServerRequest([
  630. 'url' => 'test_plugin_three/dependencies/optionalString',
  631. 'params' => [
  632. 'plugin' => null,
  633. 'controller' => 'Dependencies',
  634. 'action' => 'optionalString',
  635. ],
  636. ]);
  637. $controller = $this->factory->create($request);
  638. $result = $this->factory->invoke($controller);
  639. $data = json_decode((string)$result->getBody());
  640. $this->assertNotNull($data);
  641. $this->assertSame('default val', $data->str);
  642. }
  643. /**
  644. * Test that required strings a default value.
  645. */
  646. public function testInvokeRequiredStringParam(): void
  647. {
  648. $request = new ServerRequest([
  649. 'url' => 'test_plugin_three/dependencies/requiredString',
  650. 'params' => [
  651. 'plugin' => null,
  652. 'controller' => 'Dependencies',
  653. 'action' => 'requiredString',
  654. ],
  655. ]);
  656. $controller = $this->factory->create($request);
  657. $this->expectException(InvalidParameterException::class);
  658. $this->expectExceptionMessage('Missing passed parameter for `str` in action Dependencies::requiredString()');
  659. $this->factory->invoke($controller);
  660. }
  661. /**
  662. * Test that coercing string to float, int and bool params
  663. */
  664. public function testInvokePassedParametersCoercion(): void
  665. {
  666. $request = new ServerRequest([
  667. 'url' => 'test_plugin_three/dependencies/requiredTyped',
  668. 'params' => [
  669. 'plugin' => null,
  670. 'controller' => 'Dependencies',
  671. 'action' => 'requiredTyped',
  672. 'pass' => ['1.0', '02', '0', '8,9'],
  673. ],
  674. ]);
  675. $controller = $this->factory->create($request);
  676. $result = $this->factory->invoke($controller);
  677. $data = json_decode((string)$result->getBody(), true);
  678. $this->assertNotNull($data);
  679. $this->assertSame(['one' => 1.0, 'two' => 2, 'three' => false, 'four' => ['8', '9']], $data);
  680. }
  681. /**
  682. * Test that default values work for typed parameters
  683. */
  684. public function testInvokeOptionalTypedParam(): void
  685. {
  686. $request = new ServerRequest([
  687. 'url' => 'test_plugin_three/dependencies/optionalTyped',
  688. 'params' => [
  689. 'plugin' => null,
  690. 'controller' => 'Dependencies',
  691. 'action' => 'optionalTyped',
  692. 'pass' => ['1.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->assertNotNull($data);
  699. $this->assertSame(['one' => 1.0, 'two' => 2, 'three' => true], $data);
  700. }
  701. /**
  702. * Test using invalid value for supported type
  703. */
  704. public function testInvokePassedParametersUnsupportedFloatCoercion(): void
  705. {
  706. $request = new ServerRequest([
  707. 'url' => 'test_plugin_three/dependencies/requiredTyped',
  708. 'params' => [
  709. 'plugin' => null,
  710. 'controller' => 'Dependencies',
  711. 'action' => 'requiredTyped',
  712. 'pass' => ['true', '2', '1'],
  713. ],
  714. ]);
  715. $controller = $this->factory->create($request);
  716. $this->expectException(InvalidParameterException::class);
  717. $this->expectExceptionMessage('Unable to coerce "true" to `float` for `one` in action Dependencies::requiredTyped()');
  718. $this->factory->invoke($controller);
  719. }
  720. /**
  721. * Test using invalid value for supported type
  722. */
  723. public function testInvokePassedParametersUnsupportedIntCoercion(): void
  724. {
  725. $request = new ServerRequest([
  726. 'url' => 'test_plugin_three/dependencies/requiredTyped',
  727. 'params' => [
  728. 'plugin' => null,
  729. 'controller' => 'Dependencies',
  730. 'action' => 'requiredTyped',
  731. 'pass' => ['1', '2.0', '1'],
  732. ],
  733. ]);
  734. $controller = $this->factory->create($request);
  735. $this->expectException(InvalidParameterException::class);
  736. $this->expectExceptionMessage('Unable to coerce "2.0" to `int` for `two` in action Dependencies::requiredTyped()');
  737. $this->factory->invoke($controller);
  738. }
  739. /**
  740. * Test using invalid value for supported type
  741. */
  742. public function testInvokePassedParametersUnsupportedBoolCoercion(): void
  743. {
  744. $request = new ServerRequest([
  745. 'url' => 'test_plugin_three/dependencies/requiredTyped',
  746. 'params' => [
  747. 'plugin' => null,
  748. 'controller' => 'Dependencies',
  749. 'action' => 'requiredTyped',
  750. 'pass' => ['1', '1', 'true'],
  751. ],
  752. ]);
  753. $controller = $this->factory->create($request);
  754. $this->expectException(InvalidParameterException::class);
  755. $this->expectExceptionMessage('Unable to coerce "true" to `bool` for `three` in action Dependencies::requiredTyped()');
  756. $this->factory->invoke($controller);
  757. }
  758. /**
  759. * Test using an unsupported type.
  760. */
  761. public function testInvokePassedParamUnsupportedType(): void
  762. {
  763. $request = new ServerRequest([
  764. 'url' => 'test_plugin_three/dependencies/unsupportedTyped',
  765. 'params' => [
  766. 'plugin' => null,
  767. 'controller' => 'Dependencies',
  768. 'action' => 'unsupportedTyped',
  769. 'pass' => ['test'],
  770. ],
  771. ]);
  772. $controller = $this->factory->create($request);
  773. $this->expectException(InvalidParameterException::class);
  774. $this->expectExceptionMessage('Unable to coerce "test" to `iterable` for `one` in action Dependencies::unsupportedTyped()');
  775. $this->factory->invoke($controller);
  776. }
  777. public function testMiddleware(): void
  778. {
  779. $request = new ServerRequest([
  780. 'url' => 'posts',
  781. 'params' => [
  782. 'controller' => 'Posts',
  783. 'action' => 'index',
  784. 'pass' => [],
  785. ],
  786. ]);
  787. $controller = $this->factory->create($request);
  788. $this->factory->invoke($controller);
  789. $request = $controller->getRequest();
  790. $this->assertTrue($request->getAttribute('for-all'));
  791. $this->assertTrue($request->getAttribute('index-only'));
  792. $this->assertNull($request->getAttribute('all-except-index'));
  793. $request = new ServerRequest([
  794. 'url' => 'posts/get',
  795. 'params' => [
  796. 'controller' => 'Posts',
  797. 'action' => 'get',
  798. 'pass' => [],
  799. ],
  800. ]);
  801. $controller = $this->factory->create($request);
  802. $this->factory->invoke($controller);
  803. $request = $controller->getRequest();
  804. $this->assertTrue($request->getAttribute('for-all'));
  805. $this->assertNull($request->getAttribute('index-only'));
  806. $this->assertTrue($request->getAttribute('all-except-index'));
  807. }
  808. }