From 038570297b8c3f82370efbf04a4fdd3e6b0ab157 Mon Sep 17 00:00:00 2001 From: Daniel Messenger Date: Fri, 2 Oct 2015 11:08:54 +0100 Subject: [PATCH 1/5] Improving test coverage --- test/ArraySerializableTest.php | 88 +++++++++++++++++++ test/ClassMethodsTest.php | 41 +++++++++ test/Filter/NumberOfParameterFilterTest.php | 11 +++ .../ClassMethodsTraversableOptions.php | 46 ++++++++++ 4 files changed, 186 insertions(+) create mode 100644 test/ArraySerializableTest.php create mode 100644 test/TestAsset/ClassMethodsTraversableOptions.php diff --git a/test/ArraySerializableTest.php b/test/ArraySerializableTest.php new file mode 100644 index 0000000..60af902 --- /dev/null +++ b/test/ArraySerializableTest.php @@ -0,0 +1,88 @@ +hydrator = new ArraySerializable(); + } + + + + /** + * Verify that we get an exception when trying to extract on a non-object + */ + public function testHydratorExtractThrowsExceptionOnNonObjectParameter() + { + $this->setExpectedException(BadMethodCallException::class); + $this->hydrator->extract('thisIsNotAnObject'); + } + + /** + * Verify that we get an exception when trying to hydrate a non-object + */ + public function testHydratorHydrateThrowsExceptionOnNonObjectParameter() + { + $this->setExpectedException(BadMethodCallException::class); + $this->hydrator->hydrate(['some' => 'data'], 'thisIsNotAnObject'); + } + + /** + * Verifies that we can extract from an ArraySerializableInterface + */ + public function testCanExtractFromArraySerializableObject() + { + $this->assertSame( + [ + 'foo' => 'bar', + 'bar' => 'foo', + 'blubb' => 'baz', + 'quo' => 'blubb', + ], + $this->hydrator->extract(new ArraySerializableAsset()) + ); + } + + /** + * Verifies we can hydrate an ArraySerializableInterface + */ + public function testCanHydrateToArraySerializableObject() + { + $data = [ + 'foo' => 'bar1', + 'bar' => 'foo1', + 'blubb' => 'baz1', + 'quo' => 'blubb1', + ]; + $object = $this->hydrator->hydrate($data, new ArraySerializableAsset()); + + $this->assertSame($data, $object->getArrayCopy()); + } +} diff --git a/test/ClassMethodsTest.php b/test/ClassMethodsTest.php index c6eedce..cdde62d 100644 --- a/test/ClassMethodsTest.php +++ b/test/ClassMethodsTest.php @@ -10,10 +10,13 @@ namespace ZendTest\Hydrator; use Zend\Hydrator\ClassMethods; +use Zend\Hydrator\Exception\BadMethodCallException; +use Zend\Hydrator\Exception\InvalidArgumentException; use ZendTest\Hydrator\TestAsset\ClassMethodsCamelCaseMissing; use ZendTest\Hydrator\TestAsset\ClassMethodsOptionalParameters; use ZendTest\Hydrator\TestAsset\ClassMethodsCamelCase; use ZendTest\Hydrator\TestAsset\ArraySerializable; +use ZendTest\Hydrator\TestAsset\ClassMethodsTraversableOptions; /** * Unit tests for {@see ClassMethods} @@ -73,4 +76,42 @@ public function testCanHydratedPromiscuousInstances() $arraySerializable->getArrayCopy() ); } + + /** + * Verifies the options must be an array or Traversable + */ + public function testSetOptionsThrowsInvalidArgumentException() + { + $this->setExpectedException(InvalidArgumentException::class); + $this->hydrator->setOptions('invalid options'); + } + + /** + * Verifies options can be set from a Traversable object + */ + public function testSetOptionsFromTraversable() + { + $options = new ClassMethodsTraversableOptions(); + $this->hydrator->setOptions($options); + + $this->assertSame(false, $this->hydrator->getUnderscoreSeparatedKeys()); + } + + /** + * Verifies a BadMethodCallException is thrown for extracting a non-object + */ + public function testExtractNonObjectThrowsBadMethodCallException() + { + $this->setExpectedException(BadMethodCallException::class); + $this->hydrator->extract('non-object'); + } + + /** + * Verifies a BadMethodCallException is thrown for hydrating a non-object + */ + public function testHydrateNonObjectThrowsBadMethodCallException() + { + $this->setExpectedException(BadMethodCallException::class); + $this->hydrator->hydrate([], 'non-object'); + } } diff --git a/test/Filter/NumberOfParameterFilterTest.php b/test/Filter/NumberOfParameterFilterTest.php index 6cf5b50..7a1b345 100644 --- a/test/Filter/NumberOfParameterFilterTest.php +++ b/test/Filter/NumberOfParameterFilterTest.php @@ -10,6 +10,7 @@ namespace ZendTest\Hydrator\Filter; use Zend\Hydrator\Filter\NumberOfParameterFilter; +use Zend\Hydrator\Exception\InvalidArgumentException; /** * Unit tests for {@see NumberOfParameterFilter} @@ -38,6 +39,16 @@ public function testArityOne() $this->assertTrue($filter->filter(__CLASS__ . '::methodWithOptionalParameters')); } + /** + * Verifies an InvalidArgumentException is thrown for a method that doesn't exist + */ + public function testFilterPropertyDoesNotExist() + { + $this->setExpectedException(InvalidArgumentException::class); + $filter = new NumberOfParameterFilter(1); + $filter->filter(__CLASS__ . '::methodDoesNotExist'); + } + /** * Test asset method */ diff --git a/test/TestAsset/ClassMethodsTraversableOptions.php b/test/TestAsset/ClassMethodsTraversableOptions.php new file mode 100644 index 0000000..6e3e74d --- /dev/null +++ b/test/TestAsset/ClassMethodsTraversableOptions.php @@ -0,0 +1,46 @@ + false, + ]; + + public function current() + { + return current($this->options); + } + + public function next() + { + next($this->options); + } + + public function key() + { + return key($this->options); + } + + public function valid() + { + return isset($this->options[$this->key()]); + } + + public function rewind() + { + reset($this->options); + } +} From 2ffb6f0b697d283fdc6ca45b04fac1fcf2db8572 Mon Sep 17 00:00:00 2001 From: Daniel Messenger Date: Fri, 2 Oct 2015 11:13:17 +0100 Subject: [PATCH 2/5] Fixing formatting --- test/ArraySerializableTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/ArraySerializableTest.php b/test/ArraySerializableTest.php index 60af902..bb6004f 100644 --- a/test/ArraySerializableTest.php +++ b/test/ArraySerializableTest.php @@ -34,8 +34,6 @@ public function setUp() $this->hydrator = new ArraySerializable(); } - - /** * Verify that we get an exception when trying to extract on a non-object */ From 3e9637b0149ca9f74f17457662a5ea9ac5af6002 Mon Sep 17 00:00:00 2001 From: Dan Messenger Date: Fri, 2 Oct 2015 14:30:19 +0100 Subject: [PATCH 3/5] Fixed tests to include expected exception messages --- test/ArraySerializableTest.php | 11 +++++++++-- test/ClassMethodsTest.php | 15 ++++++++++++--- test/Filter/NumberOfParameterFilterTest.php | 5 ++++- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/test/ArraySerializableTest.php b/test/ArraySerializableTest.php index bb6004f..2bd7e6f 100644 --- a/test/ArraySerializableTest.php +++ b/test/ArraySerializableTest.php @@ -39,7 +39,10 @@ public function setUp() */ public function testHydratorExtractThrowsExceptionOnNonObjectParameter() { - $this->setExpectedException(BadMethodCallException::class); + $this->setExpectedException( + BadMethodCallException::class, + 'Zend\Hydrator\ArraySerializable::extract expects the provided object to implement getArrayCopy()' + ); $this->hydrator->extract('thisIsNotAnObject'); } @@ -48,7 +51,11 @@ public function testHydratorExtractThrowsExceptionOnNonObjectParameter() */ public function testHydratorHydrateThrowsExceptionOnNonObjectParameter() { - $this->setExpectedException(BadMethodCallException::class); + $this->setExpectedException( + BadMethodCallException::class, + 'Zend\Hydrator\ArraySerializable::hydrate expects the provided object to implement' + . ' exchangeArray() or populate()' + ); $this->hydrator->hydrate(['some' => 'data'], 'thisIsNotAnObject'); } diff --git a/test/ClassMethodsTest.php b/test/ClassMethodsTest.php index cdde62d..8372778 100644 --- a/test/ClassMethodsTest.php +++ b/test/ClassMethodsTest.php @@ -82,7 +82,10 @@ public function testCanHydratedPromiscuousInstances() */ public function testSetOptionsThrowsInvalidArgumentException() { - $this->setExpectedException(InvalidArgumentException::class); + $this->setExpectedException( + InvalidArgumentException::class, + 'The options parameter must be an array or a Traversable' + ); $this->hydrator->setOptions('invalid options'); } @@ -102,7 +105,10 @@ public function testSetOptionsFromTraversable() */ public function testExtractNonObjectThrowsBadMethodCallException() { - $this->setExpectedException(BadMethodCallException::class); + $this->setExpectedException( + BadMethodCallException::class, + 'Zend\Hydrator\ClassMethods::extract expects the provided $object to be a PHP object)' + ); $this->hydrator->extract('non-object'); } @@ -111,7 +117,10 @@ public function testExtractNonObjectThrowsBadMethodCallException() */ public function testHydrateNonObjectThrowsBadMethodCallException() { - $this->setExpectedException(BadMethodCallException::class); + $this->setExpectedException( + BadMethodCallException::class, + 'Zend\Hydrator\ClassMethods::hydrate expects the provided $object to be a PHP object)' + ); $this->hydrator->hydrate([], 'non-object'); } } diff --git a/test/Filter/NumberOfParameterFilterTest.php b/test/Filter/NumberOfParameterFilterTest.php index 7a1b345..4fa0df4 100644 --- a/test/Filter/NumberOfParameterFilterTest.php +++ b/test/Filter/NumberOfParameterFilterTest.php @@ -44,7 +44,10 @@ public function testArityOne() */ public function testFilterPropertyDoesNotExist() { - $this->setExpectedException(InvalidArgumentException::class); + $this->setExpectedException( + InvalidArgumentException::class, + 'Method ZendTest\Hydrator\Filter\NumberOfParameterFilterTest::methodDoesNotExist doesn\'t exist' + ); $filter = new NumberOfParameterFilter(1); $filter->filter(__CLASS__ . '::methodDoesNotExist'); } From e99ec7b3fa680a7a3b5bc2e8c750c8278aba8302 Mon Sep 17 00:00:00 2001 From: Dan Messenger Date: Fri, 2 Oct 2015 14:43:19 +0100 Subject: [PATCH 4/5] Fixing test to use ArrayObject --- test/ClassMethodsTest.php | 6 ++- .../ClassMethodsTraversableOptions.php | 46 ------------------- 2 files changed, 4 insertions(+), 48 deletions(-) delete mode 100644 test/TestAsset/ClassMethodsTraversableOptions.php diff --git a/test/ClassMethodsTest.php b/test/ClassMethodsTest.php index 8372778..0ff1c95 100644 --- a/test/ClassMethodsTest.php +++ b/test/ClassMethodsTest.php @@ -12,11 +12,11 @@ use Zend\Hydrator\ClassMethods; use Zend\Hydrator\Exception\BadMethodCallException; use Zend\Hydrator\Exception\InvalidArgumentException; +use Zend\Stdlib\ArrayObject; use ZendTest\Hydrator\TestAsset\ClassMethodsCamelCaseMissing; use ZendTest\Hydrator\TestAsset\ClassMethodsOptionalParameters; use ZendTest\Hydrator\TestAsset\ClassMethodsCamelCase; use ZendTest\Hydrator\TestAsset\ArraySerializable; -use ZendTest\Hydrator\TestAsset\ClassMethodsTraversableOptions; /** * Unit tests for {@see ClassMethods} @@ -94,7 +94,9 @@ public function testSetOptionsThrowsInvalidArgumentException() */ public function testSetOptionsFromTraversable() { - $options = new ClassMethodsTraversableOptions(); + $options = new ArrayObject([ + 'underscoreSeparatedKeys' => false, + ]); $this->hydrator->setOptions($options); $this->assertSame(false, $this->hydrator->getUnderscoreSeparatedKeys()); diff --git a/test/TestAsset/ClassMethodsTraversableOptions.php b/test/TestAsset/ClassMethodsTraversableOptions.php deleted file mode 100644 index 6e3e74d..0000000 --- a/test/TestAsset/ClassMethodsTraversableOptions.php +++ /dev/null @@ -1,46 +0,0 @@ - false, - ]; - - public function current() - { - return current($this->options); - } - - public function next() - { - next($this->options); - } - - public function key() - { - return key($this->options); - } - - public function valid() - { - return isset($this->options[$this->key()]); - } - - public function rewind() - { - reset($this->options); - } -} From 5487e3ba1b759ca1c75ea6e8ccca008b79631ec0 Mon Sep 17 00:00:00 2001 From: Dan Messenger Date: Fri, 2 Oct 2015 15:13:33 +0100 Subject: [PATCH 5/5] Fixing use of ArrayObject to the native PHP implementation --- test/ClassMethodsTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/ClassMethodsTest.php b/test/ClassMethodsTest.php index 0ff1c95..001dd3f 100644 --- a/test/ClassMethodsTest.php +++ b/test/ClassMethodsTest.php @@ -12,7 +12,6 @@ use Zend\Hydrator\ClassMethods; use Zend\Hydrator\Exception\BadMethodCallException; use Zend\Hydrator\Exception\InvalidArgumentException; -use Zend\Stdlib\ArrayObject; use ZendTest\Hydrator\TestAsset\ClassMethodsCamelCaseMissing; use ZendTest\Hydrator\TestAsset\ClassMethodsOptionalParameters; use ZendTest\Hydrator\TestAsset\ClassMethodsCamelCase; @@ -94,7 +93,7 @@ public function testSetOptionsThrowsInvalidArgumentException() */ public function testSetOptionsFromTraversable() { - $options = new ArrayObject([ + $options = new \ArrayObject([ 'underscoreSeparatedKeys' => false, ]); $this->hydrator->setOptions($options);