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

Commit ae5d326

Browse files
committed
Fix validation of $creationOptions in ElementFactory
In setCreationOptions(array $options), a TypeError would be thrown if it was anything other than an array. Move the exceptions and traversable checks to the constructor.
1 parent ff4c9ec commit ae5d326

File tree

3 files changed

+60
-12
lines changed

3 files changed

+60
-12
lines changed

src/ElementFactory.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ public function __construct($creationOptions = null)
3737
if (null === $creationOptions) {
3838
return;
3939
}
40+
if ($creationOptions instanceof Traversable) {
41+
$creationOptions = iterator_to_array($creationOptions);
42+
}
43+
44+
if (! is_array($creationOptions)) {
45+
throw new InvalidServiceException(sprintf(
46+
'%s cannot use non-array, non-traversable, non-null creation options; received %s',
47+
__CLASS__,
48+
(is_object($creationOptions) ? get_class($creationOptions) : gettype($creationOptions))
49+
));
50+
}
4051

4152
$this->setCreationOptions($creationOptions);
4253
}
@@ -118,18 +129,6 @@ public function createService(ServiceLocatorInterface $serviceLocator, $canonica
118129
*/
119130
public function setCreationOptions(array $creationOptions)
120131
{
121-
if ($creationOptions instanceof Traversable) {
122-
$creationOptions = iterator_to_array($creationOptions);
123-
}
124-
125-
if (! is_array($creationOptions)) {
126-
throw new InvalidServiceException(sprintf(
127-
'%s cannot use non-array, non-traversable creation options; received %s',
128-
__CLASS__,
129-
(is_object($creationOptions) ? get_class($creationOptions) : gettype($creationOptions))
130-
));
131-
}
132-
133132
$this->creationOptions = $creationOptions;
134133
}
135134
}

test/ElementFactoryTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zf2 for the canonical source repository
6+
* @copyright Copyright (c) 2005-2019 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
*/
9+
10+
namespace ZendTest\Form;
11+
12+
use ArrayObject;
13+
use PHPUnit\Framework\TestCase;
14+
use Zend\Form\ElementFactory;
15+
use Zend\ServiceManager\ServiceLocatorInterface;
16+
use ZendTest\Form\TestAsset\ArgumentRecorder;
17+
18+
/**
19+
* @group Zend_Form
20+
*/
21+
class FormElementManagerTest extends TestCase
22+
{
23+
public function testInjectToFormFactoryAware()
24+
{
25+
$serviceLocator = $this->prophesize(ServiceLocatorInterface::class)->reveal();
26+
27+
$factory = new ElementFactory(new ArrayObject(['key' => 'value']));
28+
$result = $factory->createService($serviceLocator, ArgumentRecorder::class);
29+
$this->assertInstanceOf(ArgumentRecorder::class, $result);
30+
$this->assertSame(['argumentrecorder', ['key' => 'value']], $result->args);
31+
}
32+
}

test/TestAsset/ArgumentRecorder.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zf2 for the canonical source repository
6+
* @copyright Copyright (c) 2005-2019 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
*/
9+
10+
namespace ZendTest\Form\TestAsset;
11+
12+
class ArgumentRecorder {
13+
public $args;
14+
public function __construct(...$args) {
15+
$this->args = $args;
16+
}
17+
}

0 commit comments

Comments
 (0)