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

Commit d7f31e1

Browse files
committed
Added ValidatorPluginManagerFactory for creating and returning a ValidatorPluginManager instance
Imports it from zend-mvc, and adds tests to validate usage in both v2 and v3 releases of zend-servicemanager.
1 parent 07a66da commit d7f31e1

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

src/ValidatorPluginManagerFactory.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* @link http://github.com/zendframework/zend-validator for the canonical source repository
4+
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
5+
* @license http://framework.zend.com/license/new-bsd New BSD License
6+
*/
7+
8+
namespace Zend\Validator;
9+
10+
use Interop\Container\ContainerInterface;
11+
use Zend\ServiceManager\FactoryInterface;
12+
use Zend\ServiceManager\ServiceLocatorInterface;
13+
14+
class ValidatorPluginManagerFactory implements FactoryInterface
15+
{
16+
/**
17+
* zend-servicemanager v2 support for invocation options.
18+
*
19+
* @param array
20+
*/
21+
protected $creationOptions;
22+
23+
/**
24+
* {@inheritDoc}
25+
*
26+
* @return ValidatorPluginManager
27+
*/
28+
public function __invoke(ContainerInterface $container, $name, array $options = null)
29+
{
30+
return new ValidatorPluginManager($container, $options ?: []);
31+
}
32+
33+
/**
34+
* {@inheritDoc}
35+
*
36+
* @return ValidatorPluginManager
37+
*/
38+
public function createService(ServiceLocatorInterface $container, $name = null, $requestedName = null)
39+
{
40+
return $this($container, $requestedName ?: ValidatorPluginManager::class, $this->creationOptions);
41+
}
42+
43+
/**
44+
* zend-servicemanager v2 support for invocation options.
45+
*
46+
* @param array $options
47+
* @return void
48+
*/
49+
public function setCreationOptions(array $options)
50+
{
51+
$this->creationOptions = $options;
52+
}
53+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
/**
3+
* @link http://github.com/zendframework/zend-validator for the canonical source repository
4+
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
5+
* @license http://framework.zend.com/license/new-bsd New BSD License
6+
*/
7+
8+
namespace ZendTest\Validator;
9+
10+
use Interop\Container\ContainerInterface;
11+
use PHPUnit_Framework_TestCase as TestCase;
12+
use Zend\Validator\ValidatorInterface;
13+
use Zend\Validator\ValidatorPluginManager;
14+
use Zend\Validator\ValidatorPluginManagerFactory;
15+
use Zend\ServiceManager\ServiceLocatorInterface;
16+
17+
class ValidatorPluginManagerFactoryTest extends TestCase
18+
{
19+
public function testFactoryReturnsPluginManager()
20+
{
21+
$container = $this->prophesize(ContainerInterface::class)->reveal();
22+
$factory = new ValidatorPluginManagerFactory();
23+
24+
$validators = $factory($container, ValidatorPluginManagerFactory::class);
25+
$this->assertInstanceOf(ValidatorPluginManager::class, $validators);
26+
27+
if (method_exists($validators, 'configure')) {
28+
// zend-servicemanager v3
29+
$this->assertAttributeSame($container, 'creationContext', $validators);
30+
} else {
31+
// zend-servicemanager v2
32+
$this->assertSame($container, $validators->getServiceLocator());
33+
}
34+
}
35+
36+
/**
37+
* @depends testFactoryReturnsPluginManager
38+
*/
39+
public function testFactoryConfiguresPluginManagerUnderContainerInterop()
40+
{
41+
$container = $this->prophesize(ContainerInterface::class)->reveal();
42+
$validator = $this->prophesize(ValidatorInterface::class)->reveal();
43+
44+
$factory = new ValidatorPluginManagerFactory();
45+
$validators = $factory($container, ValidatorPluginManagerFactory::class, [
46+
'services' => [
47+
'test' => $validator,
48+
],
49+
]);
50+
$this->assertSame($validator, $validators->get('test'));
51+
}
52+
53+
/**
54+
* @depends testFactoryReturnsPluginManager
55+
*/
56+
public function testFactoryConfiguresPluginManagerUnderServiceManagerV2()
57+
{
58+
$container = $this->prophesize(ServiceLocatorInterface::class);
59+
$container->willImplement(ContainerInterface::class);
60+
61+
$validator = $this->prophesize(ValidatorInterface::class)->reveal();
62+
63+
$factory = new ValidatorPluginManagerFactory();
64+
$factory->setCreationOptions([
65+
'services' => [
66+
'test' => $validator,
67+
],
68+
]);
69+
70+
$validators = $factory->createService($container->reveal());
71+
$this->assertSame($validator, $validators->get('test'));
72+
}
73+
}

0 commit comments

Comments
 (0)