-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TASK] Add functional tests to all visitors
Closes #34
- Loading branch information
Showing
9 changed files
with
646 additions
and
0 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
Tests/Functional/Parser/Visitor/ClassMethodVisitorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
/* | ||
* This file is developed by evoWeb. | ||
* | ||
* It is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License, either version 2 | ||
* of the License, or any later version. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Evoweb\Extender\Tests\Functional\Parser\Visitor; | ||
|
||
use Evoweb\Extender\Parser\FileSegments; | ||
use Evoweb\Extender\Parser\Visitor\ClassMethodVisitor; | ||
use Evoweb\Extender\Tests\Functional\AbstractTestBase; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PhpParser\NodeTraverser; | ||
use PhpParser\ParserFactory; | ||
use PhpParser\PhpVersion; | ||
use PHPUnit\Framework\Attributes\Test; | ||
|
||
class ClassMethodVisitorTest extends AbstractTestBase | ||
{ | ||
#[Test] | ||
public function enterNode(): void | ||
{ | ||
$fileSegments = new FileSegments(); | ||
$visitor = new ClassMethodVisitor($fileSegments); | ||
|
||
$classMethode1 = new ClassMethod('Method1'); | ||
$classMethode2 = new ClassMethod('Method2'); | ||
|
||
$class = new Class_('TestClass'); | ||
$class->stmts[] = $classMethode1; | ||
$class->stmts[] = $classMethode2; | ||
|
||
$traverser = new NodeTraverser(); | ||
$traverser->addVisitor($visitor); | ||
$traverser->traverse([$class]); | ||
|
||
$this->assertEquals($classMethode2, $fileSegments->getMethods()[1]); | ||
} | ||
|
||
#[Test] | ||
public function parsedCode(): void | ||
{ | ||
$code = <<<'CODE' | ||
<?php | ||
class TestClass | ||
{ | ||
public function method1(): void {} | ||
public function method2(): void {} | ||
} | ||
CODE; | ||
$parser = (new ParserFactory())->createForVersion(PhpVersion::fromComponents(8, 2)); | ||
$ast = $parser->parse($code); | ||
|
||
$fileSegments = new FileSegments(); | ||
$visitor = new ClassMethodVisitor($fileSegments); | ||
|
||
$traverser = new NodeTraverser(); | ||
$traverser->addVisitor($visitor); | ||
$traverser->traverse($ast); | ||
|
||
$this->assertEquals('method2', $fileSegments->getMethods()[1]->name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
/* | ||
* This file is developed by evoWeb. | ||
* | ||
* It is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License, either version 2 | ||
* of the License, or any later version. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Evoweb\Extender\Tests\Functional\Parser\Visitor; | ||
|
||
use Evoweb\Extender\Parser\FileSegments; | ||
use Evoweb\Extender\Parser\Visitor\ClassVisitor; | ||
use Evoweb\Extender\Tests\Functional\AbstractTestBase; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\NodeTraverser; | ||
use PhpParser\ParserFactory; | ||
use PhpParser\PhpVersion; | ||
use PHPUnit\Framework\Attributes\Test; | ||
|
||
class ClassVisitorTest extends AbstractTestBase | ||
{ | ||
#[Test] | ||
public function enterNode(): void | ||
{ | ||
$fileSegments = new FileSegments(); | ||
$visitor = new ClassVisitor($fileSegments); | ||
|
||
$class = new Class_('TestClass'); | ||
|
||
$traverser = new NodeTraverser(); | ||
$traverser->addVisitor($visitor); | ||
$traverser->traverse([$class]); | ||
|
||
$this->assertEquals($class, $fileSegments->getClass()); | ||
} | ||
|
||
#[Test] | ||
public function parsedCode(): void | ||
{ | ||
$code = <<<'CODE' | ||
<?php | ||
class TestClass | ||
{ | ||
} | ||
CODE; | ||
$parser = (new ParserFactory())->createForVersion(PhpVersion::fromComponents(8, 2)); | ||
$ast = $parser->parse($code); | ||
|
||
$fileSegments = new FileSegments(); | ||
$visitor = new ClassVisitor($fileSegments); | ||
|
||
$traverser = new NodeTraverser(); | ||
$traverser->addVisitor($visitor); | ||
$traverser->traverse($ast); | ||
|
||
$this->assertEquals('TestClass', $fileSegments->getClass()->name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
/* | ||
* This file is developed by evoWeb. | ||
* | ||
* It is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License, either version 2 | ||
* of the License, or any later version. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Evoweb\Extender\Tests\Functional\Parser\Visitor; | ||
|
||
use Evoweb\Extender\Parser\FileSegments; | ||
use Evoweb\Extender\Parser\Visitor\ConstantVisitor; | ||
use Evoweb\Extender\Tests\Functional\AbstractTestBase; | ||
use PhpParser\Node\Const_; | ||
use PhpParser\Node\Scalar\Int_; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\Node\Stmt\ClassConst; | ||
use PhpParser\NodeTraverser; | ||
use PhpParser\ParserFactory; | ||
use PhpParser\PhpVersion; | ||
use PHPUnit\Framework\Attributes\Test; | ||
|
||
class ConstantVisitorTest extends AbstractTestBase | ||
{ | ||
#[Test] | ||
public function enterNode(): void | ||
{ | ||
$fileSegments = new FileSegments(); | ||
$visitor = new ConstantVisitor($fileSegments); | ||
|
||
$constant1 = new ClassConst([new Const_('Constant1', new Int_(1))]); | ||
$constant2 = new ClassConst([new Const_('Constant2', new Int_(2))]); | ||
|
||
$class = new Class_('TestClass'); | ||
$class->stmts[] = $constant1; | ||
$class->stmts[] = $constant2; | ||
|
||
$traverser = new NodeTraverser(); | ||
$traverser->addVisitor($visitor); | ||
$traverser->traverse([$class]); | ||
|
||
$this->assertEquals($constant2, $fileSegments->getClassConsts()[1]); | ||
} | ||
|
||
#[Test] | ||
public function parsedCode(): void | ||
{ | ||
$code = <<<'CODE' | ||
<?php | ||
class TestClass | ||
{ | ||
protected const constant1 = ''; | ||
protected const constant2 = ''; | ||
} | ||
CODE; | ||
$parser = (new ParserFactory())->createForVersion(PhpVersion::fromComponents(8, 2)); | ||
$ast = $parser->parse($code); | ||
|
||
$fileSegments = new FileSegments(); | ||
$visitor = new ConstantVisitor($fileSegments); | ||
|
||
$traverser = new NodeTraverser(); | ||
$traverser->addVisitor($visitor); | ||
$traverser->traverse($ast); | ||
|
||
$this->assertEquals('constant2', $fileSegments->getClassConsts()[1]->consts[0]->name); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
Tests/Functional/Parser/Visitor/ConstructorVisitorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
/* | ||
* This file is developed by evoWeb. | ||
* | ||
* It is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License, either version 2 | ||
* of the License, or any later version. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Evoweb\Extender\Tests\Functional\Parser\Visitor; | ||
|
||
use Evoweb\Extender\Parser\FileSegments; | ||
use Evoweb\Extender\Parser\Visitor\ConstructorVisitor; | ||
use Evoweb\Extender\Tests\Functional\AbstractTestBase; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PhpParser\NodeTraverser; | ||
use PhpParser\ParserFactory; | ||
use PhpParser\PhpVersion; | ||
use PHPUnit\Framework\Attributes\Test; | ||
|
||
class ConstructorVisitorTest extends AbstractTestBase | ||
{ | ||
#[Test] | ||
public function enterNode(): void | ||
{ | ||
$fileSegments = new FileSegments(); | ||
$visitor = new ConstructorVisitor($fileSegments); | ||
|
||
$classMethode1 = new ClassMethod('Method1'); | ||
$classMethode2 = new ClassMethod('__construct'); | ||
|
||
$class = new Class_('TestClass'); | ||
$class->stmts[] = $classMethode1; | ||
$class->stmts[] = $classMethode2; | ||
|
||
$traverser = new NodeTraverser(); | ||
$traverser->addVisitor($visitor); | ||
$traverser->traverse([$class]); | ||
|
||
$this->assertEquals($classMethode2, $fileSegments->getConstructor()); | ||
} | ||
|
||
#[Test] | ||
public function parsedCode(): void | ||
{ | ||
$code = <<<'CODE' | ||
<?php | ||
class TestClass | ||
{ | ||
public function __construct() {} | ||
} | ||
CODE; | ||
$parser = (new ParserFactory())->createForVersion(PhpVersion::fromComponents(8, 2)); | ||
$ast = $parser->parse($code); | ||
|
||
$fileSegments = new FileSegments(); | ||
$visitor = new ConstructorVisitor($fileSegments); | ||
|
||
$traverser = new NodeTraverser(); | ||
$traverser->addVisitor($visitor); | ||
$traverser->traverse($ast); | ||
|
||
$this->assertEquals('__construct', $fileSegments->getConstructor()->name); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
Tests/Functional/Parser/Visitor/InitializeObjectVisitorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
/* | ||
* This file is developed by evoWeb. | ||
* | ||
* It is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License, either version 2 | ||
* of the License, or any later version. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Evoweb\Extender\Tests\Functional\Parser\Visitor; | ||
|
||
use Evoweb\Extender\Parser\FileSegments; | ||
use Evoweb\Extender\Parser\Visitor\InitializeObjectVisitor; | ||
use Evoweb\Extender\Tests\Functional\AbstractTestBase; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PhpParser\NodeTraverser; | ||
use PhpParser\ParserFactory; | ||
use PhpParser\PhpVersion; | ||
use PHPUnit\Framework\Attributes\Test; | ||
|
||
class InitializeObjectVisitorTest extends AbstractTestBase | ||
{ | ||
#[Test] | ||
public function enterNode(): void | ||
{ | ||
$fileSegments = new FileSegments(); | ||
$visitor = new InitializeObjectVisitor($fileSegments); | ||
|
||
$constructMethode = new ClassMethod('__construct'); | ||
$initializeObjectMethode = new ClassMethod('initializeObject'); | ||
|
||
$class = new Class_('TestClass'); | ||
$class->stmts[] = $constructMethode; | ||
$class->stmts[] = $initializeObjectMethode; | ||
|
||
$traverser = new NodeTraverser(); | ||
$traverser->addVisitor($visitor); | ||
$traverser->traverse([$class]); | ||
|
||
$this->assertEquals($initializeObjectMethode, $fileSegments->getInitializeObject()); | ||
} | ||
|
||
#[Test] | ||
public function parsedCode(): void | ||
{ | ||
$code = <<<'CODE' | ||
<?php | ||
class TestClass | ||
{ | ||
public function __construct() {} | ||
public function initializeObject() {} | ||
} | ||
CODE; | ||
$parser = (new ParserFactory())->createForVersion(PhpVersion::fromComponents(8, 2)); | ||
$ast = $parser->parse($code); | ||
|
||
$fileSegments = new FileSegments(); | ||
$visitor = new InitializeObjectVisitor($fileSegments); | ||
|
||
$traverser = new NodeTraverser(); | ||
$traverser->addVisitor($visitor); | ||
$traverser->traverse($ast); | ||
|
||
$this->assertEquals('initializeObject', $fileSegments->getInitializeObject()->name); | ||
} | ||
} |
Oops, something went wrong.