From b50ffe9928a25aa8be8a8a04dbdb4f4ae12d4175 Mon Sep 17 00:00:00 2001 From: webchick Date: Tue, 7 Jan 2014 23:49:45 -0800 Subject: [PATCH] Issue #2159737 by damiankloip: Unit test the Drupal\serialization\Normalizer\NormalizerBase class. --- .../Normalizer/NormalizerBase.php | 3 +- .../Tests/Normalizer/NormalizerBaseTest.php | 95 +++++++++++++++++++ 2 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 core/modules/serialization/tests/Drupal/serialization/Tests/Normalizer/NormalizerBaseTest.php diff --git a/core/modules/serialization/lib/Drupal/serialization/Normalizer/NormalizerBase.php b/core/modules/serialization/lib/Drupal/serialization/Normalizer/NormalizerBase.php index cb5094985b0..6b5b65d098c 100644 --- a/core/modules/serialization/lib/Drupal/serialization/Normalizer/NormalizerBase.php +++ b/core/modules/serialization/lib/Drupal/serialization/Normalizer/NormalizerBase.php @@ -7,7 +7,6 @@ namespace Drupal\serialization\Normalizer; -use Symfony\Component\Serializer\Exception\RuntimeException; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use Symfony\Component\Serializer\Normalizer\SerializerAwareNormalizer; @@ -27,7 +26,7 @@ abstract class NormalizerBase extends SerializerAwareNormalizer implements Norma * Implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface::supportsNormalization(). */ public function supportsNormalization($data, $format = NULL) { - return is_object($data) && ($data instanceof $this->supportedInterfaceOrClass); + return is_object($data) && (isset($this->supportedInterfaceOrClass) && ($data instanceof $this->supportedInterfaceOrClass)); } } diff --git a/core/modules/serialization/tests/Drupal/serialization/Tests/Normalizer/NormalizerBaseTest.php b/core/modules/serialization/tests/Drupal/serialization/Tests/Normalizer/NormalizerBaseTest.php new file mode 100644 index 00000000000..f03755cb7b1 --- /dev/null +++ b/core/modules/serialization/tests/Drupal/serialization/Tests/Normalizer/NormalizerBaseTest.php @@ -0,0 +1,95 @@ + 'NormalizerBase', + 'description' => 'Tests the abstract NormalizerBase class.', + 'group' => 'Serialization', + ); + } + + /** + * Tests the supportsNormalization method. + * + * @dataProvider providerTestSupportsNormalization + * + * @param bool $expected_return + * The expected boolean return value from supportNormalization. + * @param mixed $data + * The data passed to supportsNormalization. + * @param string $supported_interface_or_class + * (optional) the supported interface or class to set on the normalizer. + */ + public function testSupportsNormalization($expected_return, $data, $supported_interface_or_class = NULL) { + $normalizer_base = $this->getMockForAbstractClass('Drupal\serialization\Tests\Normalizer\TestNormalizerBase'); + + if (isset($supported_interface_or_class)) { + $normalizer_base->setSupportedInterfaceOrClass($supported_interface_or_class); + } + + $this->assertSame($expected_return, $normalizer_base->supportsNormalization($data)); + } + + /** + * Data provider for testSupportsNormalization. + * + * @return array + * An array of provider data for testSupportsNormalization. + */ + public function providerTestSupportsNormalization() { + return array( + // Something that is not an object should return FALSE immediately. + array(FALSE, array()), + // An object with no class set should return FALSE. + array(FALSE, new \stdClass()), + // Set a supported Class. + array(TRUE, new \stdClass(), 'stdClass'), + // Set a supported interface. + array(TRUE, new \RecursiveArrayIterator(), 'RecursiveIterator'), + // Set a different class. + array(FALSE, new \stdClass(), 'ArrayIterator'), + // Set a different interface. + array(FALSE, new \stdClass(), 'RecursiveIterator'), + ); + } + +} + +/** + * Test class for NormalizerBase. + */ +abstract class TestNormalizerBase extends NormalizerBase { + + /** + * Sets the protected supportedInterfaceOrClass property. + * + * @param string $supported_interface_or_class + * The class name to set. + */ + public function setSupportedInterfaceOrClass($supported_interface_or_class) { + $this->supportedInterfaceOrClass = $supported_interface_or_class; + } + +}