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

Code refactor with the new servicemanager of ZF3 #49

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@
},
"require": {
"php": ">=5.5",
"zendframework/zend-stdlib": "~2.5"
"zendframework/zend-stdlib": "dev-develop as 2.8.0",
"container-interop/container-interop": "^1.1"
},
"require-dev": {
"zendframework/zend-cache": "~2.5",
"zendframework/zend-config": "~2.5",
"zendframework/zend-db": "~2.5",
"zendframework/zend-filter": "~2.5",
"zendframework/zend-cache": "dev-develop as 2.6.0",
"zendframework/zend-config": "dev-develop as 2.6.0",
"zendframework/zend-db": "dev-develop as 2.7.0",
"zendframework/zend-filter": "dev-develop as 2.6.0",
"zendframework/zend-http": "~2.5",
"zendframework/zend-i18n": "~2.5",
"zendframework/zend-math": "~2.5",
"zendframework/zend-servicemanager": "~2.5",
"zendframework/zend-session": "~2.5",
"zendframework/zend-i18n": "dev-develop as 2.6.0",
"zendframework/zend-math": "dev-develop as 2.6.0",
"zendframework/zend-servicemanager": "dev-develop as 2.7.0",
"zendframework/zend-session": "dev-develop as 2.6.0",
"zendframework/zend-uri": "~2.5",
"fabpot/php-cs-fixer": "1.7.*",
"phpunit/PHPUnit": "~4.0"
Expand Down
3 changes: 2 additions & 1 deletion src/Explode.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Traversable;
use Zend\Stdlib\ArrayUtils;
use Zend\ServiceManager\ServiceManager;

class Explode extends AbstractValidator implements ValidatorPluginManagerAwareInterface
{
Expand Down Expand Up @@ -85,7 +86,7 @@ public function setValidatorPluginManager(ValidatorPluginManager $pluginManager)
public function getValidatorPluginManager()
{
if (!$this->pluginManager) {
$this->setValidatorPluginManager(new ValidatorPluginManager());
$this->setValidatorPluginManager(new ValidatorPluginManager(new ServiceManager));
}

return $this->pluginManager;
Expand Down
6 changes: 4 additions & 2 deletions src/StaticValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace Zend\Validator;

use Zend\ServiceManager\ServiceManager;

class StaticValidator
{
/**
Expand All @@ -26,7 +28,7 @@ public static function setPluginManager(ValidatorPluginManager $plugins = null)
{
// Don't share by default to allow different arguments on subsequent calls
if ($plugins instanceof ValidatorPluginManager) {
$plugins->setShareByDefault(false);
$plugins->configure(['shared_by_default' => false]);
}
static::$plugins = $plugins;
}
Expand All @@ -39,7 +41,7 @@ public static function setPluginManager(ValidatorPluginManager $plugins = null)
public static function getPluginManager()
{
if (null === static::$plugins) {
static::setPluginManager(new ValidatorPluginManager());
static::setPluginManager(new ValidatorPluginManager(new ServiceManager));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a very common pattern repeated across all SM v3 PRs. May the SM should be injected by default?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Maks3w note the context here: static usage. By very definition, no instance is available.

Within the MVC and the various plugin manager factories, the SM will be injected by default; that's the new design. (Instead of passing it to setServiceLocator(), it becomes a constructor argument, and becomes the "creation context", which means that it is that instance, not the plugin manager, passed to factories.)

}
return static::$plugins;
}
Expand Down
3 changes: 2 additions & 1 deletion src/ValidatorChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Countable;
use Zend\Stdlib\PriorityQueue;
use Zend\ServiceManager\ServiceManager;

class ValidatorChain implements
Countable,
Expand Down Expand Up @@ -66,7 +67,7 @@ public function count()
public function getPluginManager()
{
if (!$this->plugins) {
$this->setPluginManager(new ValidatorPluginManager());
$this->setPluginManager(new ValidatorPluginManager(new ServiceManager));
}
return $this->plugins;
}
Expand Down
414 changes: 289 additions & 125 deletions src/ValidatorPluginManager.php

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions test/StaticValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ public function testLazyLoadsValidatorPluginManagerByDefault()

public function testCanSetCustomPluginManager()
{
$plugins = new ValidatorPluginManager();
$plugins = new ValidatorPluginManager($this->getMockBuilder('Zend\ServiceManager\ServiceManager')->getMock());
StaticValidator::setPluginManager($plugins);
$this->assertSame($plugins, StaticValidator::getPluginManager());
}

public function testPassingNullWhenSettingPluginManagerResetsPluginManager()
{
$plugins = new ValidatorPluginManager();
$plugins = new ValidatorPluginManager($this->getMockBuilder('Zend\ServiceManager\ServiceManager')->getMock());
StaticValidator::setPluginManager($plugins);
$this->assertSame($plugins, StaticValidator::getPluginManager());
StaticValidator::setPluginManager(null);
Expand All @@ -133,10 +133,10 @@ public function testPassingNullWhenSettingPluginManagerResetsPluginManager()
public function parameterizedData()
{
return [
'valid-positive-range' => [5, 'Between', ['min' => 1, 'max' => 10], true],
'valid-negative-range' => [-5, 'Between', ['min' => -10, 'max' => -1], true],
'invalid-positive-range' => [-5, 'Between', ['min' => 1, 'max' => 10], false],
'invalid-negative-range' => [5, 'Between', ['min' => -10, 'max' => -1], false],
'valid-positive-range' => [5, 'between', ['min' => 1, 'max' => 10], true],
'valid-negative-range' => [-5, 'between', ['min' => -10, 'max' => -1], true],
'invalid-positive-range' => [-5, 'between', ['min' => 1, 'max' => 10], false],
'invalid-negative-range' => [5, 'between', ['min' => -10, 'max' => -1], false],
];
}

Expand All @@ -151,8 +151,8 @@ public function testExecuteValidWithParameters($value, $validator, $options, $ex
public function invalidParameterizedData()
{
return [
'positive-range' => [5, 'Between', [1, 10]],
'negative-range' => [-5, 'Between', [-10, -1]],
'positive-range' => [5, 'between', [1, 10]],
'negative-range' => [-5, 'between', [-10, -1]],
];
}

Expand Down
20 changes: 10 additions & 10 deletions test/ValidatorPluginManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace ZendTest\Validator;

use Zend\Validator\ValidatorPluginManager;
use Zend\ServiceManager\ServiceManager;

/**
* @group Zend_Validator
Expand All @@ -18,7 +19,7 @@ class ValidatorPluginManagerTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->validators = new ValidatorPluginManager();
$this->validators = new ValidatorPluginManager(new ServiceManager);
}

public function testAllowsInjectingTranslator()
Expand All @@ -35,11 +36,10 @@ public function testAllowsInjectingTranslator()
->with($this->equalTo('MvcTranslator'))
->will($this->returnValue(true));

$this->validators->setServiceLocator($serviceLocator);
$this->assertSame($serviceLocator, $this->validators->getServiceLocator());
$validators = new ValidatorPluginManager($serviceLocator);

$validator = $this->validators->get('notempty');
$this->assertSame($translator, $validator->getTranslator());
$validator = $validators->get('notempty');
$this->assertEquals($translator, $validator->getTranslator());
}

public function testNoTranslatorInjectedWhenTranslatorIsNotPresent()
Expand All @@ -50,23 +50,23 @@ public function testNoTranslatorInjectedWhenTranslatorIsNotPresent()
->with($this->equalTo('MvcTranslator'))
->will($this->returnValue(false));

$this->validators->setServiceLocator($serviceLocator);
$this->assertSame($serviceLocator, $this->validators->getServiceLocator());
$validators = new ValidatorPluginManager($serviceLocator);

$validator = $this->validators->get('notempty');
$validator = $validators->get('notempty');
$this->assertNull($validator->getTranslator());
}

public function testRegisteringInvalidValidatorRaisesException()
{
$this->setExpectedException('Zend\Validator\Exception\RuntimeException');
$this->setExpectedException('Zend\ServiceManager\Exception\InvalidServiceException');
$this->validators->setService('test', $this);
$this->validators->get('test');
}

public function testLoadingInvalidValidatorRaisesException()
{
$this->validators->setInvokableClass('test', get_class($this));
$this->setExpectedException('Zend\Validator\Exception\RuntimeException');
$this->setExpectedException('Zend\ServiceManager\Exception\InvalidServiceException');
$this->validators->get('test');
}

Expand Down