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

Commit

Permalink
Merge branch 'hotfix/19' into release-2.7
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Feb 19, 2016
2 parents b894a85 + 27aaaf5 commit 4499760
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 49 deletions.
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## 2.7.6 - 2016-02-19

### Added

- Nothing.

### Deprecated

- Nothing.

### Removed

- Nothing.

### Fixed

- [#56](https://github.com/zendframework/zend-stdlib/pull/56) updates the
`AggregateHydrator` implementation to add overrides for the `hydrate()` and
`extract()` methods; this was done to ensure they trigger zend-stdlib-specific
event classes (vs the zend-hydrator base classes), in order to ensure
backwards compatibility.

## 2.7.5 - 2016-02-16

### Added
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"require": {
"php": "^5.5 || ^7.0",
"zendframework/zend-hydrator": "~1.0"
"zendframework/zend-hydrator": "~1.1"
},
"require-dev": {
"zendframework/zend-config": "~2.5",
Expand Down
23 changes: 23 additions & 0 deletions src/Hydrator/Aggregate/AggregateHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,27 @@
*/
class AggregateHydrator extends BaseAggregateHydrator implements HydratorInterface
{
/**
* {@inheritDoc}
*/
public function extract($object)
{
$event = new ExtractEvent($this, $object);

$this->getEventManager()->triggerEvent($event);

return $event->getExtractedData();
}

/**
* {@inheritDoc}
*/
public function hydrate(array $data, $object)
{
$event = new HydrateEvent($this, $object, $data);

$this->getEventManager()->triggerEvent($event);

return $event->getHydratedObject();
}
}
97 changes: 85 additions & 12 deletions test/Hydrator/Aggregate/AggregateHydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@
namespace ZendTest\Stdlib\Hydrator\Aggregate;

use PHPUnit_Framework_TestCase;
use Prophecy\Argument;
use Zend\EventManager\EventManager;
use Zend\Hydrator\Aggregate\HydratorListener;
use Zend\Hydrator\HydratorInterface;
use Zend\Stdlib\Hydrator\Aggregate\AggregateHydrator;
use Zend\Stdlib\Hydrator\Aggregate\ExtractEvent;
use Zend\Stdlib\Hydrator\Aggregate\HydrateEvent;
use stdClass;

/**
Expand All @@ -24,7 +30,7 @@ class AggregateHydratorTest extends PHPUnit_Framework_TestCase
protected $hydrator;

/**
* @var \Zend\EventManager\EventManagerInterface|\PHPUnit_Framework_MockObject_MockObject
* @var \Zend\EventManager\EventManager|\PHPUnit_Framework_MockObject_MockObject
*/
protected $eventManager;

Expand All @@ -33,7 +39,7 @@ class AggregateHydratorTest extends PHPUnit_Framework_TestCase
*/
public function setUp()
{
$this->eventManager = $this->getMock('Zend\EventManager\EventManagerInterface');
$this->eventManager = $this->getMock(EventManager::class);
$this->hydrator = new AggregateHydrator();

$this->hydrator->setEventManager($this->eventManager);
Expand All @@ -44,45 +50,112 @@ public function setUp()
*/
public function testAdd()
{
$attached = $this->getMock('Zend\Hydrator\HydratorInterface');
$hydrator = $this->prophesize(HydratorInterface::class)->reveal();

$events = $this->prophesize(EventManager::class);

$events->setIdentifiers(Argument::type('array'))->shouldBeCalled();

$events->attach(
HydrateEvent::EVENT_HYDRATE,
Argument::that(function ($argument) {
if (! is_callable($argument)) {
return false;
}
if (! is_array($argument)) {
return false;
}
return (
$argument[0] instanceof HydratorListener
&& $argument[1] === 'onHydrate'
);
}),
123
)->shouldBeCalled();

$events->attach(
ExtractEvent::EVENT_EXTRACT,
Argument::that(function ($argument) {
if (! is_callable($argument)) {
return false;
}
if (! is_array($argument)) {
return false;
}
return (
$argument[0] instanceof HydratorListener
&& $argument[1] === 'onExtract'
);
}),
123
)->shouldBeCalled();

$this->hydrator->setEventManager($events->reveal());
$this->hydrator->add($hydrator, 123);
}

/**
* @covers \Zend\Stdlib\Hydrator\Aggregate\AggregateHydrator::hydrate
*/
public function testHydrate()
{
$object = new stdClass();

$this
->eventManager
->expects($this->once())
->method('attachAggregate')
->with($this->isInstanceOf('Zend\Hydrator\Aggregate\HydratorListener'), 123);
->method('triggerEvent')
->with($this->isInstanceOf('Zend\Hydrator\Aggregate\HydrateEvent'));

$this->hydrator->add($attached, 123);
$this->assertSame($object, $this->hydrator->hydrate(['foo' => 'bar'], $object));
}

/**
* @covers \Zend\Stdlib\Hydrator\Aggregate\AggregateHydrator::extract
*/
public function testExtract()
{
$object = new stdClass();

$this
->eventManager
->expects($this->once())
->method('triggerEvent')
->with($this->isInstanceOf('Zend\Hydrator\Aggregate\ExtractEvent'));

$this->assertSame([], $this->hydrator->extract($object));
}

/**
* @group 55
* @covers \Zend\Stdlib\Hydrator\Aggregate\AggregateHydrator::hydrate
*/
public function testHydrate()
public function testHydrateUsesStdlibHydrateEvent()
{
$object = new stdClass();

$this
->eventManager
->expects($this->once())
->method('trigger')
->with($this->isInstanceOf('Zend\Hydrator\Aggregate\HydrateEvent'));
->method('triggerEvent')
->with($this->isInstanceOf('Zend\Stdlib\Hydrator\Aggregate\HydrateEvent'));

$this->assertSame($object, $this->hydrator->hydrate(['foo' => 'bar'], $object));
}

/**
* @group 55
* @covers \Zend\Stdlib\Hydrator\Aggregate\AggregateHydrator::extract
*/
public function testExtract()
public function testExtractUsesStdlibExtractEvent()
{
$object = new stdClass();

$this
->eventManager
->expects($this->once())
->method('trigger')
->with($this->isInstanceOf('Zend\Hydrator\Aggregate\ExtractEvent'));
->method('triggerEvent')
->with($this->isInstanceOf('Zend\Stdlib\Hydrator\Aggregate\ExtractEvent'));

$this->assertSame([], $this->hydrator->extract($object));
}
Expand Down
8 changes: 6 additions & 2 deletions test/Hydrator/DelegatingHydratorFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,21 @@

namespace ZendTest\Stdlib\Hydrator;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Stdlib\Hydrator\DelegatingHydratorFactory;

class DelegatingHydratorFactoryTest extends \PHPUnit_Framework_TestCase
{
public function testFactory()
{
$hydratorManager = $this->getMock('Zend\ServiceManager\ServiceLocatorInterface');
$hydratorManager = $this->prophesize(ServiceLocatorInterface::class);
$hydratorManager->willImplement(ContainerInterface::class);

$factory = new DelegatingHydratorFactory();
$this->assertInstanceOf(
'Zend\Hydrator\DelegatingHydrator',
$factory->createService($hydratorManager)
$factory->createService($hydratorManager->reveal())
);
}
}
51 changes: 17 additions & 34 deletions test/Hydrator/DelegatingHydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@

namespace ZendTest\Stdlib\Hydrator;

use Zend\Stdlib\Hydrator\DelegatingHydrator;
use ArrayObject;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Stdlib\Hydrator\DelegatingHydrator;
use Zend\Stdlib\Hydrator\HydratorInterface;

/**
* Unit tests for {@see \Zend\Stdlib\Hydrator\DelegatingHydrator}
Expand Down Expand Up @@ -39,51 +42,31 @@ class DelegatingHydratorTest extends \PHPUnit_Framework_TestCase
*/
public function setUp()
{
$this->hydrators = $this->getMock('Zend\ServiceManager\ServiceLocatorInterface');
$this->hydrator = new DelegatingHydrator($this->hydrators);
$this->hydrators = $this->prophesize(ServiceLocatorInterface::class);
$this->hydrators->willImplement(ContainerInterface::class);
$this->hydrator = new DelegatingHydrator($this->hydrators->reveal());
$this->object = new ArrayObject;
}

public function testExtract()
{
$this->hydrators->expects($this->any())
->method('has')
->with('ArrayObject')
->will($this->returnValue(true));

$hydrator = $this->getMock('Zend\Stdlib\Hydrator\HydratorInterface');
$hydrator = $this->prophesize(HydratorInterface::class);
$hydrator->extract($this->object)->willReturn(['foo' => 'bar']);

$this->hydrators->expects($this->any())
->method('get')
->with('ArrayObject')
->will($this->returnValue($hydrator));
$this->hydrators->has(ArrayObject::class)->willReturn(true);
$this->hydrators->get(ArrayObject::class)->willReturn($hydrator->reveal());

$hydrator->expects($this->any())
->method('extract')
->with($this->object)
->will($this->returnValue(['foo' => 'bar']));

$this->assertEquals(['foo' => 'bar'], $hydrator->extract($this->object));
$this->assertEquals(['foo' => 'bar'], $this->hydrator->extract($this->object));
}

public function testHydrate()
{
$this->hydrators->expects($this->any())
->method('has')
->with('ArrayObject')
->will($this->returnValue(true));

$hydrator = $this->getMock('Zend\Stdlib\Hydrator\HydratorInterface');
$hydrator = $this->prophesize(HydratorInterface::class);
$hydrator->hydrate(['foo' => 'bar'], $this->object)->willReturn($this->object);

$this->hydrators->expects($this->any())
->method('get')
->with('ArrayObject')
->will($this->returnValue($hydrator));
$this->hydrators->has(ArrayObject::class)->willReturn(true);
$this->hydrators->get(ArrayObject::class)->willReturn($hydrator->reveal());

$hydrator->expects($this->any())
->method('hydrate')
->with(['foo' => 'bar'], $this->object)
->will($this->returnValue($this->object));
$this->assertEquals($this->object, $hydrator->hydrate(['foo' => 'bar'], $this->object));
$this->assertEquals($this->object, $this->hydrator->hydrate(['foo' => 'bar'], $this->object));
}
}

0 comments on commit 4499760

Please sign in to comment.