ForwardsCallsTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace Illuminate\Tests\Support;
  3. use BadMethodCallException;
  4. use Error;
  5. use Illuminate\Support\Traits\ForwardsCalls;
  6. use PHPUnit\Framework\TestCase;
  7. class ForwardsCallsTest extends TestCase
  8. {
  9. public function testForwardsCalls()
  10. {
  11. $results = (new ForwardsCallsOne)->forwardedTwo('foo', 'bar');
  12. $this->assertEquals(['foo', 'bar'], $results);
  13. }
  14. public function testNestedForwardCalls()
  15. {
  16. $results = (new ForwardsCallsOne)->forwardedBase('foo', 'bar');
  17. $this->assertEquals(['foo', 'bar'], $results);
  18. }
  19. public function testMissingForwardedCallThrowsCorrectError()
  20. {
  21. $this->expectException(BadMethodCallException::class);
  22. $this->expectExceptionMessage('Call to undefined method Illuminate\Tests\Support\ForwardsCallsOne::missingMethod()');
  23. (new ForwardsCallsOne)->missingMethod('foo', 'bar');
  24. }
  25. public function testMissingAlphanumericForwardedCallThrowsCorrectError()
  26. {
  27. $this->expectException(BadMethodCallException::class);
  28. $this->expectExceptionMessage('Call to undefined method Illuminate\Tests\Support\ForwardsCallsOne::this1_shouldWork_too()');
  29. (new ForwardsCallsOne)->this1_shouldWork_too('foo', 'bar');
  30. }
  31. public function testNonForwardedErrorIsNotTamperedWith()
  32. {
  33. $this->expectException(Error::class);
  34. $this->expectExceptionMessage('Call to undefined method Illuminate\Tests\Support\ForwardsCallsBase::missingMethod()');
  35. (new ForwardsCallsOne)->baseError('foo', 'bar');
  36. }
  37. public function testThrowBadMethodCallException()
  38. {
  39. $this->expectException(BadMethodCallException::class);
  40. $this->expectExceptionMessage('Call to undefined method Illuminate\Tests\Support\ForwardsCallsOne::test()');
  41. (new ForwardsCallsOne)->throwTestException('test');
  42. }
  43. }
  44. class ForwardsCallsOne
  45. {
  46. use ForwardsCalls;
  47. public function __call($method, $parameters)
  48. {
  49. return $this->forwardCallTo(new ForwardsCallsTwo, $method, $parameters);
  50. }
  51. public function throwTestException($method)
  52. {
  53. static::throwBadMethodCallException($method);
  54. }
  55. }
  56. class ForwardsCallsTwo
  57. {
  58. use ForwardsCalls;
  59. public function __call($method, $parameters)
  60. {
  61. return $this->forwardCallTo(new ForwardsCallsBase, $method, $parameters);
  62. }
  63. public function forwardedTwo(...$parameters)
  64. {
  65. return $parameters;
  66. }
  67. }
  68. class ForwardsCallsBase
  69. {
  70. public function forwardedBase(...$parameters)
  71. {
  72. return $parameters;
  73. }
  74. public function baseError()
  75. {
  76. return $this->missingMethod();
  77. }
  78. }