PropertyAccessorArrayAccessTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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\PropertyAccess\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\PropertyAccess\PropertyAccess;
  13. use Symfony\Component\PropertyAccess\PropertyAccessor;
  14. abstract class PropertyAccessorArrayAccessTest extends TestCase
  15. {
  16. /**
  17. * @var PropertyAccessor
  18. */
  19. protected $propertyAccessor;
  20. protected function setUp()
  21. {
  22. $this->propertyAccessor = new PropertyAccessor();
  23. }
  24. abstract protected function getContainer(array $array);
  25. public function getValidPropertyPaths()
  26. {
  27. return array(
  28. array($this->getContainer(array('firstName' => 'Bernhard')), '[firstName]', 'Bernhard'),
  29. array($this->getContainer(array('person' => $this->getContainer(array('firstName' => 'Bernhard')))), '[person][firstName]', 'Bernhard'),
  30. );
  31. }
  32. /**
  33. * @dataProvider getValidPropertyPaths
  34. */
  35. public function testGetValue($collection, $path, $value)
  36. {
  37. $this->assertSame($value, $this->propertyAccessor->getValue($collection, $path));
  38. }
  39. /**
  40. * @expectedException \Symfony\Component\PropertyAccess\Exception\NoSuchIndexException
  41. */
  42. public function testGetValueFailsIfNoSuchIndex()
  43. {
  44. $this->propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()
  45. ->enableExceptionOnInvalidIndex()
  46. ->getPropertyAccessor();
  47. $object = $this->getContainer(array('firstName' => 'Bernhard'));
  48. $this->propertyAccessor->getValue($object, '[lastName]');
  49. }
  50. /**
  51. * @dataProvider getValidPropertyPaths
  52. */
  53. public function testSetValue($collection, $path)
  54. {
  55. $this->propertyAccessor->setValue($collection, $path, 'Updated');
  56. $this->assertSame('Updated', $this->propertyAccessor->getValue($collection, $path));
  57. }
  58. /**
  59. * @dataProvider getValidPropertyPaths
  60. */
  61. public function testIsReadable($collection, $path)
  62. {
  63. $this->assertTrue($this->propertyAccessor->isReadable($collection, $path));
  64. }
  65. /**
  66. * @dataProvider getValidPropertyPaths
  67. */
  68. public function testIsWritable($collection, $path)
  69. {
  70. $this->assertTrue($this->propertyAccessor->isWritable($collection, $path));
  71. }
  72. }