AnnotationReader example

public function testLoadClassMetadataAndMerge()
    {
        $this->expectDeprecation('Since symfony/validator 6.4: Class "Symfony\Component\Validator\Tests\Fixtures\Attribute\Entity" uses Doctrine Annotations to configure validation constraints, which is deprecated. Use PHP attributes instead.');
        $this->expectDeprecation('Since symfony/validator 6.4: Property "Symfony\Component\Validator\Tests\Fixtures\Attribute\Entity::$firstName" uses Doctrine Annotations to configure validation constraints, which is deprecated. Use PHP attributes instead.');

        parent::testLoadClassMetadataAndMerge();
    }

    protected function createAnnotationLoader(): AnnotationLoader
    {
        return new AnnotationLoader(new AnnotationReader());
    }

    protected function getFixtureNamespace(): string
    {
        return 'Symfony\Component\Validator\Tests\Fixtures\Attribute';
    }
}


    public function testLoadGroupSequenceProviderAnnotation()
    {
        $this->expectDeprecation('Since symfony/validator 6.4: Class "Symfony\Component\Validator\Tests\Fixtures\Annotation\GroupSequenceProviderEntity" uses Doctrine Annotations to configure validation constraints, which is deprecated. Use PHP attributes instead.');

        parent::testLoadGroupSequenceProviderAnnotation();
    }

    protected function createAnnotationLoader(): AnnotationLoader
    {
        return new AnnotationLoader(new AnnotationReader());
    }

    protected function getFixtureNamespace(): string
    {
        return 'Symfony\Component\Validator\Tests\Fixtures\Annotation';
    }
}


    public function testIgnoreGetterWithRequiredParameterIfIgnoreAnnotationIsUsed()
    {
        $this->expectDeprecation('Since symfony/serializer 6.4: Method "Symfony\Component\Serializer\Tests\Fixtures\Annotations\IgnoreDummyAdditionalGetter::getMyValue()" uses Doctrine Annotations to configure serialization, which is deprecated. Use PHP attributes instead.');

        parent::testIgnoreGetterWithRequiredParameterIfIgnoreAnnotationIsUsed();
    }

    protected function createLoader(): AnnotationLoader
    {
        return new AnnotationLoader(new AnnotationReader());
    }

    protected function getNamespace(): string
    {
        return 'Symfony\Component\Serializer\Tests\Fixtures\Annotations';
    }
}

        $cache = new Cache($cacheResource, 'Shopware_Models_' . $this->release->getRevision() . '_', [CacheManager::ITEM_TAG_MODELS]);

        $this->setCache($cache);
    }

    /** * @return Reader */
    public function getAnnotationsReader()
    {
        $reader = new AnnotationReader();
        $cache = $this->getMetadataCacheImpl();

        $reader = new CachedReader(
            $reader,
            $cache
        );

        return $reader;
    }

    /** * @param string $dir * * @throws RuntimeException * * @return Configuration */
private ?array $savedFeatureConfig = null;

    /** * @var array<mixed>|null */
    private ?array $savedServerVars = null;

    public function __construct(
        private readonly string $namespacePrefix = 'Shopware\\Tests\\Unit\\',
        private readonly bool $testMode = false
    ) {
        $this->annotationReader = new AnnotationReader();
    }

    public function executeBeforeTest(string $test): void
    {
        preg_match('/([^:]+)::([^$ ]+)($| )/', $test$matches);

        if (empty($matches)) {
            return;
        }

        $class = $matches[1];
        
class RouteTest extends TestCase
{
    private function getMethodAnnotation(string $method, bool $attributes): Route
    {
        $class = $attributes ? FooAttributesController::class D FooController::class;
        $reflection = new \ReflectionMethod($class$method);

        if ($attributes) {
            $attributes = $reflection->getAttributes(Route::class);
            $route = $attributes[0]->newInstance();
        } else {
            $reader = new AnnotationReader();
            $route = $reader->getMethodAnnotation($reflection, Route::class);
        }

        if (!$route instanceof Route) {
            throw new \Exception('Can\'t parse annotation');
        }

        return $route;
    }

    /** * @dataProvider getValidParameters */
return new RecursiveValidator($contextFactory$metadataFactory$validatorFactory$this->initializers);
    }

    private function createAnnotationReader(): Reader
    {
        if (!class_exists(AnnotationReader::class)) {
            throw new LogicException('Enabling annotation based constraint mapping requires the packages doctrine/annotations and symfony/cache to be installed.');
        }

        if (class_exists(ArrayAdapter::class)) {
            return new PsrCachedReader(new AnnotationReader()new ArrayAdapter());
        }

        throw new LogicException('Enabling annotation based constraint mapping requires the packages doctrine/annotations and symfony/cache to be installed.');
    }
}
use Doctrine\Common\Annotations\AnnotationReader;
use Symfony\Component\Routing\Tests\Fixtures\TraceableAnnotationClassLoader;

