File: EnumCaseTest.php

package info (click to toggle)
php-parser 5.6.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,532 kB
  • sloc: php: 23,585; yacc: 1,272; makefile: 39; sh: 8
file content (82 lines) | stat: -rw-r--r-- 2,144 bytes parent folder | download | duplicates (3)
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
<?php declare(strict_types=1);

namespace PhpParser\Builder;

use PhpParser\Comment;
use PhpParser\Node\Arg;
use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Stmt;
use PHPUnit\Framework\Attributes\DataProvider;

class EnumCaseTest extends \PHPUnit\Framework\TestCase {
    public function createEnumCaseBuilder($name) {
        return new EnumCase($name);
    }

    public function testDocComment(): void {
        $node = $this->createEnumCaseBuilder('TEST')
            ->setDocComment('/** Test */')
            ->getNode();

        $this->assertEquals(
            new Stmt\EnumCase(
                "TEST",
                null,
                [],
                [
                    'comments' => [new Comment\Doc('/** Test */')]
                ]
            ),
            $node
        );
    }

    public function testAddAttribute(): void {
        $attribute = new Attribute(
            new Name('Attr'),
            [new Arg(new Int_(1), false, false, [], new Identifier('name'))]
        );
        $attributeGroup = new AttributeGroup([$attribute]);

        $node = $this->createEnumCaseBuilder('ATTR_GROUP')
            ->addAttribute($attributeGroup)
            ->getNode();

        $this->assertEquals(
            new Stmt\EnumCase(
                "ATTR_GROUP",
                null,
                [$attributeGroup]
            ),
            $node
        );
    }

    #[DataProvider('provideTestDefaultValues')]
    public function testValues($value, $expectedValueNode): void {
        $node = $this->createEnumCaseBuilder('TEST')
            ->setValue($value)
            ->getNode()
        ;

        $this->assertEquals($expectedValueNode, $node->expr);
    }

    public static function provideTestDefaultValues() {
        return [
            [
                31415,
                new Scalar\Int_(31415)
            ],
            [
                'Hallo World',
                new Scalar\String_('Hallo World')
            ],
        ];
    }
}