YamlDriverTest.php 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /*
  3. * Copyright 2013 Johannes M. Schmitt <schmittjoh@gmail.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace JMS\Serializer\Tests\Metadata\Driver;
  18. use Metadata\Driver\FileLocator;
  19. use JMS\Serializer\Metadata\PropertyMetadata;
  20. use JMS\Serializer\Metadata\Driver\YamlDriver;
  21. class YamlDriverTest extends BaseDriverTest
  22. {
  23. public function testAccessorOrderIsInferred()
  24. {
  25. $m = $this->getDriverForSubDir('accessor_inferred')->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\Person'));
  26. $this->assertEquals(array('age', 'name'), array_keys($m->propertyMetadata));
  27. }
  28. public function testShortExposeSyntax()
  29. {
  30. $m = $this->getDriverForSubDir('short_expose')->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\Person'));
  31. $this->assertArrayHasKey('name', $m->propertyMetadata);
  32. $this->assertArrayNotHasKey('age', $m->propertyMetadata);
  33. }
  34. public function testBlogPost()
  35. {
  36. $m = $this->getDriverForSubDir('exclude_all')->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\BlogPost'));
  37. $this->assertArrayHasKey('title', $m->propertyMetadata);
  38. $excluded = array('createdAt', 'published', 'comments', 'author');
  39. foreach ($excluded as $key) {
  40. $this->assertArrayNotHasKey($key, $m->propertyMetadata);
  41. }
  42. }
  43. public function testBlogPostExcludeNoneStrategy()
  44. {
  45. $m = $this->getDriverForSubDir('exclude_none')->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\BlogPost'));
  46. $this->assertArrayNotHasKey('title', $m->propertyMetadata);
  47. $excluded = array('createdAt', 'published', 'comments', 'author');
  48. foreach ($excluded as $key) {
  49. $this->assertArrayHasKey($key, $m->propertyMetadata);
  50. }
  51. }
  52. public function testBlogPostCaseInsensitive()
  53. {
  54. $m = $this->getDriverForSubDir('case')->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\BlogPost'));
  55. $p = new PropertyMetadata($m->name, 'title');
  56. $p->type = array('name' => 'string', 'params' => array());
  57. $this->assertEquals($p, $m->propertyMetadata['title']);
  58. }
  59. public function testBlogPostAccessor()
  60. {
  61. $m = $this->getDriverForSubDir('accessor')->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\BlogPost'));
  62. $this->assertArrayHasKey('title', $m->propertyMetadata);
  63. $p = new PropertyMetadata($m->name, 'title');
  64. $p->getter = 'getOtherTitle';
  65. $p->setter = 'setOtherTitle';
  66. $this->assertEquals($p, $m->propertyMetadata['title']);
  67. }
  68. private function getDriverForSubDir($subDir = null)
  69. {
  70. return new YamlDriver(new FileLocator(array(
  71. 'JMS\Serializer\Tests\Fixtures' => __DIR__.'/yml' . ($subDir ? '/'.$subDir : ''),
  72. )));
  73. }
  74. protected function getDriver()
  75. {
  76. return $this->getDriverForSubDir();
  77. }
  78. }