Skip to content

Commit 3fcad54

Browse files
committed
php nodes tests
1 parent 3ec1814 commit 3fcad54

14 files changed

+148
-16
lines changed

src/Symfony/Component/Serializer/Php/ArrayAccessNode.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,24 @@
2020
{
2121
public function __construct(
2222
public PhpNodeInterface $array,
23-
public PhpNodeInterface $key,
23+
public ?PhpNodeInterface $key,
2424
) {
2525
}
2626

2727
public function compile(Compiler $compiler): void
2828
{
29-
$compiler->raw(sprintf('%s[%s]', $compiler->subcompile($this->array), $compiler->subcompile($this->key)));
29+
$compiler->raw(sprintf(
30+
'%s[%s]',
31+
$compiler->subcompile($this->array),
32+
null !== $this->key ? $compiler->subcompile($this->key) : '',
33+
));
3034
}
3135

3236
public function optimize(Optimizer $optimizer): static
3337
{
3438
return new self(
3539
$optimizer->optimize($this->array),
36-
$optimizer->optimize($this->key),
40+
null !== $this->key ? $optimizer->optimize($this->key) : null,
3741
);
3842
}
3943
}

src/Symfony/Component/Serializer/Php/ForEachNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424
public function __construct(
2525
public PhpNodeInterface $collection,
26-
public VariableNode|null $keyName,
26+
public ?VariableNode $keyName,
2727
public VariableNode $valueName,
2828
public array $body,
2929
) {

src/Symfony/Component/Serializer/Php/ParametersNode.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ public function compile(Compiler $compiler): void
3939
$byReference = true;
4040
}
4141

42-
$name = $compiler->subcompile(new VariableNode($name));
43-
44-
$argumentSources[] = sprintf('%s%s%s', $type, $byReference ? '&' : '', $name);
42+
$argumentSources[] = sprintf('%s%s%s', $type, $byReference ? '&' : '', $compiler->subcompile(new VariableNode($name)));
4543
}
4644

4745
$compiler->raw(implode(', ', $argumentSources));

src/Symfony/Component/Serializer/Php/PropertyNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function compile(Compiler $compiler): void
2929
{
3030
$compiler
3131
->compile($this->object)
32-
->raw(sprintf('%s%s', $this->static ? '::' : '->', $this->property));
32+
->raw(sprintf('%s%s', $this->static ? '::$' : '->', $this->property));
3333
}
3434

3535
public function optimize(Optimizer $optimizer): static

src/Symfony/Component/Serializer/Tests/Php/ArgumentsNodeTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Serializer\Php\ArgumentsNode;
1616
use Symfony\Component\Serializer\Php\Compiler;
17+
use Symfony\Component\Serializer\Php\ScalarNode;
1718
use Symfony\Component\Serializer\Php\VariableNode;
1819

1920
class ArgumentsNodeTest extends TestCase
@@ -23,7 +24,7 @@ public function testCompile()
2324
(new ArgumentsNode([new VariableNode('foo')]))->compile($compiler = new Compiler());
2425
$this->assertSame('$foo', $compiler->source());
2526

26-
(new ArgumentsNode([new VariableNode('foo'), new VariableNode('bar', byReference: true)]))->compile($compiler = new Compiler());
27-
$this->assertSame('$foo, &$bar', $compiler->source());
27+
(new ArgumentsNode([new ScalarNode(123), new VariableNode('bar', byReference: true)]))->compile($compiler = new Compiler());
28+
$this->assertSame('123, &$bar', $compiler->source());
2829
}
2930
}

src/Symfony/Component/Serializer/Tests/Php/ArrayAccessNodeTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,8 @@ public function testCompile()
2323
{
2424
(new ArrayAccessNode(new VariableNode('foo'), new ScalarNode('bar')))->compile($compiler = new Compiler());
2525
$this->assertSame('$foo["bar"]', $compiler->source());
26+
27+
(new ArrayAccessNode(new VariableNode('foo'), key: null))->compile($compiler = new Compiler());
28+
$this->assertSame('$foo[]', $compiler->source());
2629
}
2730
}

src/Symfony/Component/Serializer/Tests/Php/ArrayNodeTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,8 @@ public function testCompile()
2828

2929
(new ArrayNode(['foo' => new ScalarNode('foo'), 'bar' => new ScalarNode('bar')]))->compile($compiler = new Compiler());
3030
$this->assertSame('["foo" => "foo", "bar" => "bar"]', $compiler->source());
31+
32+
(new ArrayNode([1 => new ScalarNode('foo'), 3 => new ScalarNode('bar')]))->compile($compiler = new Compiler());
33+
$this->assertSame('[1 => "foo", 3 => "bar"]', $compiler->source());
3134
}
3235
}

src/Symfony/Component/Serializer/Tests/Php/ClosureNodeTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Serializer\Php\Compiler;
1818
use Symfony\Component\Serializer\Php\ExpressionNode;
1919
use Symfony\Component\Serializer\Php\ParametersNode;
20+
use Symfony\Component\Serializer\Php\PhpNodeInterface;
2021
use Symfony\Component\Serializer\Php\ScalarNode;
2122
use Symfony\Component\Serializer\Php\VariableNode;
2223

