forked from Respect/Stringifier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCallableStringifierTest.php
More file actions
223 lines (192 loc) · 7.77 KB
/
CallableStringifierTest.php
File metadata and controls
223 lines (192 loc) · 7.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
/*
* This file is part of Respect/Stringifier.
* Copyright (c) Henrique Moody <henriquemoody@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
namespace Respect\Stringifier\Test\Unit\Stringifiers;
use Countable;
use DateTime;
use Iterator;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Respect\Stringifier\Stringifiers\CallableStringifier;
use Respect\Stringifier\Test\Double\FakeQuoter;
use Respect\Stringifier\Test\Double\FakeStringifier;
use function array_sum;
use function sprintf;
#[CoversClass(CallableStringifier::class)]
final class CallableStringifierTest extends TestCase
{
private const int DEPTH = 0;
#[Test]
public function itShouldNotStringifyWhenRawValueIsNotCallable(): void
{
$sut = new CallableStringifier(new FakeStringifier(), new FakeQuoter(), closureOnly: false);
self::assertNull($sut->stringify(1, self::DEPTH));
}
#[Test]
#[DataProvider('closureRawValuesProvider')]
public function itShouldStringifyWhenRawValueIsClosure(callable $raw, string $expectedWithoutQuotes): void
{
$quoter = new FakeQuoter();
$sut = new CallableStringifier(new FakeStringifier(), $quoter);
$actual = $sut->stringify($raw, self::DEPTH);
$expected = $quoter->quote($expectedWithoutQuotes, self::DEPTH);
self::assertEquals($expected, $actual);
}
#[Test]
#[DataProvider('nonClosureCallableRawValuesProvider')]
public function itShouldNotStringifyNonClosureCallableByDefault(callable $raw, string $useless): void
{
$sut = new CallableStringifier(new FakeStringifier(), new FakeQuoter());
self::assertNull($sut->stringify($raw, self::DEPTH));
}
#[Test]
#[DataProvider('nonClosureCallableRawValuesProvider')]
public function itShouldStringifyNonClosureCallableWhenClosureOnlyIsFalse(
callable $raw,
string $expectedWithoutQuotes,
): void {
$quoter = new FakeQuoter();
$sut = new CallableStringifier(new FakeStringifier(), $quoter, closureOnly: false);
$actual = $sut->stringify($raw, self::DEPTH);
$expected = $quoter->quote($expectedWithoutQuotes, self::DEPTH);
self::assertEquals($expected, $actual);
}
#[Test]
public function itShouldStringifyWhenRawValueIsCallableWithDefaultValues(): void
{
$raw = static fn(int $value = 1): int => $value;
$stringifier = new FakeStringifier();
$quoter = new FakeQuoter();
$sut = new CallableStringifier($stringifier, $quoter);
$actual = $sut->stringify($raw, self::DEPTH);
$expected = $quoter->quote(
sprintf('Closure { static fn(int $value = %s): int }', $stringifier->stringify(1, self::DEPTH + 1)),
self::DEPTH,
);
self::assertEquals($expected, $actual);
}
#[Test]
public function itShouldStringifyWhenRawValueIsCallableThatDoesNotHaveAnAccessibleDefaultValue(): void
{
$raw = 'array_walk';
$quoter = new FakeQuoter();
$sut = new CallableStringifier(new FakeStringifier(), $quoter, closureOnly: false);
$actual = $sut->stringify($raw, self::DEPTH);
$expected = $quoter->quote(
'array_walk(object|array &$array, callable $callback, ?mixed $arg = fake.1.cbade92e): true',
self::DEPTH,
);
self::assertEquals($expected, $actual);
}
/** @return array<string, array{0: callable, 1: string}> */
public static function closureRawValuesProvider(): array
{
$var1 = 1;
$var2 = 2;
return [
'static closure without parameters' => [
static fn() => 1,
'Closure { static fn() }',
],
'non-static closure without parameters' => [
fn() => 1,
'Closure { fn() }',
],
'static closure with return type' => [
static fn(): int => 1,
'Closure { static fn(): int }',
],
'non-static closure with return type' => [
fn(): int => 1,
'Closure { fn(): int }',
],
'static closure with typed parameter' => [
static fn(float $value): int => (int) $value,
'Closure { static fn(float $value): int }',
],
'static closure with reference parameter' => [
static fn(float &$value): int => (int) $value,
'Closure { static fn(float &$value): int }',
],
'static closure with nullable parameter' => [
static fn(float|null $value): int => (int) $value,
'Closure { static fn(?float $value): int }',
],
'static closure with constant default value' => [
static fn(int $value = self::DEPTH): int => $value,
'Closure { static fn(int $value = self::DEPTH): int }',
],
'static closure with union type parameter' => [
static fn(int|float $value): int => (int) $value,
'Closure { static fn(int|float $value): int }',
],
'static closure with intersection type parameter' => [
static fn(Countable&Iterator $value): int => $value->count(),
'Closure { static fn(Countable&Iterator $value): int }',
],
'static closure with variadic parameter' => [
static fn(int ...$value): int => array_sum($value),
'Closure { static fn(int ...$value): int }',
],
'static closure with multiple parameters' => [
static fn(float $value1, float $value2): float => $value1 + $value2,
'Closure { static fn(float $value1, float $value2): float }',
],
'static closure with single use variable' => [
static function (int $value) use ($var1): int {
return $value + $var1;
},
'Closure { static fn(int $value) use ($var1): int }',
],
'static closure with multiple use variables' => [
static function (int $value) use ($var1, $var2): int {
return $value + $var1 + $var2;
},
'Closure { static fn(int $value) use ($var1, $var2): int }',
],
'non-static closure with use variable' => [
function (int $value) use ($var1): int {
return $value + $var1;
},
'Closure { fn(int $value) use ($var1): int }',
],
];
}
/** @return array<string, array{0: callable, 1: string}> */
public static function nonClosureCallableRawValuesProvider(): array
{
return [
'invokable object' => [
new class {
public function __invoke(int $parameter): never
{
exit($parameter);
}
},
'class->__invoke(int $parameter): never',
],
'object method as array' => [
[new DateTime(), 'format'],
'DateTime->format(string $format)',
],
'static method as array' => [
['DateTime', 'createFromImmutable'],
'DateTime::createFromImmutable(DateTimeImmutable $object)',
],
'static method as string' => [
'DateTimeImmutable::getLastErrors',
'DateTimeImmutable::getLastErrors()',
],
'function name as string' => [
'chr',
'chr(int $codepoint): string',
],
];
}
}