1<?php 2 3declare(strict_types=1); 4 5namespace JMS\Serializer\Tests\Metadata\Driver; 6 7use JMS\Serializer\Metadata\Driver\YamlDriver; 8use JMS\Serializer\Metadata\PropertyMetadata; 9use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy; 10use Metadata\Driver\FileLocator; 11 12class YamlDriverTest extends BaseDriverTest 13{ 14 public function testAccessorOrderIsInferred() 15 { 16 $m = $this->getDriverForSubDir('accessor_inferred')->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\Person')); 17 self::assertEquals(['age', 'name'], array_keys($m->propertyMetadata)); 18 } 19 20 public function testShortExposeSyntax() 21 { 22 $m = $this->getDriverForSubDir('short_expose')->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\Person')); 23 24 self::assertArrayHasKey('name', $m->propertyMetadata); 25 self::assertArrayNotHasKey('age', $m->propertyMetadata); 26 } 27 28 public function testBlogPost() 29 { 30 $m = $this->getDriverForSubDir('exclude_all')->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\BlogPost')); 31 32 self::assertArrayHasKey('title', $m->propertyMetadata); 33 34 $excluded = ['createdAt', 'published', 'comments', 'author']; 35 foreach ($excluded as $key) { 36 self::assertArrayNotHasKey($key, $m->propertyMetadata); 37 } 38 } 39 40 public function testBlogPostExcludeNoneStrategy() 41 { 42 $m = $this->getDriverForSubDir('exclude_none')->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\BlogPost')); 43 44 self::assertArrayNotHasKey('title', $m->propertyMetadata); 45 46 $excluded = ['createdAt', 'published', 'comments', 'author']; 47 foreach ($excluded as $key) { 48 self::assertArrayHasKey($key, $m->propertyMetadata); 49 } 50 } 51 52 public function testBlogPostCaseInsensitive() 53 { 54 $m = $this->getDriverForSubDir('case')->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\BlogPost')); 55 56 $p = new PropertyMetadata($m->name, 'title'); 57 $p->serializedName = 'title'; 58 $p->type = ['name' => 'string', 'params' => []]; 59 self::assertEquals($p, $m->propertyMetadata['title']); 60 } 61 62 public function testBlogPostAccessor() 63 { 64 $m = $this->getDriverForSubDir('accessor')->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\BlogPost')); 65 66 self::assertArrayHasKey('title', $m->propertyMetadata); 67 68 $p = new PropertyMetadata($m->name, 'title'); 69 $p->getter = 'getOtherTitle'; 70 $p->setter = 'setOtherTitle'; 71 $p->serializedName = 'title'; 72 self::assertEquals($p, $m->propertyMetadata['title']); 73 } 74 75 private function getDriverForSubDir($subDir = null) 76 { 77 return new YamlDriver(new FileLocator([ 78 'JMS\Serializer\Tests\Fixtures' => __DIR__ . '/yml' . ($subDir ? '/' . $subDir : ''), 79 '' => __DIR__ . '/yml/_' . ($subDir ? '/' . $subDir : ''), 80 ]), new IdenticalPropertyNamingStrategy(), null, $this->getExpressionEvaluator()); 81 } 82 83 protected function getDriver() 84 { 85 return $this->getDriverForSubDir(); 86 } 87} 88