ClassTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\Node\Stmt;
  3. use PhpParser\Node\Scalar\String_;
  4. class ClassTest extends \PHPUnit\Framework\TestCase
  5. {
  6. public function testIsAbstract() {
  7. $class = new Class_('Foo', ['type' => Class_::MODIFIER_ABSTRACT]);
  8. $this->assertTrue($class->isAbstract());
  9. $class = new Class_('Foo');
  10. $this->assertFalse($class->isAbstract());
  11. }
  12. public function testIsFinal() {
  13. $class = new Class_('Foo', ['type' => Class_::MODIFIER_FINAL]);
  14. $this->assertTrue($class->isFinal());
  15. $class = new Class_('Foo');
  16. $this->assertFalse($class->isFinal());
  17. }
  18. public function testGetTraitUses() {
  19. $traitUses = [
  20. new TraitUse([new Trait_('foo')]),
  21. new TraitUse([new Trait_('bar')]),
  22. ];
  23. $class = new Class_('Foo', [
  24. 'stmts' => [
  25. $traitUses[0],
  26. new ClassMethod('fooBar'),
  27. $traitUses[1],
  28. ]
  29. ]);
  30. $this->assertSame($traitUses, $class->getTraitUses());
  31. }
  32. public function testGetMethods() {
  33. $methods = [
  34. new ClassMethod('foo'),
  35. new ClassMethod('bar'),
  36. new ClassMethod('fooBar'),
  37. ];
  38. $class = new Class_('Foo', [
  39. 'stmts' => [
  40. new TraitUse([]),
  41. $methods[0],
  42. new ClassConst([]),
  43. $methods[1],
  44. new Property(0, []),
  45. $methods[2],
  46. ]
  47. ]);
  48. $this->assertSame($methods, $class->getMethods());
  49. }
  50. public function testGetConstants() {
  51. $constants = [
  52. new ClassConst([new \PhpParser\Node\Const_('foo', new String_('foo_value'))]),
  53. new ClassConst([new \PhpParser\Node\Const_('bar', new String_('bar_value'))]),
  54. ];
  55. $class = new Class_('Foo', [
  56. 'stmts' => [
  57. new TraitUse([]),
  58. $constants[0],
  59. new ClassMethod('fooBar'),
  60. $constants[1],
  61. ]
  62. ]);
  63. $this->assertSame($constants, $class->getConstants());
  64. }
  65. public function testGetProperties()
  66. {
  67. $properties = [
  68. new Property(Class_::MODIFIER_PUBLIC, [new PropertyProperty('foo')]),
  69. new Property(Class_::MODIFIER_PUBLIC, [new PropertyProperty('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() {
  83. $properties = [
  84. $fooProp = new Property(Class_::MODIFIER_PUBLIC, [new PropertyProperty('foo1')]),
  85. $barProp = new Property(Class_::MODIFIER_PUBLIC, [new PropertyProperty('BAR1')]),
  86. $fooBarProp = new Property(Class_::MODIFIER_PUBLIC, [new PropertyProperty('foo2'), new PropertyProperty('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() {
  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. }