Skip to content

Commit

Permalink
[TASK] Add functional tests to all visitors
Browse files Browse the repository at this point in the history
Closes #34
  • Loading branch information
garbast committed Nov 23, 2024
1 parent 8dac7a4 commit 95389c7
Show file tree
Hide file tree
Showing 9 changed files with 646 additions and 0 deletions.
72 changes: 72 additions & 0 deletions Tests/Functional/Parser/Visitor/ClassMethodVisitorTest.php
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);
}
}
64 changes: 64 additions & 0 deletions Tests/Functional/Parser/Visitor/ClassVisitorTest.php
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);
}
}
74 changes: 74 additions & 0 deletions Tests/Functional/Parser/Visitor/ConstantVisitorTest.php
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 Tests/Functional/Parser/Visitor/ConstructorVisitorTest.php
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 Tests/Functional/Parser/Visitor/InitializeObjectVisitorTest.php
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);
}
}
Loading

0 comments on commit 95389c7

Please sign in to comment.