ControllerFactoryTest.php 31 KB

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