File: NodeDefinitionTest.php

package info (click to toggle)
symfony 7.4.0~beta2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 133,068 kB
  • sloc: php: 1,541,229; xml: 7,134; javascript: 979; sh: 584; makefile: 250; pascal: 70
file content (117 lines) | stat: -rw-r--r-- 4,629 bytes parent folder | download | duplicates (2)
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
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Config\Tests\Definition\Builder;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\BooleanNodeDefinition;
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
use Symfony\Component\Config\Definition\Builder\ScalarNodeDefinition;
use Symfony\Component\Config\Definition\Builder\StringNodeDefinition;
use Symfony\Component\Config\Definition\Builder\VariableNodeDefinition;
use Symfony\Component\Config\Definition\Exception\InvalidDefinitionException;

class NodeDefinitionTest extends TestCase
{
    public function testSetPathSeparatorChangesChildren()
    {
        $parentNode = new ArrayNodeDefinition('name');
        $childNode = $this->createMock(NodeDefinition::class);

        $childNode
            ->expects($this->once())
            ->method('setPathSeparator')
            ->with('/');
        $childNode
            ->expects($this->once())
            ->method('setParent')
            ->with($parentNode)
            ->willReturn($childNode);
        $parentNode->append($childNode);

        $parentNode->setPathSeparator('/');
    }

    public function testDocUrl()
    {
        $node = new ArrayNodeDefinition('node');
        $node->docUrlNoComposer('https://example.com/doc/{package}/{version:major}.{version:minor}', 'phpunit/phpunit', '12.1.4');

        $r = new \ReflectionObject($node);
        $p = $r->getProperty('attributes');

        $this->assertMatchesRegularExpression('~^https://example.com/doc/phpunit/phpunit/\d+\.\d+$~', $p->getValue($node)['docUrl']);
    }

    public function testDocUrlWithoutPackage()
    {
        $node = new ArrayNodeDefinition('node');
        $node->docUrl('https://example.com/doc/empty{version:major}.empty{version:minor}');

        $r = new \ReflectionObject($node);
        $p = $r->getProperty('attributes');

        $this->assertSame('https://example.com/doc/empty.empty', $p->getValue($node)['docUrl']);
    }

    public function testUnknownPackageThrowsException()
    {
        $node = new ArrayNodeDefinition('node');

        $this->expectException(\OutOfBoundsException::class);
        $this->expectExceptionMessage('Package "phpunit/invalid" is not installed');

        $node->docUrl('https://example.com/doc/{package}/{version:major}.{version:minor}', 'phpunit/invalid');
    }

    #[IgnoreDeprecations]
    #[Group('legacy')]
    #[DataProvider('provideDefinitionClassesAndDefaultValues')]
    public function testIncoherentRequiredAndDefaultValue(string $class, mixed $defaultValue)
    {
        $node = new $class('foo');
        self::assertInstanceOf(NodeDefinition::class, $node);

        // $this->expectException(InvalidDefinitionException::class);
        // $this->expectExceptionMessage('The node "foo" cannot be required and have a default value.');
        $this->expectUserDeprecationMessage('Since symfony/config 7.4: Flagging a node with a default value as required is deprecated. Remove the default from node "foo" or make it optional.');

        $node->defaultValue($defaultValue)->isRequired();
    }

    #[IgnoreDeprecations]
    #[Group('legacy')]
    #[DataProvider('provideDefinitionClassesAndDefaultValues')]
    public function testIncoherentDefaultValueAndRequired(string $class, mixed $defaultValue)
    {
        $node = new $class('foo');
        self::assertInstanceOf(NodeDefinition::class, $node);

        // $this->expectException(InvalidDefinitionException::class);
        // $this->expectExceptionMessage('The node "foo" cannot be required and have a default value.');
        $this->expectUserDeprecationMessage('Since symfony/config 7.4: Setting a default value to a required node is deprecated. Remove the default value from the node "foo" or make it optional.');

        $node->isRequired()->defaultValue($defaultValue);
    }

    public static function provideDefinitionClassesAndDefaultValues()
    {
        yield [ArrayNodeDefinition::class, []];
        yield [ScalarNodeDefinition::class, null];
        yield [BooleanNodeDefinition::class, false];
        yield [StringNodeDefinition::class, 'default'];
        yield [VariableNodeDefinition::class, 'default'];
    }
}