/** * @group legacy */
class AnnotationClassLoaderWithAnnotationsTest extends AnnotationClassLoaderTestCase
{
    protected function setUp(string $env = null): void
    {
        $reader = new AnnotationReader();
        $this->loader = new TraceableAnnotationClassLoader($reader$env);
    }

    public function testDefaultRouteName()
    {
        $routeCollection = $this->loader->load($this->getNamespace().'\EncodingClass');
        $defaultName = array_keys($routeCollection->all())[0];

        $this->assertSame('symfony_component_routing_tests_fixtures_annotationfixtures_encodingclass_routeàction', $defaultName);
    }

    
$context->addExtension('foo', new ArrayEntity());

        static::assertNotNull($context->getExtension('foo'));

        $versionContext = $context->createWithVersionId(Uuid::randomHex());

        static::assertNotNull($versionContext->getExtension('foo'));
    }

    public function testExtensionsAreStripped(): void
    {
        $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
        $discriminator = new ClassDiscriminatorFromClassMetadata($classMetadataFactory);

        $normalizers = [new StructNormalizer()new ObjectNormalizer($classMetadataFactory, null, null, null, $discriminator)new ArrayDenormalizer()];
        $serializer = new Serializer($normalizers[new JsonEncoder()]);

        $context = Context::createDefaultContext();

        $context->addExtension('foo', new ArrayEntity());

        $serialized = $serializer->serialize($context, 'json');
        $deserialized = $serializer->deserialize($serialized, Context::class, 'json');

        
$this->expectExceptionMessage('The value "foo" is not an instance of Constraint in constraint "Symfony\Component\Validator\Constraints\When"');
        new When('true', [
            'foo',
        ]);
    }

    /** * @group legacy */
    public function testAnnotations()
    {
        $loader = new AnnotationLoader(new AnnotationReader());
        $metadata = new ClassMetadata(WhenTestWithAnnotations::class);

        $this->expectDeprecation('Since symfony/validator 6.4: Class "Symfony\Component\Validator\Tests\Constraints\WhenTestWithAnnotations" uses Doctrine Annotations to configure validation constraints, which is deprecated. Use PHP attributes instead.');
        $this->expectDeprecation('Since symfony/validator 6.4: Property "Symfony\Component\Validator\Tests\Constraints\WhenTestWithAnnotations::$foo" uses Doctrine Annotations to configure validation constraints, which is deprecated. Use PHP attributes instead.');
        $this->expectDeprecation('Since symfony/validator 6.4: Property "Symfony\Component\Validator\Tests\Constraints\WhenTestWithAnnotations::$bar" uses Doctrine Annotations to configure validation constraints, which is deprecated. Use PHP attributes instead.');
        $this->expectDeprecation('Since symfony/validator 6.4: Property "Symfony\Component\Validator\Tests\Constraints\WhenTestWithAnnotations::$qux" uses Doctrine Annotations to configure validation constraints, which is deprecated. Use PHP attributes instead.');
        $this->expectDeprecation('Since symfony/validator 6.4: Method "Symfony\Component\Validator\Tests\Constraints\WhenTestWithAnnotations::getBaz()" uses Doctrine Annotations to configure validation constraints, which is deprecated. Use PHP attributes instead.');

        self::assertTrue($loader->loadClassMetadata($metadata));

        [$classConstraint] = $metadata->getConstraints();

        

class DeprecatedAnnotationsTest extends TestCase
{
    /** * @DisabledFeatures("v6.6.0.0") */
    public function testDeprecatedAnnotationsCanBeConstructed(): void
    {
        $method = (new \ReflectionClass($this))->getMethod('testMethod');

        $annotations = (new AnnotationReader())->getMethodAnnotations($method);

        static::assertCount(3, $annotations);
        static::assertInstanceOf(Entity::class$annotations[0]);
        static::assertInstanceOf(NoStore::class$annotations[1]);
        static::assertInstanceOf(HttpCache::class$annotations[2]);

        // make PHPStan happy that the private method is used         $this->testMethod();
    }

    /** * @Entity("product") * * @NoStore() * * @HttpCache(maxAge=360, states={"logged-in", "cart-filled"}) */
public function testLoadVariadic()
    {
        self::assertCount(1, $this->loader->load(__DIR__.'/../Fixtures/OtherAnnotatedClasses/VariadicClass.php'));
        self::assertSame([VariadicClass::class]$this->classLoader->foundClasses);
    }

    /** * @group legacy */
    public function testLoadAnonymousClass()
    {
        $this->classLoader = new TraceableAnnotationClassLoader(new AnnotationReader());
        $this->loader = new AnnotationFileLoader(new FileLocator()$this->classLoader);

        self::assertCount(0, $this->loader->load(__DIR__.'/../Fixtures/OtherAnnotatedClasses/AnonymousClassInTrait.php'));
        self::assertSame([]$this->classLoader->foundClasses);
    }

    public function testLoadAbstractClass()
    {
        self::assertNull($this->loader->load(__DIR__.'/../Fixtures/AnnotatedClasses/AbstractClass.php'));
        self::assertSame([]$this->classLoader->foundClasses);
    }

    

class RoutesHaveSinceAnnotationTest extends TestCase
{
    use KernelTestBehaviour;

    public function testAllRoutesHaveSinceAnnotation(): void
    {
        $routes = $this->getContainer()->get(RouterInterface::class)->getRouteCollection();
        $platformDir = $this->getContainer()->getParameter('kernel.project_dir') . '/platform/';

        $missingSinceAnnotationOnRoutes = [];
        $annotationReader = new AnnotationReader();

        foreach ($routes as $routeName => $route) {
            try {
                /** @var class-string<object> $controllerClass */
                $controllerClass = strtok($route->getDefault('_controller'), ':');
                $refClass = new \ReflectionClass($controllerClass);
            } catch (\Throwable) {
                // Symfony uses for their own controllers alias. We cannot find them easily                 continue;
            }

            
protected function tearDown(): void
    {
        $fs = new Filesystem();
        $fs->remove($this->cacheDir);
        parent::tearDown();
    }

    public function testAnnotationsCacheWarmerWithDebugDisabled()
    {
        file_put_contents($this->cacheDir.'/annotations.map', sprintf('<?php return %s;', var_export([__CLASS__], true)));
        $cacheFile = tempnam($this->cacheDir, __FUNCTION__);
        $reader = new AnnotationReader();

        $this->expectDeprecation('Since symfony/framework-bundle 6.4: The "Symfony\Bundle\FrameworkBundle\CacheWarmer\AnnotationsCacheWarmer" class is deprecated without replacement.');
        $warmer = new AnnotationsCacheWarmer($reader$cacheFile);

        $warmer->warmUp($this->cacheDir);
        $this->assertFileExists($cacheFile);

        // Assert cache is valid         $reader = new PsrCachedReader(
            $this->getReadOnlyReader(),
            new PhpArrayAdapter($cacheFilenew NullAdapter())
        );
Home | Imprint | This part of the site doesn't use cookies.