ControllerFactoryTest.php 29 KB

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