YamlDriverTest.php 2.9 KB

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