xref: /PHP-8.2/Zend/tests/closure_compare.phpt (revision 6a9daafe)
1--TEST--
2Closure comparison
3--FILE--
4<?php
5function foo() {
6    static $var;
7}
8
9$closures[0] = Closure::fromCallable('foo');
10$closures[1] = Closure::fromCallable('foo');
11
12printf("foo == foo: %s\n", $closures[0] == $closures[1] ? "OK" : "FAIL");
13
14$closures[0] = Closure::fromCallable('strlen');
15$closures[1] = Closure::fromCallable('strlen');
16
17printf("strlen == strlen: %s\n", $closures[0] == $closures[1] ? "OK" : "FAIL");
18
19$closures[0] = Closure::fromCallable('strlen');
20$closures[1] = Closure::fromCallable('strrev');
21
22printf("strlen != strrev: %s\n", $closures[0] != $closures[1] ? "OK" : "FAIL");
23
24trait MethodTrait {
25    public function traitMethod(){}
26}
27
28class 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
42class Bar extends Foo {}
43
44class Baz {
45    use MethodTrait;
46}
47
48$closures[0] = Closure::fromCallable([Foo::class, "existsStatic"]);
49$closures[1] = Closure::fromCallable([Bar::class, "existsStatic"]);
50
51printf("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
58printf("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
65printf("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
70printf("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
75printf("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
80printf("foo::method == foo::method: %s\n", $closures[0] == $closures[1] ? "OK" : "FAIL");
81
82$closures[1] = $closures[1]->bindTo(new Bar);
83
84printf("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
89printf("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
94printf("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
99printf("Closure[0]::invoke == Closure[0]::invoke: %s\n", $closures[2] == $closures[3] ? "OK" : "FAIL");
100?>
101--EXPECT--
102foo == foo: OK
103strlen == strlen: OK
104strlen != strrev: OK
105foo::existsStatic != bar::existsStatic: OK
106foo#0::exists != foo#1::exists: OK
107foo::traitMethod != baz::traitMethod: OK
108foo::traitMethod != foo::aliasMethod: OK
109foo::exists == foo::exists: OK
110foo::method == foo::method: OK
111foo::method != bar::method: OK
112foo::method != foo::method2: OK
113Closure[0]::invoke != Closure[1]::invoke: OK
114Closure[0]::invoke == Closure[0]::invoke: OK
115