closure_compare.phpt 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. --TEST--
  2. Closure comparison
  3. --FILE--
  4. <?php
  5. function foo() {
  6. static $var;
  7. }
  8. $closures[0] = Closure::fromCallable('foo');
  9. $closures[1] = Closure::fromCallable('foo');
  10. printf("foo == foo: %s\n", $closures[0] == $closures[1] ? "OK" : "FAIL");
  11. $closures[0] = Closure::fromCallable('strlen');
  12. $closures[1] = Closure::fromCallable('strlen');
  13. printf("strlen == strlen: %s\n", $closures[0] == $closures[1] ? "OK" : "FAIL");
  14. $closures[0] = Closure::fromCallable('strlen');
  15. $closures[1] = Closure::fromCallable('strrev');
  16. printf("strlen != strrev: %s\n", $closures[0] != $closures[1] ? "OK" : "FAIL");
  17. trait MethodTrait {
  18. public function traitMethod(){}
  19. }
  20. class Foo {
  21. use MethodTrait {
  22. MethodTrait::traitMethod as aliasMethod;
  23. }
  24. public function __call($method, $args) {
  25. }
  26. public function exists() {}
  27. public static function existsStatic() {}
  28. }
  29. class Bar extends Foo {}
  30. class Baz {
  31. use MethodTrait;
  32. }
  33. $closures[0] = Closure::fromCallable([Foo::class, "existsStatic"]);
  34. $closures[1] = Closure::fromCallable([Bar::class, "existsStatic"]);
  35. printf("foo::existsStatic != bar::existsStatic: %s\n", $closures[0] != $closures[1] ? "OK" : "FAIL");
  36. $foo = new Foo;
  37. $closures[0] = Closure::fromCallable([$foo, "exists"]);
  38. $closures[1] = $closures[0]->bindTo(new Foo);
  39. printf("foo#0::exists != foo#1::exists: %s\n", $closures[0] != $closures[1] ? "OK" : "FAIL");
  40. $baz = new Baz;
  41. $closures[0] = Closure::fromCallable([$foo, "traitMethod"]);
  42. $closures[1] = Closure::fromCallable([$baz, "traitMethod"]);
  43. printf("foo::traitMethod != baz::traitMethod: %s\n", $closures[0] != $closures[1] ? "OK" : "FAIL");
  44. $closures[0] = Closure::fromCallable([$foo, "traitMethod"]);
  45. $closures[1] = Closure::fromCallable([$foo, "aliasMethod"]);
  46. printf("foo::traitMethod != foo::aliasMethod: %s\n", $closures[0] != $closures[1] ? "OK" : "FAIL");
  47. $closures[0] = Closure::fromCallable([$foo, "exists"]);
  48. $closures[1] = Closure::fromCallable([$foo, "exists"]);
  49. printf("foo::exists == foo::exists: %s\n", $closures[0] == $closures[1] ? "OK" : "FAIL");
  50. $closures[0] = Closure::fromCallable([$foo, "method"]);
  51. $closures[1] = Closure::fromCallable([$foo, "method"]);
  52. printf("foo::method == foo::method: %s\n", $closures[0] == $closures[1] ? "OK" : "FAIL");
  53. $closures[1] = $closures[1]->bindTo(new Bar);
  54. printf("foo::method != bar::method: %s\n", $closures[0] != $closures[1] ? "OK" : "FAIL");
  55. $closures[0] = Closure::fromCallable([$foo, "method"]);
  56. $closures[1] = Closure::fromCallable([$foo, "method2"]);
  57. printf("foo::method != foo::method2: %s\n", $closures[0] != $closures[1] ? "OK" : "FAIL");
  58. $closures[2] = Closure::fromCallable([$closures[0], "__invoke"]);
  59. $closures[3] = Closure::fromCallable([$closures[1], "__invoke"]);
  60. printf("Closure[0]::invoke != Closure[1]::invoke: %s\n", $closures[2] != $closures[3] ? "OK" : "FAIL");
  61. $closures[2] = Closure::fromCallable([$closures[0], "__invoke"]);
  62. $closures[3] = Closure::fromCallable([$closures[0], "__invoke"]);
  63. printf("Closure[0]::invoke == Closure[0]::invoke: %s\n", $closures[2] == $closures[3] ? "OK" : "FAIL");
  64. ?>
  65. --EXPECT--
  66. foo == foo: OK
  67. strlen == strlen: OK
  68. strlen != strrev: OK
  69. foo::existsStatic != bar::existsStatic: OK
  70. foo#0::exists != foo#1::exists: OK
  71. foo::traitMethod != baz::traitMethod: OK
  72. foo::traitMethod != foo::aliasMethod: OK
  73. foo::exists == foo::exists: OK
  74. foo::method == foo::method: OK
  75. foo::method != bar::method: OK
  76. foo::method != foo::method2: OK
  77. Closure[0]::invoke != Closure[1]::invoke: OK
  78. Closure[0]::invoke == Closure[0]::invoke: OK