<?php

namespace MediaVorus\Media;

use Doctrine\Common\Annotations\AnnotationRegistry;
use JMS\Serializer\SerializerBuilder;
use MediaVorus\TestCase;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;

class MediaTestCase extends TestCase
{
    protected function getSerializer()
    {
        $annotationPath = __DIR__ . '/../../../../vendor/jms/serializer/src/Annotation';
        $namespace = 'JMS\\Serializer\\Annotation';

        if (is_dir($annotationPath)) {
            $rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($annotationPath));

            foreach ($rii as $file) {
                if (! $file->isFile() || $file->getExtension() !== 'php') {
                    continue;
                }

                // Build fully-qualified class name
                $relativePath = substr($file->getPathname(), strlen($annotationPath) + 1);
                $class = $namespace . '\\' . str_replace(['/', '\\', '.php'], ['\\', '\\', ''], $relativePath);

                // Load annotation class (optional)
                if (class_exists($class)) {
                    // Only for doctrine/annotations >= 2.0
                    AnnotationRegistry::loadAnnotationClass($class);
                }
            }
        }

        return SerializerBuilder::create()
            ->setCacheDir(__DIR__ . '/../../../../cache')
            ->setDebug(true)
            ->build();
    }
}

