YamlDriverTest.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /*
  3. * Copyright 2011 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\SerializerBundle\Tests\Metadata\Driver;
  18. use Metadata\Driver\FileLocator;
  19. use JMS\SerializerBundle\Metadata\PropertyMetadata;
  20. use JMS\SerializerBundle\Metadata\Driver\YamlDriver;
  21. class YamlDriverTest extends BaseDriverTest
  22. {
  23. public function testBlogPostExcludeAllStrategy()
  24. {
  25. $m = $this->getDriver('exclude_all')->loadMetadataForClass(new \ReflectionClass('JMS\SerializerBundle\Tests\Fixtures\BlogPost'));
  26. $this->assertArrayHasKey('title', $m->propertyMetadata);
  27. $excluded = array('createdAt', 'published', 'comments', 'author');
  28. foreach ($excluded as $key) {
  29. $this->assertArrayNotHasKey($key, $m->propertyMetadata);
  30. }
  31. }
  32. public function testBlogPostExcludeNoneStrategy()
  33. {
  34. $m = $this->getDriver('exclude_none')->loadMetadataForClass(new \ReflectionClass('JMS\SerializerBundle\Tests\Fixtures\BlogPost'));
  35. $this->assertArrayNotHasKey('title', $m->propertyMetadata);
  36. $excluded = array('createdAt', 'published', 'comments', 'author');
  37. foreach ($excluded as $key) {
  38. $this->assertArrayHasKey($key, $m->propertyMetadata);
  39. }
  40. }
  41. public function testBlogPostCaseInsensitive()
  42. {
  43. $m = $this->getDriver('case')->loadMetadataForClass(new \ReflectionClass('JMS\SerializerBundle\Tests\Fixtures\BlogPost'));
  44. $p = new PropertyMetadata($m->name, 'title');
  45. $p->type = 'string';
  46. $this->assertEquals($p, $m->propertyMetadata['title']);
  47. }
  48. public function testBlogPostAccessor()
  49. {
  50. $m = $this->getDriver('accessor')->loadMetadataForClass(new \ReflectionClass('JMS\SerializerBundle\Tests\Fixtures\BlogPost'));
  51. $this->assertArrayHasKey('title', $m->propertyMetadata);
  52. $p = new PropertyMetadata($m->name, 'title');
  53. $p->getter = 'getOtherTitle';
  54. $p->setter = 'setOtherTitle';
  55. $this->assertEquals($p, $m->propertyMetadata['title']);
  56. }
  57. protected function getDriver()
  58. {
  59. $append = '';
  60. if (func_num_args() == 1) {
  61. $append = '/'.func_get_arg(0);
  62. }
  63. return new YamlDriver(new FileLocator(array(
  64. 'JMS\SerializerBundle\Tests\Fixtures' => __DIR__.'/yml'.$append,
  65. )));
  66. }
  67. }