Skip to content

Commit

Permalink
Use flexible nowdocs
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Feb 20, 2025
1 parent 47c8aa4 commit f8e44d3
Show file tree
Hide file tree
Showing 3 changed files with 418 additions and 419 deletions.
179 changes: 89 additions & 90 deletions tests/ExportClosureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public function testExportSimpleClosure(): void
};

$expected = <<<'PHP'
function () {
return 'Hello, world!';
}
PHP;
function () {
return 'Hello, world!';
}
PHP;

$this->assertExportEquals($expected, $var);
}
Expand Down Expand Up @@ -59,20 +59,20 @@ public function testExportNestedComplexClosure(): void
];

$expected = <<<'PHP'
[
(object) [
'callback' => function () {
return function (\Brick\VarExporter\Tests\Classes\PublicPropertiesOnly $a, int $b, string &$c, string ...$d): ?string {
$a->foo += $b;
$c = $a->bar;
$a->bar = implode('', $d);
$this->someProp = [$a->foo, $a->bar, $c];
return $this->someProp['c'];
};
}
]
]
PHP;
[
(object) [
'callback' => function () {
return function (\Brick\VarExporter\Tests\Classes\PublicPropertiesOnly $a, int $b, string &$c, string ...$d): ?string {
$a->foo += $b;
$c = $a->bar;
$a->bar = implode('', $d);
$this->someProp = [$a->foo, $a->bar, $c];
return $this->someProp['c'];
};
}
]
]
PHP;

$this->assertExportEquals($expected, $var);
}
Expand All @@ -92,15 +92,15 @@ public function testExportNamespacedCode(): void
};

$expected = <<<'PHP'
function (\Brick\VarExporter\Tests\Classes\SetState $a): array {
return ['callback' => function (\Brick\VarExporter\Tests\Classes\SetState $a): \Brick\VarExporter\Tests\Classes\NoProperties {
strlen(PHP_VERSION);
\Brick\VarExporter\Dummy\Functions\imported_function(\Brick\VarExporter\Dummy\Constants\IMPORTED_CONSTANT);
\Brick\VarExporter\Dummy\Functions\explicitly_namespaced_function(\Brick\VarExporter\Dummy\Constants\EXPLICITLY_NAMESPACED_CONSTANT);
return new \Brick\VarExporter\Tests\Classes\NoProperties();
}];
}
PHP;
function (\Brick\VarExporter\Tests\Classes\SetState $a): array {
return ['callback' => function (\Brick\VarExporter\Tests\Classes\SetState $a): \Brick\VarExporter\Tests\Classes\NoProperties {
strlen(PHP_VERSION);
\Brick\VarExporter\Dummy\Functions\imported_function(\Brick\VarExporter\Dummy\Constants\IMPORTED_CONSTANT);
\Brick\VarExporter\Dummy\Functions\explicitly_namespaced_function(\Brick\VarExporter\Dummy\Constants\EXPLICITLY_NAMESPACED_CONSTANT);
return new \Brick\VarExporter\Tests\Classes\NoProperties();
}];
}
PHP;

$this->assertExportEquals($expected, $var);
}
Expand All @@ -112,34 +112,34 @@ public function testExportClosureWithStringsContainingLikeBreaks(): void
World!';

$b = <<<TXT
Hello,
world!
TXT;
Hello,
world!
TXT;

$c = <<<'TXT'
Hello,
world!
TXT;
Hello,
world!
TXT;

return $a . $b . $c;
};

$expected = <<<'PHP'
return function () {
$a = 'Hello,
World!';
$b = <<<TXT
Hello,
world!
TXT;
$c = <<<'TXT'
Hello,
world!
TXT;
return $a . $b . $c;
};

PHP;
return function () {
$a = 'Hello,
World!';
$b = <<<TXT
Hello,
world!
TXT;
$c = <<<'TXT'
Hello,
world!
TXT;
return $a . $b . $c;
};

PHP;

$this->assertExportEquals($expected, $var, VarExporter::ADD_RETURN);
}
Expand Down Expand Up @@ -168,12 +168,12 @@ public function testExportClosureWithUseAsVars(): void
};

$expected = <<<'PHP'
return function () {
$foo = 'bar';
return $foo;
};
return function () {
$foo = 'bar';
return $foo;
};

PHP;
PHP;

$this->assertExportEquals($expected, $var, VarExporter::ADD_RETURN | VarExporter::CLOSURE_SNAPSHOT_USES);
}
Expand All @@ -191,32 +191,31 @@ public function testExportClosureWithUseClosure(): void
};

$expected = <<<'PHP'
return function () {
$sub = function () {
$foo = 'bar';
return $foo;
};
return $sub();
};
return function () {
$sub = function () {
$foo = 'bar';
return $foo;
};
return $sub();
};

PHP;
PHP;

$this->assertExportEquals($expected, $var, VarExporter::ADD_RETURN | VarExporter::CLOSURE_SNAPSHOT_USES);
}


public function testExportArrowFunction(): void
{
$var = [fn ($planet) => 'hello ' . $planet]; // Wrapping in array for valid syntax PHP <7.4

$expected = <<<'PHP'
return [
function ($planet) {
return 'hello ' . $planet;
}
];
return [
function ($planet) {
return 'hello ' . $planet;
}
];

PHP;
PHP;

$this->assertExportEquals($expected, $var, VarExporter::ADD_RETURN);
}
Expand All @@ -241,14 +240,14 @@ public function testExportArrowFunctionWithContextVarAsVar(): void
$var = [fn ($planet) => $greet . ' ' . $planet];

$expected = <<<'PHP'
return [
function ($planet) {
$greet = 'hello';
return $greet . ' ' . $planet;
}
];
return [
function ($planet) {
$greet = 'hello';
return $greet . ' ' . $planet;
}
];

PHP;
PHP;

$this->assertExportEquals($expected, $var, VarExporter::ADD_RETURN | VarExporter::CLOSURE_SNAPSHOT_USES);
}
Expand All @@ -264,28 +263,28 @@ public function testExportEnumMatchFunction(): void
];

$expected = <<<'PHP'
[
(object) [
'callback' => function (\Brick\VarExporter\Tests\Classes\Enum $enum): string {
return match ($enum) {
\Brick\VarExporter\Tests\Classes\Enum::TEST => 'foo',
};
}
]
]
PHP;
[
(object) [
'callback' => function (\Brick\VarExporter\Tests\Classes\Enum $enum): string {
return match ($enum) {
\Brick\VarExporter\Tests\Classes\Enum::TEST => 'foo',
};
}
]
]
PHP;

$this->assertExportEquals($expected, $var);
}

public function testExportClosureDefinedInEval(): void
{
$var = eval(<<<PHP
return function() {
return 'Hello, world!';
};
PHP
);
return function() {
return 'Hello, world!';
};
PHP
);
$this->assertExportThrows("Closure defined in eval()'d code cannot be exported.", $var);
}

Expand Down
Loading

0 comments on commit f8e44d3

Please sign in to comment.