ArgumentsResolverTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. $this->expectException(\RuntimeException::class);
  109. $this->expectExceptionMessage('requires a value for the "$route" argument.');
  110. $resolver->getArguments($callable);
  111. }
  112. /**
  113. * Tests getArgument() with a named parameter with no typehint and a value.
  114. *
  115. * Without the typehint, passing a value to a named parameter will still
  116. * receive the provided value.
  117. */
  118. public function testGetArgumentRouteNoTypehintAndValue() {
  119. $scalars = ['route' => 'foo'];
  120. $resolver = new ArgumentsResolver($scalars, [], []);
  121. $callable = function ($route) {};
  122. $arguments = $resolver->getArguments($callable);
  123. $this->assertSame(['foo'], $arguments);
  124. }
  125. /**
  126. * Tests handleUnresolvedArgument() for a scalar argument.
  127. */
  128. public function testHandleNotUpcastedArgument() {
  129. $objects = ['foo' => 'bar'];
  130. $scalars = ['foo' => 'baz'];
  131. $resolver = new ArgumentsResolver($scalars, $objects, []);
  132. $callable = function (\stdClass $foo) {};
  133. $this->expectException(\RuntimeException::class);
  134. $this->expectExceptionMessage('requires a value for the "$foo" argument.');
  135. $resolver->getArguments($callable);
  136. }
  137. /**
  138. * Tests handleUnresolvedArgument() for missing arguments.
  139. *
  140. * @dataProvider providerTestHandleUnresolvedArgument
  141. */
  142. public function testHandleUnresolvedArgument($callable) {
  143. $resolver = new ArgumentsResolver([], [], []);
  144. $this->expectException(\RuntimeException::class);
  145. $this->expectExceptionMessage('requires a value for the "$foo" argument.');
  146. $resolver->getArguments($callable);
  147. }
  148. /**
  149. * Provides test data to testHandleUnresolvedArgument().
  150. */
  151. public function providerTestHandleUnresolvedArgument() {
  152. $data = [];
  153. $data[] = [function ($foo) {}];
  154. $data[] = [[new TestClass(), 'access']];
  155. $data[] = ['Drupal\Tests\Component\Utility\test_access_arguments_resolver_access'];
  156. return $data;
  157. }
  158. }
  159. /**
  160. * Provides a test class.
  161. */
  162. class TestClass {
  163. public function access($foo) {
  164. }
  165. }
  166. /**
  167. * Provides a test interface.
  168. */
  169. interface Test1Interface {
  170. }
  171. /**
  172. * Provides a different test interface.
  173. */
  174. interface Test2Interface {
  175. }
  176. function test_access_arguments_resolver_access($foo) {
  177. }