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

Commit

Permalink
Code refactor with the new servicemanager
Browse files Browse the repository at this point in the history
  • Loading branch information
ezimuel committed Jan 7, 2016
1 parent 261bab4 commit a98d7f6
Show file tree
Hide file tree
Showing 7 changed files with 325 additions and 156 deletions.
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));
}
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

0 comments on commit a98d7f6

Please sign in to comment.