ArgumentsResolverTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\Tests\Component\Utility\ArgumentsResolverTest.
  5. */
  6. namespace Drupal\Tests\Component\Utility;
  7. use Drupal\Component\Utility\ArgumentsResolver;
  8. use PHPUnit\Framework\TestCase;
  9. /**
  10. * @coversDefaultClass \Drupal\Component\Utility\ArgumentsResolver
  11. * @group Access
  12. */
  13. class ArgumentsResolverTest extends TestCase {
  14. /**
  15. * {@inheritdoc}
  16. */
  17. protected function setUp() {
  18. parent::setUp();
  19. }
  20. /**
  21. * Tests the getArgument() method.
  22. *
  23. * @dataProvider providerTestGetArgument
  24. */
  25. public function testGetArgument($callable, $scalars, $objects, $wildcards, $expected) {
  26. $arguments = (new ArgumentsResolver($scalars, $objects, $wildcards))->getArguments($callable);
  27. $this->assertSame($expected, $arguments);
  28. }
  29. /**
  30. * Provides test data to testGetArgument().
  31. */
  32. public function providerTestGetArgument() {
  33. $data = [];
  34. // Test an optional parameter with no provided value.
  35. $data[] = [
  36. function ($foo = 'foo') {}, [], [], [] , ['foo'],
  37. ];
  38. // Test an optional parameter with a provided value.
  39. $data[] = [
  40. function ($foo = 'foo') {}, ['foo' => 'bar'], [], [], ['bar'],
  41. ];
  42. // Test with a provided value.
  43. $data[] = [
  44. function ($foo) {}, ['foo' => 'bar'], [], [], ['bar'],
  45. ];
  46. // Test with an explicitly NULL value.
  47. $data[] = [
  48. function ($foo) {}, [], ['foo' => NULL], [], [NULL],
  49. ];
  50. // Test with a raw value that overrides the provided upcast value, since
  51. // it is not typehinted.
  52. $scalars = ['foo' => 'baz'];
  53. $objects = ['foo' => new \stdClass()];
  54. $data[] = [
  55. function ($foo) {}, $scalars, $objects, [], ['baz'],
  56. ];
  57. return $data;
  58. }
  59. /**
  60. * Tests getArgument() with an object.
  61. */
  62. public function testGetArgumentObject() {
  63. $callable = function (\stdClass $object) {};
  64. $object = new \stdClass();
  65. $arguments = (new ArgumentsResolver([], ['object' => $object], []))->getArguments($callable);
  66. $this->assertSame([$object], $arguments);
  67. }
  68. /**
  69. * Tests getArgument() with a wildcard object for a parameter with a custom name.
  70. */
  71. public function testGetWildcardArgument() {
  72. $callable = function (\stdClass $custom_name) {};
  73. $object = new \stdClass();
  74. $arguments = (new ArgumentsResolver([], [], [$object]))->getArguments($callable);
  75. $this->assertSame([$object], $arguments);
  76. }
  77. /**
  78. * Tests getArgument() with a Route, Request, and Account object.
  79. */
  80. public function testGetArgumentOrder() {
  81. $a1 = $this->getMockBuilder('\Drupal\Tests\Component\Utility\Test1Interface')->getMock();
  82. $a2 = $this->getMockBuilder('\Drupal\Tests\Component\Utility\TestClass')->getMock();
  83. $a3 = $this->getMockBuilder('\Drupal\Tests\Component\Utility\Test2Interface')->getMock();
  84. $objects = [
  85. 't1' => $a1,
  86. 'tc' => $a2,
  87. ];
  88. $wildcards = [$a3];
  89. $resolver = new ArgumentsResolver([], $objects, $wildcards);
  90. $callable = function (Test1Interface $t1, TestClass $tc, Test2Interface $t2) {};
  91. $arguments = $resolver->getArguments($callable);
  92. $this->assertSame([$a1, $a2, $a3], $arguments);
  93. // Test again, but with the arguments in a different order.
  94. $callable = function (Test2Interface $t2, TestClass $tc, Test1Interface $t1) {};
  95. $arguments = $resolver->getArguments($callable);
  96. $this->assertSame([$a3, $a2, $a1], $arguments);
  97. }
  98. /**
  99. * Tests getArgument() with a wildcard parameter with no typehint.
  100. *
  101. * Without the typehint, the wildcard object will not be passed to the callable.
  102. */
  103. public function testGetWildcardArgumentNoTypehint() {
  104. $a = $this->getMockBuilder('\Drupal\Tests\Component\Utility\Test1Interface')->getMock();
  105. $wildcards = [$a];
  106. $resolver = new ArgumentsResolver([], [], $wildcards);
  107. $callable = function ($route) {};
  108. if (method_exists($this, 'expectException')) {
  109. $this->expectException(\RuntimeException::class);
  110. $this->expectExceptionMessage('requires a value for the "$route" argument.');
  111. }
  112. else {
  113. $this->setExpectedException(\RuntimeException::class, 'requires a value for the "$route" argument.');
  114. }
  115. $resolver->getArguments($callable);
  116. }
  117. /**
  118. * Tests getArgument() with a named parameter with no typehint and a value.
  119. *
  120. * Without the typehint, passing a value to a named parameter will still
  121. * receive the provided value.
  122. */
  123. public function testGetArgumentRouteNoTypehintAndValue() {
  124. $scalars = ['route' => 'foo'];
  125. $resolver = new ArgumentsResolver($scalars, [], []);
  126. $callable = function ($route) {};
  127. $arguments = $resolver->getArguments($callable);
  128. $this->assertSame(['foo'], $arguments);
  129. }
  130. /**
  131. * Tests handleUnresolvedArgument() for a scalar argument.
  132. */
  133. public function testHandleNotUpcastedArgument() {
  134. $objects = ['foo' => 'bar'];
  135. $scalars = ['foo' => 'baz'];
  136. $resolver = new ArgumentsResolver($scalars, $objects, []);
  137. $callable = function (\stdClass $foo) {};
  138. if (method_exists($this, 'expectException')) {
  139. $this->expectException(\RuntimeException::class);
  140. $this->expectExceptionMessage('requires a value for the "$foo" argument.');
  141. }
  142. else {
  143. $this->setExpectedException(\RuntimeException::class, 'requires a value for the "$foo" argument.');
  144. }
  145. $resolver->getArguments($callable);
  146. }
  147. /**
  148. * Tests handleUnresolvedArgument() for missing arguments.
  149. *
  150. * @dataProvider providerTestHandleUnresolvedArgument
  151. */
  152. public function testHandleUnresolvedArgument($callable) {
  153. $resolver = new ArgumentsResolver([], [], []);
  154. if (method_exists($this, 'expectException')) {
  155. $this->expectException(\RuntimeException::class);
  156. $this->expectExceptionMessage('requires a value for the "$foo" argument.');
  157. }
  158. else {
  159. $this->setExpectedException(\RuntimeException::class, 'requires a value for the "$foo" argument.');
  160. }
  161. $resolver->getArguments($callable);
  162. }
  163. /**
  164. * Provides test data to testHandleUnresolvedArgument().
  165. */
  166. public function providerTestHandleUnresolvedArgument() {
  167. $data = [];
  168. $data[] = [function ($foo) {}];
  169. $data[] = [[new TestClass(), 'access']];
  170. $data[] = ['Drupal\Tests\Component\Utility\test_access_arguments_resolver_access'];
  171. return $data;
  172. }
  173. }
  174. /**
  175. * Provides a test class.
  176. */
  177. class TestClass {
  178. public function access($foo) {
  179. }
  180. }
  181. /**
  182. * Provides a test interface.
  183. */
  184. interface Test1Interface {
  185. }
  186. /**
  187. * Provides a different test interface.
  188. */
  189. interface Test2Interface {
  190. }
  191. function test_access_arguments_resolver_access($foo) {
  192. }