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

Commit e16dc7a

Browse files
committed
Fixed DelegatingHydrator tests
When updating zend-hydrator to 1.1, we discovered some testing issues with the underlying functionality; additionally, due to the fact that the `createService()` method of the factory delegates to the `__invoke()` method, and the typehints are different (former uses ServiceLocatorInterface, while latter uses ContainerInterface), and the fact that the DelegatingHydrator constructor now accepts a ContainerInterface (vs ServiceLocatorInterface), prophecy had to be used to allow composing multiple interfaces in the mock object.
1 parent e08cf6a commit e16dc7a

File tree

2 files changed

+24
-36
lines changed

2 files changed

+24
-36
lines changed

test/Hydrator/DelegatingHydratorFactoryTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,21 @@
99

1010
namespace ZendTest\Stdlib\Hydrator;
1111

12+
use Interop\Container\ContainerInterface;
13+
use Zend\ServiceManager\ServiceLocatorInterface;
1214
use Zend\Stdlib\Hydrator\DelegatingHydratorFactory;
1315

1416
class DelegatingHydratorFactoryTest extends \PHPUnit_Framework_TestCase
1517
{
1618
public function testFactory()
1719
{
18-
$hydratorManager = $this->getMock('Zend\ServiceManager\ServiceLocatorInterface');
20+
$hydratorManager = $this->prophesize(ServiceLocatorInterface::class);
21+
$hydratorManager->willImplement(ContainerInterface::class);
22+
1923
$factory = new DelegatingHydratorFactory();
2024
$this->assertInstanceOf(
2125
'Zend\Hydrator\DelegatingHydrator',
22-
$factory->createService($hydratorManager)
26+
$factory->createService($hydratorManager->reveal())
2327
);
2428
}
2529
}

test/Hydrator/DelegatingHydratorTest.php

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@
99

1010
namespace ZendTest\Stdlib\Hydrator;
1111

12-
use Zend\Stdlib\Hydrator\DelegatingHydrator;
1312
use ArrayObject;
13+
use Interop\Container\ContainerInterface;
14+
use Prophecy\Argument;
15+
use Zend\ServiceManager\ServiceLocatorInterface;
16+
use Zend\Stdlib\Hydrator\DelegatingHydrator;
17+
use Zend\Stdlib\Hydrator\HydratorInterface;
1418

1519
/**
1620
* Unit tests for {@see \Zend\Stdlib\Hydrator\DelegatingHydrator}
@@ -39,51 +43,31 @@ class DelegatingHydratorTest extends \PHPUnit_Framework_TestCase
3943
*/
4044
public function setUp()
4145
{
42-
$this->hydrators = $this->getMock('Zend\ServiceManager\ServiceLocatorInterface');
43-
$this->hydrator = new DelegatingHydrator($this->hydrators);
46+
$this->hydrators = $this->prophesize(ServiceLocatorInterface::class);
47+
$this->hydrators->willImplement(ContainerInterface::class);
48+
$this->hydrator = new DelegatingHydrator($this->hydrators->reveal());
4449
$this->object = new ArrayObject;
4550
}
4651

4752
public function testExtract()
4853
{
49-
$this->hydrators->expects($this->any())
50-
->method('has')
51-
->with('ArrayObject')
52-
->will($this->returnValue(true));
53-
54-
$hydrator = $this->getMock('Zend\Stdlib\Hydrator\HydratorInterface');
54+
$hydrator = $this->prophesize(HydratorInterface::class);
55+
$hydrator->extract($this->object)->willReturn(['foo' => 'bar']);
5556

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

61-
$hydrator->expects($this->any())
62-
->method('extract')
63-
->with($this->object)
64-
->will($this->returnValue(['foo' => 'bar']));
65-
66-
$this->assertEquals(['foo' => 'bar'], $hydrator->extract($this->object));
60+
$this->assertEquals(['foo' => 'bar'], $this->hydrator->extract($this->object));
6761
}
6862

6963
public function testHydrate()
7064
{
71-
$this->hydrators->expects($this->any())
72-
->method('has')
73-
->with('ArrayObject')
74-
->will($this->returnValue(true));
75-
76-
$hydrator = $this->getMock('Zend\Stdlib\Hydrator\HydratorInterface');
65+
$hydrator = $this->prophesize(HydratorInterface::class);
66+
$hydrator->hydrate(['foo' => 'bar'], $this->object)->willReturn($this->object);
7767

78-
$this->hydrators->expects($this->any())
79-
->method('get')
80-
->with('ArrayObject')
81-
->will($this->returnValue($hydrator));
68+
$this->hydrators->has(ArrayObject::class)->willReturn(true);
69+
$this->hydrators->get(ArrayObject::class)->willReturn($hydrator->reveal());
8270

83-
$hydrator->expects($this->any())
84-
->method('hydrate')
85-
->with(['foo' => 'bar'], $this->object)
86-
->will($this->returnValue($this->object));
87-
$this->assertEquals($this->object, $hydrator->hydrate(['foo' => 'bar'], $this->object));
71+
$this->assertEquals($this->object, $this->hydrator->hydrate(['foo' => 'bar'], $this->object));
8872
}
8973
}

0 commit comments

Comments
 (0)