ClassTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\Node\Stmt;
  3. use PhpParser\Modifiers;
  4. use PhpParser\Node\PropertyItem;
  5. use PhpParser\Node\Scalar\String_;
  6. class ClassTest extends \PHPUnit\Framework\TestCase {
  7. public function testIsAbstract(): void {
  8. $class = new Class_('Foo', ['type' => Modifiers::ABSTRACT]);
  9. $this->assertTrue($class->isAbstract());
  10. $class = new Class_('Foo');
  11. $this->assertFalse($class->isAbstract());
  12. }
  13. public function testIsFinal(): void {
  14. $class = new Class_('Foo', ['type' => Modifiers::FINAL]);
  15. $this->assertTrue($class->isFinal());
  16. $class = new Class_('Foo');
  17. $this->assertFalse($class->isFinal());
  18. }
  19. public function testGetTraitUses(): void {
  20. $traitUses = [
  21. new TraitUse([new Trait_('foo')]),
  22. new TraitUse([new Trait_('bar')]),
  23. ];
  24. $class = new Class_('Foo', [
  25. 'stmts' => [
  26. $traitUses[0],
  27. new ClassMethod('fooBar'),
  28. $traitUses[1],
  29. ]
  30. ]);
  31. $this->assertSame($traitUses, $class->getTraitUses());
  32. }
  33. public function testGetMethods(): void {
  34. $methods = [
  35. new ClassMethod('foo'),
  36. new ClassMethod('bar'),
  37. new ClassMethod('fooBar'),
  38. ];
  39. $class = new Class_('Foo', [
  40. 'stmts' => [
  41. new TraitUse([]),
  42. $methods[0],
  43. new ClassConst([]),
  44. $methods[1],
  45. new Property(0, []),
  46. $methods[2],
  47. ]
  48. ]);
  49. $this->assertSame($methods, $class->getMethods());
  50. }
  51. public function testGetConstants(): void {
  52. $constants = [
  53. new ClassConst([new \PhpParser\Node\Const_('foo', new String_('foo_value'))]),
  54. new ClassConst([new \PhpParser\Node\Const_('bar', new String_('bar_value'))]),
  55. ];
  56. $class = new Class_('Foo', [
  57. 'stmts' => [
  58. new TraitUse([]),
  59. $constants[0],
  60. new ClassMethod('fooBar'),
  61. $constants[1],
  62. ]
  63. ]);
  64. $this->assertSame($constants, $class->getConstants());
  65. }
  66. public function testGetProperties(): void {
  67. $properties = [
  68. new Property(Modifiers::PUBLIC, [new PropertyItem('foo')]),
  69. new Property(Modifiers::PUBLIC, [new PropertyItem('bar')]),
  70. ];
  71. $class = new Class_('Foo', [
  72. 'stmts' => [
  73. new TraitUse([]),
  74. $properties[0],
  75. new ClassConst([]),
  76. $properties[1],
  77. new ClassMethod('fooBar'),
  78. ]
  79. ]);
  80. $this->assertSame($properties, $class->getProperties());
  81. }
  82. public function testGetProperty(): void {
  83. $properties = [
  84. $fooProp = new Property(Modifiers::PUBLIC, [new PropertyItem('foo1')]),
  85. $barProp = new Property(Modifiers::PUBLIC, [new PropertyItem('BAR1')]),
  86. $fooBarProp = new Property(Modifiers::PUBLIC, [new PropertyItem('foo2'), new PropertyItem('bar2')]),
  87. ];
  88. $class = new Class_('Foo', [
  89. 'stmts' => [
  90. new TraitUse([]),
  91. $properties[0],
  92. new ClassConst([]),
  93. $properties[1],
  94. new ClassMethod('fooBar'),
  95. $properties[2],
  96. ]
  97. ]);
  98. $this->assertSame($fooProp, $class->getProperty('foo1'));
  99. $this->assertSame($barProp, $class->getProperty('BAR1'));
  100. $this->assertSame($fooBarProp, $class->getProperty('foo2'));
  101. $this->assertSame($fooBarProp, $class->getProperty('bar2'));
  102. $this->assertNull($class->getProperty('bar1'));
  103. $this->assertNull($class->getProperty('nonExisting'));
  104. }
  105. public function testGetMethod(): void {
  106. $methodConstruct = new ClassMethod('__CONSTRUCT');
  107. $methodTest = new ClassMethod('test');
  108. $class = new Class_('Foo', [
  109. 'stmts' => [
  110. new ClassConst([]),
  111. $methodConstruct,
  112. new Property(0, []),
  113. $methodTest,
  114. ]
  115. ]);
  116. $this->assertSame($methodConstruct, $class->getMethod('__construct'));
  117. $this->assertSame($methodTest, $class->getMethod('test'));
  118. $this->assertNull($class->getMethod('nonExisting'));
  119. }
  120. }