GetterMetadataTest.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Tests\Mapping;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Validator\Mapping\GetterMetadata;
  13. use Symfony\Component\Validator\Tests\Fixtures\Entity;
  14. class GetterMetadataTest extends TestCase
  15. {
  16. const CLASSNAME = 'Symfony\Component\Validator\Tests\Fixtures\Entity';
  17. public function testInvalidPropertyName()
  18. {
  19. $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\ValidatorException');
  20. new GetterMetadata(self::CLASSNAME, 'foobar');
  21. }
  22. public function testGetPropertyValueFromPublicGetter()
  23. {
  24. // private getters don't work yet because ReflectionMethod::setAccessible()
  25. // does not exist yet in a stable PHP release
  26. $entity = new Entity('foobar');
  27. $metadata = new GetterMetadata(self::CLASSNAME, 'internal');
  28. $this->assertEquals('foobar from getter', $metadata->getPropertyValue($entity));
  29. }
  30. public function testGetPropertyValueFromOverriddenPublicGetter()
  31. {
  32. $entity = new Entity();
  33. $metadata = new GetterMetadata(self::CLASSNAME, 'data');
  34. $this->assertEquals('Overridden data', $metadata->getPropertyValue($entity));
  35. }
  36. public function testGetPropertyValueFromIsser()
  37. {
  38. $entity = new Entity();
  39. $metadata = new GetterMetadata(self::CLASSNAME, 'valid', 'isValid');
  40. $this->assertEquals('valid', $metadata->getPropertyValue($entity));
  41. }
  42. public function testGetPropertyValueFromHasser()
  43. {
  44. $entity = new Entity();
  45. $metadata = new GetterMetadata(self::CLASSNAME, 'permissions');
  46. $this->assertEquals('permissions', $metadata->getPropertyValue($entity));
  47. }
  48. /**
  49. * @expectedException \Symfony\Component\Validator\Exception\ValidatorException
  50. * @expectedExceptionMessage The hasLastName() method does not exist in class Symfony\Component\Validator\Tests\Fixtures\Entity.
  51. */
  52. public function testUndefinedMethodNameThrowsException()
  53. {
  54. new GetterMetadata(self::CLASSNAME, 'lastName', 'hasLastName');
  55. }
  56. }