ClassMethodTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\Node\Stmt;
  3. use PhpParser\Modifiers;
  4. use PhpParser\Node\Expr\Variable;
  5. use PhpParser\Node\Name;
  6. use PhpParser\Node\Param;
  7. class ClassMethodTest extends \PHPUnit\Framework\TestCase {
  8. /**
  9. * @dataProvider provideModifiers
  10. */
  11. public function testModifiers($modifier): void {
  12. $node = new ClassMethod('foo', [
  13. 'type' => constant(Modifiers::class . '::' . strtoupper($modifier))
  14. ]);
  15. $this->assertTrue($node->{'is' . $modifier}());
  16. }
  17. public function testNoModifiers(): void {
  18. $node = new ClassMethod('foo', ['type' => 0]);
  19. $this->assertTrue($node->isPublic());
  20. $this->assertFalse($node->isProtected());
  21. $this->assertFalse($node->isPrivate());
  22. $this->assertFalse($node->isAbstract());
  23. $this->assertFalse($node->isFinal());
  24. $this->assertFalse($node->isStatic());
  25. $this->assertFalse($node->isMagic());
  26. }
  27. public static function provideModifiers() {
  28. return [
  29. ['public'],
  30. ['protected'],
  31. ['private'],
  32. ['abstract'],
  33. ['final'],
  34. ['static'],
  35. ];
  36. }
  37. /**
  38. * Checks that implicit public modifier detection for method is working
  39. *
  40. * @dataProvider implicitPublicModifiers
  41. *
  42. * @param string $modifier Node type modifier
  43. */
  44. public function testImplicitPublic(string $modifier): void {
  45. $node = new ClassMethod('foo', [
  46. 'type' => constant(Modifiers::class . '::' . strtoupper($modifier))
  47. ]);
  48. $this->assertTrue($node->isPublic(), 'Node should be implicitly public');
  49. }
  50. public static function implicitPublicModifiers() {
  51. return [
  52. ['abstract'],
  53. ['final'],
  54. ['static'],
  55. ];
  56. }
  57. /**
  58. * @dataProvider provideMagics
  59. *
  60. * @param string $name Node name
  61. */
  62. public function testMagic(string $name): void {
  63. $node = new ClassMethod($name);
  64. $this->assertTrue($node->isMagic(), 'Method should be magic');
  65. }
  66. public static function provideMagics() {
  67. return [
  68. ['__construct'],
  69. ['__DESTRUCT'],
  70. ['__caLL'],
  71. ['__callstatic'],
  72. ['__get'],
  73. ['__set'],
  74. ['__isset'],
  75. ['__unset'],
  76. ['__sleep'],
  77. ['__wakeup'],
  78. ['__tostring'],
  79. ['__set_state'],
  80. ['__clone'],
  81. ['__invoke'],
  82. ['__debuginfo'],
  83. ];
  84. }
  85. public function testFunctionLike(): void {
  86. $param = new Param(new Variable('a'));
  87. $type = new Name('Foo');
  88. $return = new Return_(new Variable('a'));
  89. $method = new ClassMethod('test', [
  90. 'byRef' => false,
  91. 'params' => [$param],
  92. 'returnType' => $type,
  93. 'stmts' => [$return],
  94. ]);
  95. $this->assertFalse($method->returnsByRef());
  96. $this->assertSame([$param], $method->getParams());
  97. $this->assertSame($type, $method->getReturnType());
  98. $this->assertSame([$return], $method->getStmts());
  99. $method = new ClassMethod('test', [
  100. 'byRef' => true,
  101. 'stmts' => null,
  102. ]);
  103. $this->assertTrue($method->returnsByRef());
  104. $this->assertNull($method->getStmts());
  105. }
  106. }