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

Commit c634289

Browse files
committed
Merge branch 'hotfix/232'
Close #232
2 parents ff4c9ec + 5a6df28 commit c634289

File tree

4 files changed

+97
-13
lines changed

4 files changed

+97
-13
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ All notable changes to this project will be documented in this file, in reverse
2222

2323
### Fixed
2424

25-
- Nothing.
25+
- [#232](https://github.com/zendframework/zend-form/pull/232) fixes validating `$creationOption`
26+
of `Zend\Form\ElementFactory`. Allowed values are: array, Traversable or null.
27+
If invalid value provided exception will be thrown.
2628

2729
## 2.14.1 - 2019-02-26
2830

src/ElementFactory.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ public function __construct($creationOptions = null)
3838
return;
3939
}
4040

41+
if ($creationOptions instanceof Traversable) {
42+
$creationOptions = iterator_to_array($creationOptions);
43+
}
44+
45+
if (! is_array($creationOptions)) {
46+
throw new InvalidServiceException(sprintf(
47+
'%s cannot use non-array, non-traversable, non-null creation options; received %s',
48+
__CLASS__,
49+
is_object($creationOptions) ? get_class($creationOptions) : gettype($creationOptions)
50+
));
51+
}
52+
4153
$this->setCreationOptions($creationOptions);
4254
}
4355

@@ -118,18 +130,6 @@ public function createService(ServiceLocatorInterface $serviceLocator, $canonica
118130
*/
119131
public function setCreationOptions(array $creationOptions)
120132
{
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-
133133
$this->creationOptions = $creationOptions;
134134
}
135135
}

test/ElementFactoryTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* @see https://github.com/zendframework/zend-form for the canonical source repository
4+
* @copyright Copyright (c) 2019 Zend Technologies USA Inc. (https://www.zend.com)
5+
* @license https://github.com/zendframework/zend-form/blob/master/LICENSE.md New BSD License
6+
*/
7+
8+
namespace ZendTest\Form;
9+
10+
use ArrayObject;
11+
use Interop\Container\ContainerInterface;
12+
use PHPUnit\Framework\TestCase;
13+
use Zend\Form\ElementFactory;
14+
use Zend\ServiceManager\Exception\InvalidServiceException;
15+
use Zend\ServiceManager\ServiceLocatorInterface;
16+
use ZendTest\Form\TestAsset\ArgumentRecorder;
17+
18+
class ElementFactoryTest extends TestCase
19+
{
20+
public function validCreationOptions()
21+
{
22+
yield 'ArrayObject' => [new ArrayObject(['key' => 'value']), ['key' => 'value']];
23+
yield 'array' => [['key' => 'value'], ['key' => 'value']];
24+
yield 'empty-array' => [[], []];
25+
yield 'null' => [null, []];
26+
}
27+
28+
/**
29+
* @dataProvider validCreationOptions
30+
*
31+
* @param mixed $creationOptions
32+
* @param array $expectedValue
33+
*/
34+
public function testValidCreationOptions($creationOptions, array $expectedValue)
35+
{
36+
$container = $this->prophesize(ServiceLocatorInterface::class)
37+
->willImplement(ContainerInterface::class)
38+
->reveal();
39+
40+
$factory = new ElementFactory($creationOptions);
41+
$result = $factory->createService($container, ArgumentRecorder::class);
42+
$this->assertInstanceOf(ArgumentRecorder::class, $result);
43+
$this->assertSame(['argumentrecorder', $expectedValue], $result->args);
44+
}
45+
46+
public function invalidCreationOptions()
47+
{
48+
yield 'object' => [(object) ['key' => 'val']];
49+
yield 'int' => [PHP_INT_MAX];
50+
yield 'string' => [uniqid('', true)];
51+
}
52+
53+
/**
54+
* @dataProvider invalidCreationOptions
55+
*
56+
* @param $creationOptions
57+
*/
58+
public function testInvalidCreationOptionsException($creationOptions)
59+
{
60+
$this->expectException(InvalidServiceException::class);
61+
$this->expectExceptionMessage('cannot use non-array, non-traversable, non-null creation options;');
62+
new ElementFactory($creationOptions);
63+
}
64+
}

test/TestAsset/ArgumentRecorder.php

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

0 commit comments

Comments
 (0)