-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathClassPropertyParserTest.php
66 lines (59 loc) · 3.29 KB
/
ClassPropertyParserTest.php
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
<?php
declare(strict_types=1);
namespace PhpKafka\PhpAvroSchemaGenerator\Tests\Unit\Parser;
use PhpKafka\PhpAvroSchemaGenerator\Parser\ClassPropertyParser;
use PhpKafka\PhpAvroSchemaGenerator\Parser\DocCommentParserInterface;
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyInterface;
use PhpParser\Comment\Doc;
use PhpParser\Node\Identifier;
use PhpParser\Node\NullableType;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\PropertyProperty;
use PhpParser\Node\UnionType;
use PhpParser\Node\VarLikeIdentifier;
use PHPUnit\Framework\TestCase;
class ClassPropertyParserTest extends TestCase
{
public function testParseProperty(): void
{
$doc = $this->getMockBuilder(Doc::class)->disableOriginalConstructor()->getMock();
$varId = $this->getMockBuilder(VarLikeIdentifier::class)->disableOriginalConstructor()->getMock();
$varId->name = 'bla';
$identifier = $this->getMockBuilder(Identifier::class)->disableOriginalConstructor()->getMock();
$identifier->name = 'int';
$ut = $this->getMockBuilder(UnionType::class)->disableOriginalConstructor()->getMock();
$ut->types = [$identifier];
$propertyProperty = $this->getMockBuilder(PropertyProperty::class)->disableOriginalConstructor()->getMock();
$propertyProperty->name = $varId;
$nullableType = $this->getMockBuilder(NullableType::class)->disableOriginalConstructor()->getMock();
$nullableType->type = $identifier;
$doc->expects(self::once())->method('getText')->willReturn('bla');
$docParser = $this->getMockForAbstractClass(DocCommentParserInterface::class);
$property1 = $this->getMockBuilder(Property::class)->disableOriginalConstructor()->getMock();
$property1->expects(self::once())->method('getAttributes')->willReturn(['comments' => [$doc]]);
$property1->props = [$propertyProperty];
$property1->type = $identifier;
$property2 = $this->getMockBuilder(Property::class)->disableOriginalConstructor()->getMock();
$property2->type = 'string';
$property2->props = [$propertyProperty];
$property3 = $this->getMockBuilder(Property::class)->disableOriginalConstructor()->getMock();
$property3->type = $ut;
$property3->props = [$propertyProperty];
$property4 = $this->getMockBuilder(Property::class)->disableOriginalConstructor()->getMock();
$property4->type = $nullableType;
$property4->props = [$propertyProperty];
$cpp = new ClassPropertyParser($docParser);
self::assertInstanceOf(PhpClassPropertyInterface::class, $cpp->parseProperty($property1));
self::assertInstanceOf(PhpClassPropertyInterface::class, $cpp->parseProperty($property2));
self::assertInstanceOf(PhpClassPropertyInterface::class, $cpp->parseProperty($property3));
self::assertInstanceOf(PhpClassPropertyInterface::class, $cpp->parseProperty($property4));
}
public function testParsePropertyExceptionOnNonProperty(): void
{
self::expectException(\RuntimeException::class);
self::expectExceptionMessage('Property must be of type: PhpParser\Node\Stmt\Property');
$docParser = $this->getMockForAbstractClass(DocCommentParserInterface::class);
$cpp = new ClassPropertyParser($docParser);
$cpp->parseProperty(1);
}
}