From 0fe4be929389d6e8a14768c4695657c757d4dbb3 Mon Sep 17 00:00:00 2001 From: Guilhem N Date: Tue, 26 Jul 2016 16:23:09 +0200 Subject: [PATCH 1/2] [PropertyInfo] Fix an error in PropertyInfoCacheExtractor --- .../PropertyInfoCacheExtractor.php | 2 +- .../Tests/Fixtures/NullExtractor.php | 18 ++++++++++ .../Tests/PropertyInfoCacheExtractorTest.php | 35 ++++++++++++++++--- 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/PropertyInfo/PropertyInfoCacheExtractor.php b/src/Symfony/Component/PropertyInfo/PropertyInfoCacheExtractor.php index b7d5c6d372ca..7bde2bc0631d 100644 --- a/src/Symfony/Component/PropertyInfo/PropertyInfoCacheExtractor.php +++ b/src/Symfony/Component/PropertyInfo/PropertyInfoCacheExtractor.php @@ -86,7 +86,7 @@ public function getProperties($class, array $context = array()) */ public function getTypes($class, $property, array $context = array()) { - return $this->extract('getTypes', array($class, $context)); + return $this->extract('getTypes', array($class, $property, $context)); } /** diff --git a/src/Symfony/Component/PropertyInfo/Tests/Fixtures/NullExtractor.php b/src/Symfony/Component/PropertyInfo/Tests/Fixtures/NullExtractor.php index a1ef78209342..d0285d87a536 100644 --- a/src/Symfony/Component/PropertyInfo/Tests/Fixtures/NullExtractor.php +++ b/src/Symfony/Component/PropertyInfo/Tests/Fixtures/NullExtractor.php @@ -28,6 +28,8 @@ class NullExtractor implements PropertyListExtractorInterface, PropertyDescripti */ public function getShortDescription($class, $property, array $context = array()) { + $this->assertIsString($class); + $this->assertIsString($property); } /** @@ -35,6 +37,8 @@ public function getShortDescription($class, $property, array $context = array()) */ public function getLongDescription($class, $property, array $context = array()) { + $this->assertIsString($class); + $this->assertIsString($property); } /** @@ -42,6 +46,8 @@ public function getLongDescription($class, $property, array $context = array()) */ public function getTypes($class, $property, array $context = array()) { + $this->assertIsString($class); + $this->assertIsString($property); } /** @@ -49,6 +55,8 @@ public function getTypes($class, $property, array $context = array()) */ public function isReadable($class, $property, array $context = array()) { + $this->assertIsString($class); + $this->assertIsString($property); } /** @@ -56,6 +64,8 @@ public function isReadable($class, $property, array $context = array()) */ public function isWritable($class, $property, array $context = array()) { + $this->assertIsString($class); + $this->assertIsString($property); } /** @@ -63,5 +73,13 @@ public function isWritable($class, $property, array $context = array()) */ public function getProperties($class, array $context = array()) { + $this->assertIsString($class); + } + + private function assertIsString($string) + { + if (!is_string($string)) { + throw new \Exception(sprintf('"%s" expects strings, given "%s".', __CLASS__, gettype($string))); + } } } diff --git a/src/Symfony/Component/PropertyInfo/Tests/PropertyInfoCacheExtractorTest.php b/src/Symfony/Component/PropertyInfo/Tests/PropertyInfoCacheExtractorTest.php index ce3ade2d94ec..455d39fa96c2 100644 --- a/src/Symfony/Component/PropertyInfo/Tests/PropertyInfoCacheExtractorTest.php +++ b/src/Symfony/Component/PropertyInfo/Tests/PropertyInfoCacheExtractorTest.php @@ -26,15 +26,40 @@ protected function setUp() $this->propertyInfo = new PropertyInfoCacheExtractor($this->propertyInfo, new ArrayAdapter()); } - public function testCache() + public function testGetShortDescription() { - $this->assertSame('short', $this->propertyInfo->getShortDescription('Foo', 'bar', array())); - $this->assertSame('short', $this->propertyInfo->getShortDescription('Foo', 'bar', array())); + parent::testGetShortDescription(); + parent::testGetShortDescription(); } - public function testNotSerializableContext() + public function testGetLongDescription() { - $this->assertSame('short', $this->propertyInfo->getShortDescription('Foo', 'bar', array('foo' => function () {}))); + parent::testGetLongDescription(); + parent::testGetLongDescription(); + } + + public function testGetTypes() + { + parent::testGetTypes(); + parent::testGetTypes(); + } + + public function testIsReadable() + { + parent::testIsReadable(); + parent::testIsReadable(); + } + + public function testIsWritable() + { + parent::testIsWritable(); + parent::testIsWritable(); + } + + public function testGetProperties() + { + parent::testGetProperties(); + parent::testGetProperties(); } /** From 7bc58bf1c5e2388681f681040e8251657fc248e2 Mon Sep 17 00:00:00 2001 From: Ener-Getick Date: Wed, 27 Jul 2016 14:16:13 +0200 Subject: [PATCH 2/2] [PropertyInfo] Fix array caching `null` values aren't cached because of the use of `isset` --- .../Component/PropertyInfo/PropertyInfoCacheExtractor.php | 2 +- .../Component/PropertyInfo/Tests/Fixtures/NullExtractor.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/PropertyInfo/PropertyInfoCacheExtractor.php b/src/Symfony/Component/PropertyInfo/PropertyInfoCacheExtractor.php index 7bde2bc0631d..69ade35dfeee 100644 --- a/src/Symfony/Component/PropertyInfo/PropertyInfoCacheExtractor.php +++ b/src/Symfony/Component/PropertyInfo/PropertyInfoCacheExtractor.php @@ -108,7 +108,7 @@ private function extract($method, array $arguments) $key = $this->escape($method.'.'.$serializedArguments); - if (isset($this->arrayCache[$key])) { + if (array_key_exists($key, $this->arrayCache)) { return $this->arrayCache[$key]; } diff --git a/src/Symfony/Component/PropertyInfo/Tests/Fixtures/NullExtractor.php b/src/Symfony/Component/PropertyInfo/Tests/Fixtures/NullExtractor.php index d0285d87a536..4c2af4112045 100644 --- a/src/Symfony/Component/PropertyInfo/Tests/Fixtures/NullExtractor.php +++ b/src/Symfony/Component/PropertyInfo/Tests/Fixtures/NullExtractor.php @@ -79,7 +79,7 @@ public function getProperties($class, array $context = array()) private function assertIsString($string) { if (!is_string($string)) { - throw new \Exception(sprintf('"%s" expects strings, given "%s".', __CLASS__, gettype($string))); + throw new \InvalidArgumentException(sprintf('"%s" expects strings, given "%s".', __CLASS__, gettype($string))); } } }