4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Tests\Mapping;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Validator\Mapping\GetterMetadata;
16 use Symfony\Component\Validator\Tests\Fixtures\Entity;
18 class GetterMetadataTest extends TestCase
20 const CLASSNAME = 'Symfony\Component\Validator\Tests\Fixtures\Entity';
22 public function testInvalidPropertyName()
24 $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\ValidatorException');
26 new GetterMetadata(self::CLASSNAME, 'foobar');
29 public function testGetPropertyValueFromPublicGetter()
31 // private getters don't work yet because ReflectionMethod::setAccessible()
32 // does not exist yet in a stable PHP release
34 $entity = new Entity('foobar');
35 $metadata = new GetterMetadata(self::CLASSNAME, 'internal');
37 $this->assertEquals('foobar from getter', $metadata->getPropertyValue($entity));
40 public function testGetPropertyValueFromOverriddenPublicGetter()
42 $entity = new Entity();
43 $metadata = new GetterMetadata(self::CLASSNAME, 'data');
45 $this->assertEquals('Overridden data', $metadata->getPropertyValue($entity));
48 public function testGetPropertyValueFromIsser()
50 $entity = new Entity();
51 $metadata = new GetterMetadata(self::CLASSNAME, 'valid', 'isValid');
53 $this->assertEquals('valid', $metadata->getPropertyValue($entity));
56 public function testGetPropertyValueFromHasser()
58 $entity = new Entity();
59 $metadata = new GetterMetadata(self::CLASSNAME, 'permissions');
61 $this->assertEquals('permissions', $metadata->getPropertyValue($entity));
65 * @expectedException \Symfony\Component\Validator\Exception\ValidatorException
66 * @expectedExceptionMessage The hasLastName() method does not exist in class Symfony\Component\Validator\Tests\Fixtures\Entity.
68 public function testUndefinedMethodNameThrowsException()
70 new GetterMetadata(self::CLASSNAME, 'lastName', 'hasLastName');