@@ -25,8 +26,8 @@ class ClosureNodeTest extends TestCase
2526
/**
2627
* @dataProvider compileDataProvider
2728
*
28-
* @param list<NodeInterface> $body
29-
* @param list<VariableNode> $uses
29+
* @param list<PhpNodeInterface> $body
30+
* @param list<VariableNode> $uses
3031
*/
3132
public function testCompile(string $expectedSource, ParametersNode $arguments, ?string $returnType, bool $static, array $body, ?ArgumentsNode $uses)
3233
{
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Php;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Serializer\Php\Compiler;
16+
use Symfony\Component\Serializer\Php\ContinueNode;
17+
18+
class ContinueNodeTest extends TestCase
19+
{
20+
public function testCompile()
21+
{
22+
(new ContinueNode())->compile($compiler = new Compiler());
23+
$this->assertSame('continue', $compiler->source());
24+
}
25+
}

src/Symfony/Component/Serializer/Tests/Php/IfNodeTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class IfNodeTest extends TestCase
2424
/**
2525
* @dataProvider compileDataProvider
2626
*
27-
* @param list<NodeInterface> $onIf
28-
* @param list<NodeInterface> $onElse
27+
* @param list<PhpNodeInterface> $onIf
28+
* @param list<PhpNodeInterface> $onElse
2929
* @param list<array{condition: PhpNodeInterface, body: list<PhpNodeInterface>}> $elseIfs
3030
*/
3131
public function testCompile(string $expectedSource, PhpNodeInterface $condition, array $onIf, array $onElse, array $elseIfs)

src/Symfony/Component/Serializer/Tests/Php/PropertyNodeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ public function testCompile()
2424
$this->assertSame('$foo->bar', $compiler->source());
2525

2626
(new PropertyNode(new VariableNode('foo'), 'bar', true))->compile($compiler = new Compiler());
27-
$this->assertSame('$foo::bar', $compiler->source());
27+
$this->assertSame('$foo::$bar', $compiler->source());
2828
}
2929
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Php;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Serializer\Php\AssignNode;
16+
use Symfony\Component\Serializer\Php\Compiler;
17+
use Symfony\Component\Serializer\Php\ExpressionNode;
18+
use Symfony\Component\Serializer\Php\ParametersNode;
19+
use Symfony\Component\Serializer\Php\PhpNodeInterface;
20+
use Symfony\Component\Serializer\Php\ScalarNode;
21+
use Symfony\Component\Serializer\Php\TryCatchNode;
22+
use Symfony\Component\Serializer\Php\VariableNode;
23+
24+
class TryCatchNodeTest extends TestCase
25+
{
26+
/**
27+
* @dataProvider compileDataProvider
28+
*
29+
* @param list<PhpNodeInterface> $tryNodes
30+
* @param list<PhpNodeInterface> $catchNodes
31+
*/
32+
public function testCompile(string $expectedSource, array $tryNodes, array $catchNodes, ParametersNode $catchParameters)
33+
{
34+
(new TryCatchNode($tryNodes, $catchNodes, $catchParameters))->compile($compiler = new Compiler());
35+
$this->assertSame($expectedSource, $compiler->source());
36+
}
37+
38+
/**
39+
* @return iterable<array{0: string, 1: list<PhpNodeInterface>, 2: list<PhpNodeInterface>, 3: ParametersNode}>
40+
*/
41+
public static function compileDataProvider(): iterable
42+
{
43+
yield [
44+
<<<PHP
45+
try {
46+
} catch (\$e) {
47+
}
48+
49+
PHP,
50+
[],
51+
[],
52+
new ParametersNode(['e' => null]),
53+
];
54+
yield [
55+
<<<PHP
56+
try {
57+
\$foo = true;
58+
} catch (Exception \$e) {
59+
\$foo = false;
60+
}
61+
62+
PHP,
63+
[new ExpressionNode(new AssignNode(new VariableNode('foo'), new ScalarNode(true)))],
64+
[new ExpressionNode(new AssignNode(new VariableNode('foo'), new ScalarNode(false)))],
65+
new ParametersNode(['e' => 'Exception']),
66+
];
67+
}
68+
}

src/Symfony/Component/Serializer/Tests/Php/VariableNodeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function testCompile()
2222
(new VariableNode('foo'))->compile($compiler = new Compiler());
2323
$this->assertSame('$foo', $compiler->source());
2424

25-
(new VariableNode('foo', true))->compile($compiler = new Compiler());
25+
(new VariableNode('foo', byReference: true))->compile($compiler = new Compiler());
2626
$this->assertSame('&$foo', $compiler->source());
2727
}
2828
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Php;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Serializer\Php\Compiler;
16+
use Symfony\Component\Serializer\Php\ScalarNode;
17+
use Symfony\Component\Serializer\Php\YieldNode;
18+
19+
class YieldNodeTest extends TestCase
20+
{
21+
public function testCompile()
22+
{
23+
(new YieldNode(new ScalarNode(true)))->compile($compiler = new Compiler());
24+
$this->assertSame('yield true', $compiler->source());
25+
26+
(new YieldNode(new ScalarNode('value'), new ScalarNode('key')))->compile($compiler = new Compiler());
27+
$this->assertSame('yield "key" => "value"', $compiler->source());
28+
}
29+
}

0 commit comments

Comments
 (0)