Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test get attributes from params
Browse files Browse the repository at this point in the history
samsonasik committed Feb 2, 2024
1 parent 0398c3f commit 482f506
Showing 2 changed files with 71 additions and 0 deletions.
54 changes: 54 additions & 0 deletions tests/ReflectionAttributesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
declare(strict_types=1);

namespace Go\ParserReflection;

use PHPUnit\Framework\TestCase;

class ReflectionAttributesTest extends TestCase
{
/**
* @var ReflectionFile
*/
protected $parsedRefFile;

public function testGetAttributeOnParameters()
{
$this->setUpFile(__DIR__ . '/Stub/FileWithParameters80.php');

$fileNamespace = $this->parsedRefFile->getFileNamespace('Go\ParserReflection\Stub');
$parameters = $fileNamespace->getFunction('authenticate')->getParameters();

foreach ($parameters as $parameter) {
$attributes = $parameter->getAttributes();
foreach ($attributes as $attribute) {
$originalReflectionParameter = new \ReflectionParameter('Go\ParserReflection\Stub\authenticate', $parameter->getName());
$originalAttribute = current($originalReflectionParameter->getAttributes($attribute->getName()));

$this->assertInstanceOf(ReflectionAttribute::class, $attribute);
$this->assertSame($originalAttribute->getName(), $attribute->getName());
$this->assertSame($originalAttribute->getName(), $attribute->getName());
$this->assertSame($originalAttribute->getTarget(), $attribute->getTarget());
$this->assertSame($originalAttribute->getTarget(), $attribute->getTarget());
$this->assertSame($originalAttribute->getArguments(), $attribute->getArguments());
$this->assertSame($originalAttribute->isRepeated(), $attribute->isRepeated());
}
}
}

/**
* Setups file for parsing
*
* @param string $fileName File name to use
*/
private function setUpFile($fileName)
{
$fileName = stream_resolve_include_path($fileName);
$fileNode = ReflectionEngine::parseFile($fileName);

$reflectionFile = new ReflectionFile($fileName, $fileNode);
$this->parsedRefFile = $reflectionFile;

include_once $fileName;
}
}
17 changes: 17 additions & 0 deletions tests/Stub/FileWithClassConst80.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
/**
* Parser Reflection API
*
* @copyright Copyright 2015, Lisachenko Alexander <[email protected]>
*
* This source file is subject to the license that is bundled
* with this source code in the file LICENSE.
*/

namespace Go\ParserReflection\Stub;

class FileWithClassConstAttribute
{
public const FOO = 1;
}

0 comments on commit 482f506

Please sign in to comment.