|
| 1 | +--TEST-- |
| 2 | +Closure comparison |
| 3 | +--FILE-- |
| 4 | +<?php |
| 5 | +function foo() { |
| 6 | + static $var; |
| 7 | +} |
| 8 | + |
| 9 | +$closures[0] = Closure::fromCallable('foo'); |
| 10 | +$closures[1] = Closure::fromCallable('foo'); |
| 11 | + |
| 12 | +printf("foo == foo: %s\n", $closures[0] == $closures[1] ? "OK" : "FAIL"); |
| 13 | + |
| 14 | +$closures[0] = Closure::fromCallable('strlen'); |
| 15 | +$closures[1] = Closure::fromCallable('strlen'); |
| 16 | + |
| 17 | +printf("strlen == strlen: %s\n", $closures[0] == $closures[1] ? "OK" : "FAIL"); |
| 18 | + |
| 19 | +$closures[0] = Closure::fromCallable('strlen'); |
| 20 | +$closures[1] = Closure::fromCallable('strrev'); |
| 21 | + |
| 22 | +printf("strlen != strrev: %s\n", $closures[0] != $closures[1] ? "OK" : "FAIL"); |
| 23 | + |
| 24 | +trait MethodTrait { |
| 25 | + public function traitMethod(){} |
| 26 | +} |
| 27 | + |
| 28 | +class Foo { |
| 29 | + use MethodTrait { |
| 30 | + MethodTrait::traitMethod as aliasMethod; |
| 31 | + } |
| 32 | + |
| 33 | + public function __call($method, $args) { |
| 34 | + |
| 35 | + } |
| 36 | + |
| 37 | + public function exists() {} |
| 38 | + |
| 39 | + public static function existsStatic() {} |
| 40 | +} |
| 41 | + |
| 42 | +class Bar extends Foo {} |
| 43 | + |
| 44 | +class Baz { |
| 45 | + use MethodTrait; |
| 46 | +} |
| 47 | + |
| 48 | +$closures[0] = Closure::fromCallable([Foo::class, "existsStatic"]); |
| 49 | +$closures[1] = Closure::fromCallable([Bar::class, "existsStatic"]); |
| 50 | + |
| 51 | +printf("foo::existsStatic != bar::existsStatic: %s\n", $closures[0] != $closures[1] ? "OK" : "FAIL"); |
| 52 | + |
| 53 | +$foo = new Foo; |
| 54 | + |
| 55 | +$closures[0] = Closure::fromCallable([$foo, "exists"]); |
| 56 | +$closures[1] = $closures[0]->bindTo(new Foo); |
| 57 | + |
| 58 | +printf("foo#0::exists != foo#1::exists: %s\n", $closures[0] != $closures[1] ? "OK" : "FAIL"); |
| 59 | + |
| 60 | +$baz = new Baz; |
| 61 | + |
| 62 | +$closures[0] = Closure::fromCallable([$foo, "traitMethod"]); |
| 63 | +$closures[1] = Closure::fromCallable([$baz, "traitMethod"]); |
| 64 | + |
| 65 | +printf("foo::traitMethod != baz::traitMethod: %s\n", $closures[0] != $closures[1] ? "OK" : "FAIL"); |
| 66 | + |
| 67 | +$closures[0] = Closure::fromCallable([$foo, "traitMethod"]); |
| 68 | +$closures[1] = Closure::fromCallable([$foo, "aliasMethod"]); |
| 69 | + |
| 70 | +printf("foo::traitMethod != foo::aliasMethod: %s\n", $closures[0] != $closures[1] ? "OK" : "FAIL"); |
| 71 | + |
| 72 | +$closures[0] = Closure::fromCallable([$foo, "exists"]); |
| 73 | +$closures[1] = Closure::fromCallable([$foo, "exists"]); |
| 74 | + |
| 75 | +printf("foo::exists == foo::exists: %s\n", $closures[0] == $closures[1] ? "OK" : "FAIL"); |
| 76 | + |
| 77 | +$closures[0] = Closure::fromCallable([$foo, "method"]); |
| 78 | +$closures[1] = Closure::fromCallable([$foo, "method"]); |
| 79 | + |
| 80 | +printf("foo::method == foo::method: %s\n", $closures[0] == $closures[1] ? "OK" : "FAIL"); |
| 81 | + |
| 82 | +$closures[1] = $closures[1]->bindTo(new Bar); |
| 83 | + |
| 84 | +printf("foo::method != bar::method: %s\n", $closures[0] != $closures[1] ? "OK" : "FAIL"); |
| 85 | + |
| 86 | +$closures[0] = Closure::fromCallable([$foo, "method"]); |
| 87 | +$closures[1] = Closure::fromCallable([$foo, "method2"]); |
| 88 | + |
| 89 | +printf("foo::method != foo::method2: %s\n", $closures[0] != $closures[1] ? "OK" : "FAIL"); |
| 90 | + |
| 91 | +$closures[2] = Closure::fromCallable([$closures[0], "__invoke"]); |
| 92 | +$closures[3] = Closure::fromCallable([$closures[1], "__invoke"]); |
| 93 | + |
| 94 | +printf("Closure[0]::invoke != Closure[1]::invoke: %s\n", $closures[2] != $closures[3] ? "OK" : "FAIL"); |
| 95 | + |
| 96 | +$closures[2] = Closure::fromCallable([$closures[0], "__invoke"]); |
| 97 | +$closures[3] = Closure::fromCallable([$closures[0], "__invoke"]); |
| 98 | + |
| 99 | +printf("Closure[0]::invoke == Closure[0]::invoke: %s\n", $closures[2] == $closures[3] ? "OK" : "FAIL"); |
| 100 | +?> |
| 101 | +--EXPECT-- |
| 102 | +foo == foo: OK |
| 103 | +strlen == strlen: OK |
| 104 | +strlen != strrev: OK |
| 105 | +foo::existsStatic != bar::existsStatic: OK |
| 106 | +foo#0::exists != foo#1::exists: OK |
| 107 | +foo::traitMethod != baz::traitMethod: OK |
| 108 | +foo::traitMethod != foo::aliasMethod: OK |
| 109 | +foo::exists == foo::exists: OK |
| 110 | +foo::method == foo::method: OK |
| 111 | +foo::method != bar::method: OK |
| 112 | +foo::method != foo::method2: OK |
| 113 | +Closure[0]::invoke != Closure[1]::invoke: OK |
| 114 | +Closure[0]::invoke == Closure[0]::invoke: OK |
0 commit comments