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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
<?php declare(strict_types=1);
namespace PhpParser\Node\Stmt;
use PhpParser\Modifiers;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PhpParser\Node\Param;
use PHPUnit\Framework\Attributes\DataProvider;
class ClassMethodTest extends \PHPUnit\Framework\TestCase {
#[DataProvider('provideModifiers')]
public function testModifiers($modifier): void {
$node = new ClassMethod('foo', [
'type' => constant(Modifiers::class . '::' . strtoupper($modifier))
]);
$this->assertTrue($node->{'is' . $modifier}());
}
public function testNoModifiers(): void {
$node = new ClassMethod('foo', ['type' => 0]);
$this->assertTrue($node->isPublic());
$this->assertFalse($node->isProtected());
$this->assertFalse($node->isPrivate());
$this->assertFalse($node->isAbstract());
$this->assertFalse($node->isFinal());
$this->assertFalse($node->isStatic());
$this->assertFalse($node->isMagic());
}
public static function provideModifiers() {
return [
['public'],
['protected'],
['private'],
['abstract'],
['final'],
['static'],
];
}
/**
* Checks that implicit public modifier detection for method is working
*
* @param string $modifier Node type modifier
*/
#[DataProvider('implicitPublicModifiers')]
public function testImplicitPublic(string $modifier): void {
$node = new ClassMethod('foo', [
'type' => constant(Modifiers::class . '::' . strtoupper($modifier))
]);
$this->assertTrue($node->isPublic(), 'Node should be implicitly public');
}
public static function implicitPublicModifiers() {
return [
['abstract'],
['final'],
['static'],
];
}
/**
* @param string $name Node name
*/
#[DataProvider('provideMagics')]
public function testMagic(string $name): void {
$node = new ClassMethod($name);
$this->assertTrue($node->isMagic(), 'Method should be magic');
}
public static function provideMagics() {
return [
['__construct'],
['__DESTRUCT'],
['__caLL'],
['__callstatic'],
['__get'],
['__set'],
['__isset'],
['__unset'],
['__sleep'],
['__wakeup'],
['__tostring'],
['__set_state'],
['__clone'],
['__invoke'],
['__debuginfo'],
];
}
public function testFunctionLike(): void {
$param = new Param(new Variable('a'));
$type = new Name('Foo');
$return = new Return_(new Variable('a'));
$method = new ClassMethod('test', [
'byRef' => false,
'params' => [$param],
'returnType' => $type,
'stmts' => [$return],
]);
$this->assertFalse($method->returnsByRef());
$this->assertSame([$param], $method->getParams());
$this->assertSame($type, $method->getReturnType());
$this->assertSame([$return], $method->getStmts());
$method = new ClassMethod('test', [
'byRef' => true,
'stmts' => null,
]);
$this->assertTrue($method->returnsByRef());
$this->assertNull($method->getStmts());
}
}
|