1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-12-11 17:09:41 +01:00
PHP-Parser/test/PhpParser/Node/Stmt/ClassMethodTest.php

124 lines
3.3 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
namespace PhpParser\Node\Stmt;
2017-05-07 19:54:32 +02:00
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PhpParser\Node\Param;
2017-04-27 18:14:07 +02:00
class ClassMethodTest extends \PHPUnit\Framework\TestCase
{
/**
* @dataProvider provideModifiers
*/
public function testModifiers($modifier) {
2017-08-13 14:35:03 +02:00
$node = new ClassMethod('foo', [
'type' => constant('PhpParser\Node\Stmt\Class_::MODIFIER_' . strtoupper($modifier))
2017-08-13 14:35:03 +02:00
]);
$this->assertTrue($node->{'is' . $modifier}());
}
public function testNoModifiers() {
2017-08-13 14:35:03 +02:00
$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());
2017-04-22 14:37:53 +02:00
$this->assertFalse($node->isMagic());
}
public function provideModifiers() {
2017-08-13 14:35:03 +02:00
return [
['public'],
['protected'],
['private'],
['abstract'],
['final'],
['static'],
];
}
/**
* Checks that implicit public modifier detection for method is working
*
* @dataProvider implicitPublicModifiers
*
2017-04-26 21:49:22 +02:00
* @param string $modifier Node type modifier
*/
2017-04-28 21:40:59 +02:00
public function testImplicitPublic(string $modifier)
{
2017-08-13 14:35:03 +02:00
$node = new ClassMethod('foo', [
'type' => constant('PhpParser\Node\Stmt\Class_::MODIFIER_' . strtoupper($modifier))
2017-08-13 14:35:03 +02:00
]);
$this->assertTrue($node->isPublic(), 'Node should be implicitly public');
}
public function implicitPublicModifiers() {
2017-08-13 14:35:03 +02:00
return [
['abstract'],
['final'],
['static'],
];
}
2017-04-22 14:37:53 +02:00
/**
* @dataProvider provideMagics
*
* @param string $name Node name
*/
2017-04-28 21:40:59 +02:00
public function testMagic(string $name) {
2017-04-22 14:37:53 +02:00
$node = new ClassMethod($name);
$this->assertTrue($node->isMagic(), 'Method should be magic');
}
public function provideMagics() {
2017-08-13 14:35:03 +02:00
return [
['__construct'],
['__DESTRUCT'],
['__caLL'],
['__callstatic'],
['__get'],
['__set'],
['__isset'],
['__unset'],
['__sleep'],
['__wakeup'],
['__tostring'],
['__set_state'],
['__clone'],
['__invoke'],
['__debuginfo'],
];
2017-04-22 14:37:53 +02:00
}
2017-05-07 19:54:32 +02:00
public function testFunctionLike() {
$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());
}
}