ClassMethodTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\Node\Stmt;
  3. use PhpParser\Node\Expr\Variable;
  4. use PhpParser\Node\Name;
  5. use PhpParser\Node\Param;
  6. class ClassMethodTest extends \PHPUnit\Framework\TestCase
  7. {
  8. /**
  9. * @dataProvider provideModifiers
  10. */
  11. public function testModifiers($modifier) {
  12. $node = new ClassMethod('foo', [
  13. 'type' => constant('PhpParser\Node\Stmt\Class_::MODIFIER_' . strtoupper($modifier))
  14. ]);
  15. $this->assertTrue($node->{'is' . $modifier}());
  16. }
  17. public function testNoModifiers() {
  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 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)
  45. {
  46. $node = new ClassMethod('foo', [
  47. 'type' => constant('PhpParser\Node\Stmt\Class_::MODIFIER_' . strtoupper($modifier))
  48. ]);
  49. $this->assertTrue($node->isPublic(), 'Node should be implicitly public');
  50. }
  51. public function implicitPublicModifiers() {
  52. return [
  53. ['abstract'],
  54. ['final'],
  55. ['static'],
  56. ];
  57. }
  58. /**
  59. * @dataProvider provideMagics
  60. *
  61. * @param string $name Node name
  62. */
  63. public function testMagic(string $name) {
  64. $node = new ClassMethod($name);
  65. $this->assertTrue($node->isMagic(), 'Method should be magic');
  66. }
  67. public function provideMagics() {
  68. return [
  69. ['__construct'],
  70. ['__DESTRUCT'],
  71. ['__caLL'],
  72. ['__callstatic'],
  73. ['__get'],
  74. ['__set'],
  75. ['__isset'],
  76. ['__unset'],
  77. ['__sleep'],
  78. ['__wakeup'],
  79. ['__tostring'],
  80. ['__set_state'],
  81. ['__clone'],
  82. ['__invoke'],
  83. ['__debuginfo'],
  84. ];
  85. }
  86. public function testFunctionLike() {
  87. $param = new Param(new Variable('a'));
  88. $type = new Name('Foo');
  89. $return = new Return_(new Variable('a'));
  90. $method = new ClassMethod('test', [
  91. 'byRef' => false,
  92. 'params' => [$param],
  93. 'returnType' => $type,
  94. 'stmts' => [$return],
  95. ]);
  96. $this->assertFalse($method->returnsByRef());
  97. $this->assertSame([$param], $method->getParams());
  98. $this->assertSame($type, $method->getReturnType());
  99. $this->assertSame([$return], $method->getStmts());
  100. $method = new ClassMethod('test', [
  101. 'byRef' => true,
  102. 'stmts' => null,
  103. ]);
  104. $this->assertTrue($method->returnsByRef());
  105. $this->assertNull($method->getStmts());
  106. }
  107. }