<?php

/*
 * This file is part of the PHPBench package
 *
 * (c) Daniel Leech <daniel@dantleech.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 */

namespace PhpBench\Tests\Unit\Benchmark\Metadata;

use PhpBench\Benchmark\Metadata\AnnotationReader;
use PhpBench\Benchmark\Metadata\Annotations;
use PhpBench\Reflection\ReflectionClass;
use PhpBench\Reflection\ReflectionMethod;
use PhpBench\Tests\TestCase;

class AnnotationReaderTest extends TestCase
{
    /**
     * It should read class annotations.
     */
    public function testLoadClassMetadata(): void
    {
        $reflection = new ReflectionClass();
        $reflection->class = \stdClass::class;
        $reflection->comment = <<<'EOT'
/**
 * @BeforeClassMethods({"beforeClass"})
 * @AfterClassMethods({"afterClass"})
 */
EOT;

        $annotations = $this->createReader()->getClassAnnotations($reflection);
        $this->assertCount(2, $annotations);
    }

    /**
     * It should read method annotations.
     */
    public function testLoadMethodMetadata(): void
    {
        $reflection = new ReflectionClass();
        $reflection->class = \stdClass::class;
        $reflectionMethod = new ReflectionMethod();
        $reflectionMethod->reflectionClass = $reflection;
        $reflectionMethod->comment = <<<'EOT'
/**
 * @Subject()
 * @Iterations(10)
 */
EOT;

        $annotations = $this->createReader()->getMethodAnnotations($reflectionMethod);
        $this->assertCount(2, $annotations);
    }

    /**
     * It should use imported annotations when configured to do so.
     */
    public function testImportedUse(): void
    {
        $reflection = new ReflectionClass();
        $reflection->class = \stdClass::class;
        $reflection->path = __DIR__ . '/classes/TestAnnotation.php';
        $reflectionMethod = new ReflectionMethod();
        $reflectionMethod->reflectionClass = $reflection;
        $reflectionMethod->comment = <<<'EOT'
/**
 * @PhpBench\Subject()
 * @PhpBench\Iterations(10)
 */
EOT;

        $annotations = $this->createReader(true)->getMethodAnnotations($reflectionMethod);
        $this->assertCount(2, $annotations);
    }

    private function createReader(bool $useImports = false): AnnotationReader
    {
        return new AnnotationReader($useImports);
    }
}
