PropertyAccessorArrayAccessTest.php 2.4 KB

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