Skip to content

Commit

Permalink
Adding integration tests for the aggregate hydrator
Browse files Browse the repository at this point in the history
  • Loading branch information
Ocramius committed May 9, 2013
1 parent 2ba5739 commit 2b4ce97
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 14 deletions.
4 changes: 2 additions & 2 deletions library/Zend/Stdlib/Hydrator/Aggregate/AggregateHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function extract($object)
{
$event = new ExtractEvent($this, $object);

$this->eventManager->trigger($event);
$this->getEventManager()->trigger($event);

return $event->getExtractedData();
}
Expand All @@ -49,7 +49,7 @@ public function hydrate(array $data, $object)
{
$event = new HydrateEvent($this, $object, $data);

$this->eventManager->trigger($event);
$this->getEventManager()->trigger($event);

return $event->getHydratedObject();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
<?php
/**
* Created by JetBrains PhpStorm.
* User: ocramius
* Date: 5/9/13
* Time: 4:29 PM
* To change this template use File | Settings | File Templates.
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Stdlib\Hydrator\Aggregate;


use ArrayObject;
use PHPUnit_Framework_TestCase;
use Zend\Stdlib\Hydrator\Aggregate\AggregateHydrator;
use Zend\Stdlib\Hydrator\Aggregate\ExtractEvent;
use Zend\Stdlib\Hydrator\Aggregate\HydrateEvent;
use Zend\Stdlib\Hydrator\ArraySerializable;
use Zend\Stdlib\Hydrator\ClassMethods;
use Zend\Stdlib\Hydrator\HydratorInterface;
use ZendTest\Stdlib\TestAsset\AggregateObject;
use stdClass;

/**
* Integration tests {@see \Zend\Stdlib\Hydrator\Aggregate\AggregateHydrator}
*/
class AggregateHydratorFunctionalTest extends PHPUnit_Framework_TestCase
{
/**
Expand All @@ -28,23 +39,140 @@ public function setUp()
$this->hydrator = new AggregateHydrator();
}

/**
* Verifies that no interaction happens when the aggregate hydrator is empty
*/
public function testEmptyAggregate()
{
$this->markTestIncomplete();
$object = new ArrayObject(array('zaphod' => 'beeblebrox'));

$this->assertSame(array(), $this->hydrator->extract($object));
$this->assertSame($object, $this->hydrator->hydrate(array('arthur' => 'dent'), $object));

$this->assertSame(array('zaphod' => 'beeblebrox'), $object->getArrayCopy());
}

/**
* @dataProvider getHydratorSet
*
* Verifies that using a single hydrator will have the aggregate hydrator behave like that single hydrator
*/
public function testSingleHydratorExtraction(HydratorInterface $comparisonHydrator, $object)
{
$blueprint = clone $object;

$this->hydrator->add($comparisonHydrator);

$this->assertSame($comparisonHydrator->extract($blueprint), $this->hydrator->extract($object));
}

/**
* @dataProvider getHydratorSet
*
* Verifies that using a single hydrator will have the aggregate hydrator behave like that single hydrator
*/
public function testSingleHydratorHydration(HydratorInterface $comparisonHydrator, $object, $data)
{
$blueprint = clone $object;

$this->hydrator->add($comparisonHydrator);

$hydratedBlueprint = $comparisonHydrator->hydrate($data, $blueprint);
$hydrated = $this->hydrator->hydrate($data, $object);

$this->assertEquals($hydratedBlueprint, $hydrated);

if ($hydratedBlueprint === $blueprint) {
$this->assertSame($hydrated, $object);
}
}

/**
* Verifies that multiple hydrators in an aggregate merge the extracted data
*/
public function testExtractWithMultipleHydrators()
{
$this->hydrator->add(new ClassMethods());
$this->hydrator->add(new ArraySerializable());

$object = new AggregateObject();

$extracted = $this->hydrator->extract($object);

$this->assertArrayHasKey('maintainer', $extracted);
$this->assertArrayHasKey('president', $extracted);
$this->assertSame('Marvin', $extracted['maintainer']);
$this->assertSame('Zaphod', $extracted['president']);
}

/**
* Verifies that multiple hydrators in an aggregate merge the extracted data
*/
public function testHydrateWithMultipleHydrators()
{
$this->hydrator->add(new ClassMethods());
$this->hydrator->add(new ArraySerializable());

$object = new AggregateObject();

$this->assertSame(
$object,
$this->hydrator->hydrate(array('maintainer' => 'Trillian', 'president' => '???'), $object)
);

$this->assertArrayHasKey('maintainer', $object->arrayData);
$this->assertArrayHasKey('president', $object->arrayData);
$this->assertSame('Trillian', $object->arrayData['maintainer']);
$this->assertSame('???', $object->arrayData['president']);
$this->assertSame('Trillian', $object->maintainer);
}

public function testSingleHydrator()
/**
* Verifies that stopping propagation within a listener in the hydrator allows modifying how the
* hydrator behaves
*/
public function testStoppedPropagationInExtraction()
{
$this->markTestIncomplete();
$object = new ArrayObject(array('president' => 'Zaphod'));
$callback = function (ExtractEvent $event) {
$event->setExtractedData(array('Ravenous Bugblatter Beast of Traal'));
$event->stopPropagation();
};

$this->hydrator->add(new ArraySerializable());
$this->hydrator->getEventManager()->attach(ExtractEvent::EVENT_EXTRACT, $callback, 1000);

$this->assertSame(array('Ravenous Bugblatter Beast of Traal'), $this->hydrator->extract($object));
}

public function testMultipleHydrators()
/**
* Verifies that stopping propagation within a listener in the hydrator allows modifying how the
* hydrator behaves
*/
public function testStoppedPropagationInHydration()
{
$this->markTestIncomplete();
$object = new ArrayObject();
$swappedObject = new stdClass();
$callback = function (HydrateEvent $event) use ($swappedObject) {
$event->setHydratedObject($swappedObject);
$event->stopPropagation();
};

$this->hydrator->add(new ArraySerializable());
$this->hydrator->getEventManager()->attach(HydrateEvent::EVENT_HYDRATE, $callback, 1000);

$this->assertSame($swappedObject, $this->hydrator->hydrate(array('president' => 'Zaphod'), $object));
}

public function testStoppedPropagation()
/**
* Data provider method
*
* @return array
*/
public function getHydratorSet()
{
$this->markTestIncomplete();
return array(
array(new ArraySerializable(), new ArrayObject(array('zaphod' => 'beeblebrox')), array('arthur' => 'dent')),
);
}
}
60 changes: 60 additions & 0 deletions tests/ZendTest/Stdlib/TestAsset/AggregateObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Stdlib
*/

namespace ZendTest\Stdlib\TestAsset;

/**
* Test asset to verify that a composition of a class-methods and an array-serializable
* hydrator produces the expected output
*/
class AggregateObject
{
/**
* @var array
*/
public $arrayData = array('president' => 'Zaphod');

/**
* @var string
*/
public $maintainer = 'Marvin';

/**
* @return string
*/
public function getMaintainer()
{
return $this->maintainer;
}

/**
* @param string $maintainer
*/
public function setMaintainer($maintainer)
{
$this->maintainer = $maintainer;
}

/**
* @return array
*/
public function getArrayCopy()
{
return $this->arrayData;
}

/**
* @param array $data
*/
public function exchangeArray(array $data)
{
$this->arrayData = $data;
}
}

0 comments on commit 2b4ce97

Please sign in to comment.