Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge 5487e3b into 16bd152
Browse files Browse the repository at this point in the history
  • Loading branch information
dannym87 committed Oct 2, 2015
2 parents 16bd152 + 5487e3b commit 7852139
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 0 deletions.
93 changes: 93 additions & 0 deletions test/ArraySerializableTest.php
@@ -0,0 +1,93 @@
<?php

/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Hydrator;

use Zend\Hydrator\Exception\BadMethodCallException;
use Zend\Hydrator\ArraySerializable;
use ZendTest\Hydrator\TestAsset\ArraySerializable as ArraySerializableAsset;

/**
* Unit tests for {@see ArraySerializable}
*
* @covers \Zend\Hydrator\ArraySerializable
*/
class ArraySerializableTest extends \PHPUnit_Framework_TestCase
{
/**
* @var ArraySerializable
*/
protected $hydrator;

/**
* {@inheritDoc}
*/
public function setUp()
{
$this->hydrator = new ArraySerializable();
}

/**
* Verify that we get an exception when trying to extract on a non-object
*/
public function testHydratorExtractThrowsExceptionOnNonObjectParameter()
{
$this->setExpectedException(
BadMethodCallException::class,
'Zend\Hydrator\ArraySerializable::extract expects the provided object to implement getArrayCopy()'
);
$this->hydrator->extract('thisIsNotAnObject');
}

/**
* Verify that we get an exception when trying to hydrate a non-object
*/
public function testHydratorHydrateThrowsExceptionOnNonObjectParameter()
{
$this->setExpectedException(
BadMethodCallException::class,
'Zend\Hydrator\ArraySerializable::hydrate expects the provided object to implement'
. ' exchangeArray() or populate()'
);
$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());
}
}
51 changes: 51 additions & 0 deletions test/ClassMethodsTest.php
Expand Up @@ -10,6 +10,8 @@
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;
Expand Down Expand Up @@ -73,4 +75,53 @@ public function testCanHydratedPromiscuousInstances()
$arraySerializable->getArrayCopy()
);
}

/**
* Verifies the options must be an array or Traversable
*/
public function testSetOptionsThrowsInvalidArgumentException()
{
$this->setExpectedException(
InvalidArgumentException::class,
'The options parameter must be an array or a Traversable'
);
$this->hydrator->setOptions('invalid options');
}

/**
* Verifies options can be set from a Traversable object
*/
public function testSetOptionsFromTraversable()
{
$options = new \ArrayObject([
'underscoreSeparatedKeys' => false,
]);
$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,
'Zend\Hydrator\ClassMethods::extract expects the provided $object to be a PHP object)'
);
$this->hydrator->extract('non-object');
}

/**
* Verifies a BadMethodCallException is thrown for hydrating a non-object
*/
public function testHydrateNonObjectThrowsBadMethodCallException()
{
$this->setExpectedException(
BadMethodCallException::class,
'Zend\Hydrator\ClassMethods::hydrate expects the provided $object to be a PHP object)'
);
$this->hydrator->hydrate([], 'non-object');
}
}
14 changes: 14 additions & 0 deletions test/Filter/NumberOfParameterFilterTest.php
Expand Up @@ -10,6 +10,7 @@
namespace ZendTest\Hydrator\Filter;

use Zend\Hydrator\Filter\NumberOfParameterFilter;
use Zend\Hydrator\Exception\InvalidArgumentException;

/**
* Unit tests for {@see NumberOfParameterFilter}
Expand Down Expand Up @@ -38,6 +39,19 @@ 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,
'Method ZendTest\Hydrator\Filter\NumberOfParameterFilterTest::methodDoesNotExist doesn\'t exist'
);
$filter = new NumberOfParameterFilter(1);
$filter->filter(__CLASS__ . '::methodDoesNotExist');
}

/**
* Test asset method
*/
Expand Down

0 comments on commit 7852139

Please sign in to comment.