1<?php 2 3declare(strict_types=1); 4 5namespace Metadata\Tests\Driver; 6 7use Metadata\Driver\FileLocator; 8use PHPUnit\Framework\TestCase; 9 10class FileLocatorTest extends TestCase 11{ 12 public function testFindFileForClass() 13 { 14 $locator = new FileLocator([ 15 'Metadata\Tests\Driver\Fixture\A' => __DIR__ . '/Fixture/A', 16 'Metadata\Tests\Driver\Fixture\B' => __DIR__ . '/Fixture/B', 17 'Metadata\Tests\Driver\Fixture\C' => __DIR__ . '/Fixture/C', 18 ]); 19 20 $ref = new \ReflectionClass('Metadata\Tests\Driver\Fixture\A\A'); 21 $this->assertEquals(realpath(__DIR__ . '/Fixture/A/A.xml'), realpath($locator->findFileForClass($ref, 'xml'))); 22 23 $ref = new \ReflectionClass('Metadata\Tests\Driver\Fixture\B\B'); 24 $this->assertNull($locator->findFileForClass($ref, 'xml')); 25 26 $ref = new \ReflectionClass('Metadata\Tests\Driver\Fixture\C\SubDir\C'); 27 $this->assertEquals(realpath(__DIR__ . '/Fixture/C/SubDir.C.yml'), realpath($locator->findFileForClass($ref, 'yml'))); 28 } 29 30 public function testTraits() 31 { 32 if (version_compare(PHP_VERSION, '5.4.0', '<')) { 33 $this->markTestSkipped('No traits available'); 34 } 35 36 $locator = new FileLocator([ 37 'Metadata\Tests\Driver\Fixture\T' => __DIR__ . '/Fixture/T', 38 ]); 39 40 $ref = new \ReflectionClass('Metadata\Tests\Driver\Fixture\T\T'); 41 $this->assertEquals(realpath(__DIR__ . '/Fixture/T/T.xml'), realpath($locator->findFileForClass($ref, 'xml'))); 42 } 43 44 public function testFindFileForGlobalNamespacedClass() 45 { 46 $locator = new FileLocator([ 47 '' => __DIR__ . '/Fixture/D', 48 ]); 49 50 require_once __DIR__ . '/Fixture/D/D.php'; 51 $ref = new \ReflectionClass('D'); 52 $this->assertEquals(realpath(__DIR__ . '/Fixture/D/D.yml'), realpath($locator->findFileForClass($ref, 'yml'))); 53 } 54 55 public function testFindAllFiles() 56 { 57 $locator = new FileLocator([ 58 'Metadata\Tests\Driver\Fixture\A' => __DIR__ . '/Fixture/A', 59 'Metadata\Tests\Driver\Fixture\B' => __DIR__ . '/Fixture/B', 60 'Metadata\Tests\Driver\Fixture\C' => __DIR__ . '/Fixture/C', 61 'Metadata\Tests\Driver\Fixture\D' => __DIR__ . '/Fixture/D', 62 ]); 63 64 $this->assertCount(1, $xmlFiles = $locator->findAllClasses('xml')); 65 $this->assertSame('Metadata\Tests\Driver\Fixture\A\A', $xmlFiles[0]); 66 67 $this->assertCount(3, $ymlFiles = $locator->findAllClasses('yml')); 68 $this->assertSame('Metadata\Tests\Driver\Fixture\B\B', $ymlFiles[0]); 69 $this->assertSame('Metadata\Tests\Driver\Fixture\C\SubDir\C', $ymlFiles[1]); 70 $this->assertSame('Metadata\Tests\Driver\Fixture\D\D', $ymlFiles[2]); 71 } 72} 73