ControllerFactoryTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  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 dependcies 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 dependcies 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. * Ensure that a controllers startup process can emit a response
  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. * Ensure that a controllers startup process can emit a response
  431. */
  432. public function testInvokeInjectParametersRequiredDefined(): void
  433. {
  434. $this->container->add(stdClass::class, json_decode('{"key":"value"}'));
  435. $request = new ServerRequest([
  436. 'url' => 'test_plugin_three/dependencies/requiredDep',
  437. 'params' => [
  438. 'plugin' => null,
  439. 'controller' => 'Dependencies',
  440. 'action' => 'requiredDep',
  441. ],
  442. ]);
  443. $controller = $this->factory->create($request);
  444. $result = $this->factory->invoke($controller);
  445. $data = json_decode((string)$result->getBody());
  446. $this->assertNotNull($data);
  447. $this->assertNull($data->any);
  448. $this->assertNull($data->str);
  449. $this->assertSame('value', $data->dep->key);
  450. }
  451. /**
  452. * Ensure that a controllers startup process can emit a response
  453. */
  454. public function testInvokeInjectParametersRequiredNotDefined(): void
  455. {
  456. $request = new ServerRequest([
  457. 'url' => 'test_plugin_three/dependencies/index',
  458. 'params' => [
  459. 'plugin' => null,
  460. 'controller' => 'Dependencies',
  461. 'action' => 'requiredDep',
  462. ],
  463. ]);
  464. $controller = $this->factory->create($request);
  465. $this->expectException(InvalidParameterException::class);
  466. $this->expectExceptionMessage('Failed to inject dependency from service container for `dep` in action Dependencies::requiredDep()');
  467. $this->factory->invoke($controller);
  468. }
  469. public function testInvokeInjectParametersRequiredMissingUntyped(): void
  470. {
  471. $request = new ServerRequest([
  472. 'url' => 'test_plugin_three/dependencies/requiredParam',
  473. 'params' => [
  474. 'plugin' => null,
  475. 'controller' => 'Dependencies',
  476. 'action' => 'requiredParam',
  477. ],
  478. ]);
  479. $controller = $this->factory->create($request);
  480. $this->expectException(InvalidParameterException::class);
  481. $this->expectExceptionMessage('Missing passed parameter for `one` in action Dependencies::requiredParam()');
  482. $this->factory->invoke($controller);
  483. }
  484. public function testInvokeInjectParametersRequiredUntyped(): void
  485. {
  486. $request = new ServerRequest([
  487. 'url' => 'test_plugin_three/dependencies/requiredParam',
  488. 'params' => [
  489. 'plugin' => null,
  490. 'controller' => 'Dependencies',
  491. 'action' => 'requiredParam',
  492. 'pass' => ['one'],
  493. ],
  494. ]);
  495. $controller = $this->factory->create($request);
  496. $result = $this->factory->invoke($controller);
  497. $data = json_decode((string)$result->getBody());
  498. $this->assertNotNull($data);
  499. $this->assertSame($data->one, 'one');
  500. }
  501. public function testInvokeInjectParametersRequiredWithPassedParameters(): void
  502. {
  503. $this->container->add(stdClass::class, json_decode('{"key":"value"}'));
  504. $request = new ServerRequest([
  505. 'url' => 'test_plugin_three/dependencies/requiredDep',
  506. 'params' => [
  507. 'plugin' => null,
  508. 'controller' => 'Dependencies',
  509. 'action' => 'requiredDep',
  510. 'pass' => ['one', 'two'],
  511. ],
  512. ]);
  513. $controller = $this->factory->create($request);
  514. $result = $this->factory->invoke($controller);
  515. $data = json_decode((string)$result->getBody());
  516. $this->assertNotNull($data);
  517. $this->assertSame($data->any, 'one');
  518. $this->assertSame($data->str, 'two');
  519. $this->assertSame('value', $data->dep->key);
  520. }
  521. /**
  522. * Test that routing parameters are passed into variadic controller functions
  523. */
  524. public function testInvokeInjectPassedParametersVariadic(): void
  525. {
  526. $request = new ServerRequest([
  527. 'url' => 'test_plugin_three/dependencies/variadic',
  528. 'params' => [
  529. 'plugin' => null,
  530. 'controller' => 'Dependencies',
  531. 'action' => 'variadic',
  532. 'pass' => ['one', 'two'],
  533. ],
  534. ]);
  535. $controller = $this->factory->create($request);
  536. $result = $this->factory->invoke($controller);
  537. $data = json_decode((string)$result->getBody());
  538. $this->assertNotNull($data);
  539. $this->assertSame(['one', 'two'], $data->args);
  540. }
  541. /**
  542. * Test that routing parameters are passed into controller action using spread operator
  543. */
  544. public function testInvokeInjectPassedParametersSpread(): void
  545. {
  546. $request = new ServerRequest([
  547. 'url' => 'test_plugin_three/dependencies/spread',
  548. 'params' => [
  549. 'plugin' => null,
  550. 'controller' => 'Dependencies',
  551. 'action' => 'spread',
  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(['one', 'two'], $data->args);
  560. }
  561. /**
  562. * Test that routing parameters are passed into controller action using spread operator
  563. */
  564. public function testInvokeInjectPassedParametersSpreadNoParams(): void
  565. {
  566. $request = new ServerRequest([
  567. 'url' => 'test_plugin_three/dependencies/spread',
  568. 'params' => [
  569. 'plugin' => null,
  570. 'controller' => 'Dependencies',
  571. 'action' => 'spread',
  572. 'pass' => [],
  573. ],
  574. ]);
  575. $controller = $this->factory->create($request);
  576. $result = $this->factory->invoke($controller);
  577. $data = json_decode((string)$result->getBody());
  578. $this->assertNotNull($data);
  579. $this->assertSame([], $data->args);
  580. }
  581. /**
  582. * Test that default parameters work for controller methods
  583. */
  584. public function testInvokeOptionalStringParam(): void
  585. {
  586. $request = new ServerRequest([
  587. 'url' => 'test_plugin_three/dependencies/optionalString',
  588. 'params' => [
  589. 'plugin' => null,
  590. 'controller' => 'Dependencies',
  591. 'action' => 'optionalString',
  592. ],
  593. ]);
  594. $controller = $this->factory->create($request);
  595. $result = $this->factory->invoke($controller);
  596. $data = json_decode((string)$result->getBody());
  597. $this->assertNotNull($data);
  598. $this->assertSame('default val', $data->str);
  599. }
  600. /**
  601. * Test that required strings a default value.
  602. */
  603. public function testInvokeRequiredStringParam(): void
  604. {
  605. $request = new ServerRequest([
  606. 'url' => 'test_plugin_three/dependencies/requiredString',
  607. 'params' => [
  608. 'plugin' => null,
  609. 'controller' => 'Dependencies',
  610. 'action' => 'requiredString',
  611. ],
  612. ]);
  613. $controller = $this->factory->create($request);
  614. $this->expectException(InvalidParameterException::class);
  615. $this->expectExceptionMessage('Missing passed parameter for `str` in action Dependencies::requiredString()');
  616. $this->factory->invoke($controller);
  617. }
  618. /**
  619. * Test that coercing string to float, int and bool params
  620. */
  621. public function testInvokePassedParametersCoercion(): void
  622. {
  623. $request = new ServerRequest([
  624. 'url' => 'test_plugin_three/dependencies/requiredTyped',
  625. 'params' => [
  626. 'plugin' => null,
  627. 'controller' => 'Dependencies',
  628. 'action' => 'requiredTyped',
  629. 'pass' => ['1.0', '2', '0'],
  630. ],
  631. ]);
  632. $controller = $this->factory->create($request);
  633. $result = $this->factory->invoke($controller);
  634. $data = json_decode((string)$result->getBody(), true);
  635. $this->assertNotNull($data);
  636. $this->assertSame(['one' => 1.0, 'two' => 2, 'three' => false], $data);
  637. }
  638. /**
  639. * Test that default values work for typed parameters
  640. */
  641. public function testInvokeOptionalTypedParam(): void
  642. {
  643. $request = new ServerRequest([
  644. 'url' => 'test_plugin_three/dependencies/optionalTyped',
  645. 'params' => [
  646. 'plugin' => null,
  647. 'controller' => 'Dependencies',
  648. 'action' => 'optionalTyped',
  649. 'pass' => ['1.0'],
  650. ],
  651. ]);
  652. $controller = $this->factory->create($request);
  653. $result = $this->factory->invoke($controller);
  654. $data = json_decode((string)$result->getBody(), true);
  655. $this->assertNotNull($data);
  656. $this->assertSame(['one' => 1.0, 'two' => 2, 'three' => true], $data);
  657. }
  658. /**
  659. * Test using invalid value for supported type
  660. */
  661. public function testInvokePassedParametersUnsupportedFloatCoercion(): void
  662. {
  663. $request = new ServerRequest([
  664. 'url' => 'test_plugin_three/dependencies/requiredTyped',
  665. 'params' => [
  666. 'plugin' => null,
  667. 'controller' => 'Dependencies',
  668. 'action' => 'requiredTyped',
  669. 'pass' => ['true', '2', '1'],
  670. ],
  671. ]);
  672. $controller = $this->factory->create($request);
  673. $this->expectException(InvalidParameterException::class);
  674. $this->expectExceptionMessage('Unable to coerce "true" to `float` for `one` in action Dependencies::requiredTyped()');
  675. $this->factory->invoke($controller);
  676. }
  677. /**
  678. * Test using invalid value for supported type
  679. */
  680. public function testInvokePassedParametersUnsupportedIntCoercion(): void
  681. {
  682. $request = new ServerRequest([
  683. 'url' => 'test_plugin_three/dependencies/requiredTyped',
  684. 'params' => [
  685. 'plugin' => null,
  686. 'controller' => 'Dependencies',
  687. 'action' => 'requiredTyped',
  688. 'pass' => ['1', '2.0', '1'],
  689. ],
  690. ]);
  691. $controller = $this->factory->create($request);
  692. $this->expectException(InvalidParameterException::class);
  693. $this->expectExceptionMessage('Unable to coerce "2.0" to `int` for `two` in action Dependencies::requiredTyped()');
  694. $this->factory->invoke($controller);
  695. }
  696. /**
  697. * Test using invalid value for supported type
  698. */
  699. public function testInvokePassedParametersUnsupportedBoolCoercion(): void
  700. {
  701. $request = new ServerRequest([
  702. 'url' => 'test_plugin_three/dependencies/requiredTyped',
  703. 'params' => [
  704. 'plugin' => null,
  705. 'controller' => 'Dependencies',
  706. 'action' => 'requiredTyped',
  707. 'pass' => ['1', '1', 'true'],
  708. ],
  709. ]);
  710. $controller = $this->factory->create($request);
  711. $this->expectException(InvalidParameterException::class);
  712. $this->expectExceptionMessage('Unable to coerce "true" to `bool` for `three` in action Dependencies::requiredTyped()');
  713. $this->factory->invoke($controller);
  714. }
  715. /**
  716. * Test using an unsupported type.
  717. */
  718. public function testInvokePassedParamUnsupportedType(): void
  719. {
  720. $request = new ServerRequest([
  721. 'url' => 'test_plugin_three/dependencies/unsupportedTyped',
  722. 'params' => [
  723. 'plugin' => null,
  724. 'controller' => 'Dependencies',
  725. 'action' => 'unsupportedTyped',
  726. 'pass' => ['test'],
  727. ],
  728. ]);
  729. $controller = $this->factory->create($request);
  730. $this->expectException(InvalidParameterException::class);
  731. $this->expectExceptionMessage('Unable to coerce "test" to `array` for `one` in action Dependencies::unsupportedTyped()');
  732. $this->factory->invoke($controller);
  733. }
  734. public function testMiddleware(): void
  735. {
  736. $request = new ServerRequest([
  737. 'url' => 'posts',
  738. 'params' => [
  739. 'controller' => 'Posts',
  740. 'action' => 'index',
  741. 'pass' => [],
  742. ],
  743. ]);
  744. $controller = $this->factory->create($request);
  745. $this->factory->invoke($controller);
  746. $request = $controller->getRequest();
  747. $this->assertTrue($request->getAttribute('for-all'));
  748. $this->assertTrue($request->getAttribute('index-only'));
  749. $this->assertNull($request->getAttribute('all-except-index'));
  750. $request = new ServerRequest([
  751. 'url' => 'posts/get',
  752. 'params' => [
  753. 'controller' => 'Posts',
  754. 'action' => 'get',
  755. 'pass' => [],
  756. ],
  757. ]);
  758. $controller = $this->factory->create($request);
  759. $this->factory->invoke($controller);
  760. $request = $controller->getRequest();
  761. $this->assertTrue($request->getAttribute('for-all'));
  762. $this->assertNull($request->getAttribute('index-only'));
  763. $this->assertTrue($request->getAttribute('all-except-index'));
  764. }
  765. }