From 18f35b4f7e666552700cd9d5b7cd007bc1405a7d Mon Sep 17 00:00:00 2001 From: blanchonvincent Date: Sat, 8 Sep 2012 16:46:06 +0200 Subject: [PATCH 01/12] Add adapter plugin manager tests Add adapter plugin manager tests --- src/AdapterPluginManager.php | 31 ++++++++++++++++ test/AdapterPluginManagerTest.php | 62 +++++++++++++++++++++++++++++++ test/FactoryTest.php | 27 ++++++++++++++ 3 files changed, 120 insertions(+) create mode 100644 test/AdapterPluginManagerTest.php create mode 100644 test/FactoryTest.php diff --git a/src/AdapterPluginManager.php b/src/AdapterPluginManager.php index 9686347..c77d674 100644 --- a/src/AdapterPluginManager.php +++ b/src/AdapterPluginManager.php @@ -35,6 +35,37 @@ class AdapterPluginManager extends AbstractPluginManager 'iterator' => 'Zend\Paginator\Adapter\Iterator', 'null' => 'Zend\Paginator\Adapter\Null', ); + + /** + * Attempt to create an instance via an invokable class + * + * Overrides parent implementation by passing $creationOptions to the + * constructor, if non-null. + * + * @param string $canonicalName + * @param string $requestedName + * @return null|\stdClass + * @throws Exception\ServiceNotCreatedException If resolved class does not exist + */ + protected function createFromInvokable($canonicalName, $requestedName) + { + $invokable = $this->invokableClasses[$canonicalName]; + + if (null === $this->creationOptions + || (is_array($this->creationOptions) && empty($this->creationOptions)) + ) { + $instance = new $invokable(); + } else { + if($canonicalName == "dbselect" && is_array($this->creationOptions)) { + $class = new \ReflectionClass($invokable); + $instance = $class->newInstanceArgs($this->creationOptions); + } else { + $instance = new $invokable($this->creationOptions); + } + } + + return $instance; + } /** * Validate the plugin diff --git a/test/AdapterPluginManagerTest.php b/test/AdapterPluginManagerTest.php new file mode 100644 index 0000000..3bf90a9 --- /dev/null +++ b/test/AdapterPluginManagerTest.php @@ -0,0 +1,62 @@ +adapaterPluginManager = new AdapterPluginManager(); + $this->mockSelect = $this->getMock('Zend\Db\Sql\Select'); + + $mockStatement = $this->getMock('Zend\Db\Adapter\Driver\StatementInterface'); + $mockResult = $this->getMock('Zend\Db\Adapter\Driver\ResultInterface'); + + $mockDriver = $this->getMock('Zend\Db\Adapter\Driver\DriverInterface'); + $mockDriver->expects($this->any())->method('createStatement')->will($this->returnValue($mockStatement)); + $mockStatement->expects($this->any())->method('execute')->will($this->returnValue($mockResult)); + $mockPlatform = $this->getMock('Zend\Db\Adapter\Platform\PlatformInterface'); + $mockPlatform->expects($this->any())->method('getName')->will($this->returnValue('platform')); + + $this->mockAdapter = $this->getMockForAbstractClass( + 'Zend\Db\Adapter\Adapter', + array($mockDriver, $mockPlatform) + ); + } + + public function testCanRetrieveAdapterPlugin() + { + $plugin = $this->adapaterPluginManager->get('array', array(1, 2, 3)); + $this->assertInstanceOf('Zend\Paginator\Adapter\ArrayAdapter', $plugin); + $plugin = $this->adapaterPluginManager->get('iterator', new \ArrayIterator(range(1, 101))); + $this->assertInstanceOf('Zend\Paginator\Adapter\Iterator', $plugin); + $plugin = $this->adapaterPluginManager->get('dbselect', array($this->mockSelect, $this->mockAdapter)); + $this->assertInstanceOf('Zend\Paginator\Adapter\DbSelect', $plugin); + $plugin = $this->adapaterPluginManager->get('null', 101); + $this->assertInstanceOf('Zend\Paginator\Adapter\Null', $plugin); + } + +} \ No newline at end of file diff --git a/test/FactoryTest.php b/test/FactoryTest.php new file mode 100644 index 0000000..6b00c0b --- /dev/null +++ b/test/FactoryTest.php @@ -0,0 +1,27 @@ + Date: Sat, 8 Sep 2012 16:52:18 +0200 Subject: [PATCH 02/12] Add factory paginator tests Add factory paginator tests --- src/Factory.php | 6 ++++-- src/Paginator.php | 2 +- test/FactoryTest.php | 14 +++++++++++++- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Factory.php b/src/Factory.php index 4f564e0..afc6fc5 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -16,11 +16,13 @@ * @category Zend * @package Zend_Paginator */ -class Factory implements Countable, IteratorAggregate +class Factory { + protected static $adapters; + public static function factory($items, $adapter) { - if(!$adapter instanceof AdapterInterface && $adapter instanceof AdapterAggregateInterface) { + if(!$adapter instanceof AdapterInterface && !$adapter instanceof AdapterAggregateInterface) { $adapter = self::getAdapterPluginManager()->get($adapter, $items); } diff --git a/src/Paginator.php b/src/Paginator.php index b6b245f..4e95e74 100644 --- a/src/Paginator.php +++ b/src/Paginator.php @@ -287,7 +287,7 @@ public function __construct($adapter) $this->adapter = $adapter->getPaginatorAdapter(); } else { throw new Exception\InvalidArgumentException( - 'Zend_Paginator only accepts instances of the type ' . + 'Zend\Paginator only accepts instances of the type ' . 'Zend\Paginator\Adapter\AdapterInterface or Zend\Paginator\AdapterAggregateInterface.' ); } diff --git a/test/FactoryTest.php b/test/FactoryTest.php index 6b00c0b..05fa503 100644 --- a/test/FactoryTest.php +++ b/test/FactoryTest.php @@ -11,6 +11,7 @@ namespace ZendTest\Paginator; use Zend\Paginator\Factory; +use Zend\Paginator\Adapter\ArrayAdapter; /** * @category Zend @@ -20,8 +21,19 @@ */ class FactoryTest extends \PHPUnit_Framework_TestCase { + public function testCanFactoryPaginatorWithStringAdapterObject() + { + $datas = array(1, 2, 3); + $paginator = Factory::Factory($datas, new ArrayAdapter($datas)); + $this->assertInstanceOf('Zend\Paginator\Adapter\ArrayAdapter', $paginator->getAdapter()); + $this->assertEquals(count($datas), $paginator->getCurrentItemCount()); + } + public function testCanFactoryPaginatorWithStringAdapterName() { - + $datas = array(1, 2, 3); + $paginator = Factory::Factory($datas, 'array'); + $this->assertInstanceOf('Zend\Paginator\Adapter\ArrayAdapter', $paginator->getAdapter()); + $this->assertEquals(count($datas), $paginator->getCurrentItemCount()); } } \ No newline at end of file From baaa3ea4a5305b5099d3dcec0cff2500b92606dc Mon Sep 17 00:00:00 2001 From: blanchonvincent Date: Sat, 8 Sep 2012 16:56:14 +0200 Subject: [PATCH 03/12] Add test with AdapterAggregateInterface Add test with AdapterAggregateInterface --- test/FactoryTest.php | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/test/FactoryTest.php b/test/FactoryTest.php index 05fa503..8cf9eff 100644 --- a/test/FactoryTest.php +++ b/test/FactoryTest.php @@ -10,8 +10,8 @@ namespace ZendTest\Paginator; -use Zend\Paginator\Factory; -use Zend\Paginator\Adapter\ArrayAdapter; +use Zend\Paginator; +use Zend\Paginator\Adapter; /** * @category Zend @@ -24,7 +24,7 @@ class FactoryTest extends \PHPUnit_Framework_TestCase public function testCanFactoryPaginatorWithStringAdapterObject() { $datas = array(1, 2, 3); - $paginator = Factory::Factory($datas, new ArrayAdapter($datas)); + $paginator = Paginator\Factory::Factory($datas, new Adapter\ArrayAdapter($datas)); $this->assertInstanceOf('Zend\Paginator\Adapter\ArrayAdapter', $paginator->getAdapter()); $this->assertEquals(count($datas), $paginator->getCurrentItemCount()); } @@ -32,8 +32,22 @@ public function testCanFactoryPaginatorWithStringAdapterObject() public function testCanFactoryPaginatorWithStringAdapterName() { $datas = array(1, 2, 3); - $paginator = Factory::Factory($datas, 'array'); + $paginator = Paginator\Factory::Factory($datas, 'array'); $this->assertInstanceOf('Zend\Paginator\Adapter\ArrayAdapter', $paginator->getAdapter()); $this->assertEquals(count($datas), $paginator->getCurrentItemCount()); } + + public function testCanFactoryPaginatorWithStringAdapterAggregate() + { + $paginator = Paginator\Factory::Factory(null, new TestArrayAggregate); + $this->assertInstanceOf('Zend\Paginator\Adapter\ArrayAdapter', $paginator->getAdapter()); + } +} + +class TestArrayAggregate implements Paginator\AdapterAggregateInterface +{ + public function getPaginatorAdapter() + { + return new Adapter\ArrayAdapter(array(1, 2, 3, 4)); + } } \ No newline at end of file From 98320baa8f757e7cf420612fd5c1043b5d3980e3 Mon Sep 17 00:00:00 2001 From: blanchonvincent Date: Sat, 8 Sep 2012 17:02:53 +0200 Subject: [PATCH 04/12] Fix unit tests error Fix unit tests error --- test/AdapterPluginManagerTest.php | 2 +- test/FactoryTest.php | 9 +-------- test/PaginatorTest.php | 9 +-------- test/TestAsset/TestArrayAggregate.php | 27 +++++++++++++++++++++++++++ 4 files changed, 30 insertions(+), 17 deletions(-) create mode 100644 test/TestAsset/TestArrayAggregate.php diff --git a/test/AdapterPluginManagerTest.php b/test/AdapterPluginManagerTest.php index 3bf90a9..a7ae11e 100644 --- a/test/AdapterPluginManagerTest.php +++ b/test/AdapterPluginManagerTest.php @@ -18,7 +18,7 @@ * @subpackage UnitTests * @group Zend_Paginator */ -class FactoryTest extends \PHPUnit_Framework_TestCase +class AdapterPluginManagerTest extends \PHPUnit_Framework_TestCase { protected $adapaterPluginManager; diff --git a/test/FactoryTest.php b/test/FactoryTest.php index 8cf9eff..9cd090e 100644 --- a/test/FactoryTest.php +++ b/test/FactoryTest.php @@ -12,6 +12,7 @@ use Zend\Paginator; use Zend\Paginator\Adapter; +use ZendTest\Paginator\TestAsset\TestArrayAggregate; /** * @category Zend @@ -42,12 +43,4 @@ public function testCanFactoryPaginatorWithStringAdapterAggregate() $paginator = Paginator\Factory::Factory(null, new TestArrayAggregate); $this->assertInstanceOf('Zend\Paginator\Adapter\ArrayAdapter', $paginator->getAdapter()); } -} - -class TestArrayAggregate implements Paginator\AdapterAggregateInterface -{ - public function getPaginatorAdapter() - { - return new Adapter\ArrayAdapter(array(1, 2, 3, 4)); - } } \ No newline at end of file diff --git a/test/PaginatorTest.php b/test/PaginatorTest.php index 23a724b..3ff4d4c 100644 --- a/test/PaginatorTest.php +++ b/test/PaginatorTest.php @@ -23,6 +23,7 @@ use Zend\Paginator\Exception; use Zend\View; use Zend\View\Helper; +use ZendTest\Paginator\TestAsset\TestArrayAggregate; /** * @category Zend @@ -796,11 +797,3 @@ public function testGetCacheId() } } - -class TestArrayAggregate implements Paginator\AdapterAggregateInterface -{ - public function getPaginatorAdapter() - { - return new Adapter\ArrayAdapter(array(1, 2, 3, 4)); - } -} diff --git a/test/TestAsset/TestArrayAggregate.php b/test/TestAsset/TestArrayAggregate.php new file mode 100644 index 0000000..b90f0e8 --- /dev/null +++ b/test/TestAsset/TestArrayAggregate.php @@ -0,0 +1,27 @@ + Date: Sat, 15 Sep 2012 14:15:39 +0200 Subject: [PATCH 05/12] Add class as abstract Make class abstract, offer only static methods --- src/Factory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Factory.php b/src/Factory.php index afc6fc5..f2c5ad6 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -16,7 +16,7 @@ * @category Zend * @package Zend_Paginator */ -class Factory +abstract class Factory { protected static $adapters; From 662d7718ba62e9081fed66de92c0d94b2e8c0f14 Mon Sep 17 00:00:00 2001 From: blanchonvincent Date: Wed, 19 Sep 2012 12:56:54 +0200 Subject: [PATCH 06/12] Add unit test and improve existing Add unit test and improve existing --- test/FactoryTest.php | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/test/FactoryTest.php b/test/FactoryTest.php index 9cd090e..355251b 100644 --- a/test/FactoryTest.php +++ b/test/FactoryTest.php @@ -22,10 +22,34 @@ */ class FactoryTest extends \PHPUnit_Framework_TestCase { + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $mockSelect; + + protected $mockAdapter; + + protected function setUp() + { + $this->mockSelect = $this->getMock('Zend\Db\Sql\Select'); + + $mockStatement = $this->getMock('Zend\Db\Adapter\Driver\StatementInterface'); + $mockResult = $this->getMock('Zend\Db\Adapter\Driver\ResultInterface'); + + $mockDriver = $this->getMock('Zend\Db\Adapter\Driver\DriverInterface'); + $mockDriver->expects($this->any())->method('createStatement')->will($this->returnValue($mockStatement)); + $mockStatement->expects($this->any())->method('execute')->will($this->returnValue($mockResult)); + $mockPlatform = $this->getMock('Zend\Db\Adapter\Platform\PlatformInterface'); + $mockPlatform->expects($this->any())->method('getName')->will($this->returnValue('platform')); + + $this->mockAdapter = $this->getMockForAbstractClass( + 'Zend\Db\Adapter\Adapter', + array($mockDriver, $mockPlatform) + ); + } + public function testCanFactoryPaginatorWithStringAdapterObject() { $datas = array(1, 2, 3); - $paginator = Paginator\Factory::Factory($datas, new Adapter\ArrayAdapter($datas)); + $paginator = Paginator\Factory::factory($datas, new Adapter\ArrayAdapter($datas)); $this->assertInstanceOf('Zend\Paginator\Adapter\ArrayAdapter', $paginator->getAdapter()); $this->assertEquals(count($datas), $paginator->getCurrentItemCount()); } @@ -33,14 +57,20 @@ public function testCanFactoryPaginatorWithStringAdapterObject() public function testCanFactoryPaginatorWithStringAdapterName() { $datas = array(1, 2, 3); - $paginator = Paginator\Factory::Factory($datas, 'array'); + $paginator = Paginator\Factory::factory($datas, 'array'); $this->assertInstanceOf('Zend\Paginator\Adapter\ArrayAdapter', $paginator->getAdapter()); $this->assertEquals(count($datas), $paginator->getCurrentItemCount()); } public function testCanFactoryPaginatorWithStringAdapterAggregate() { - $paginator = Paginator\Factory::Factory(null, new TestArrayAggregate); + $paginator = Paginator\Factory::factory(null, new TestArrayAggregate); $this->assertInstanceOf('Zend\Paginator\Adapter\ArrayAdapter', $paginator->getAdapter()); } + + public function testCanFactoryPaginatorWithDbSelect() + { + $paginator = Paginator\Factory::factory(array($this->mockSelect, $this->mockAdapter), 'dbselect'); + $this->assertInstanceOf('Zend\Paginator\Adapter\DbSelect', $paginator->getAdapter()); + } } \ No newline at end of file From bbaff212dae7388675f09c9b8dfc979331ae9e97 Mon Sep 17 00:00:00 2001 From: blanchonvincent Date: Thu, 27 Sep 2012 22:38:54 +0200 Subject: [PATCH 07/12] Updte factory for one argument Updte factory for one argument --- src/AdapterPluginManager.php | 2 +- src/Factory.php | 33 ++++++++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/AdapterPluginManager.php b/src/AdapterPluginManager.php index c77d674..5b1babd 100644 --- a/src/AdapterPluginManager.php +++ b/src/AdapterPluginManager.php @@ -35,7 +35,7 @@ class AdapterPluginManager extends AbstractPluginManager 'iterator' => 'Zend\Paginator\Adapter\Iterator', 'null' => 'Zend\Paginator\Adapter\Null', ); - + /** * Attempt to create an instance via an invokable class * diff --git a/src/Factory.php b/src/Factory.php index f2c5ad6..4a78735 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -10,7 +10,9 @@ namespace Zend\Paginator; +use Traversable; use Zend\Paginator\Adapter\AdapterInterface; +use Zend\Stdlib\ArrayUtils; /** * @category Zend @@ -19,16 +21,37 @@ abstract class Factory { protected static $adapters; - - public static function factory($items, $adapter) - { + + public static function factory($items, $adapter = null) + { + if(null === $adapter) { + if ($items instanceof Traversable) { + $items = ArrayUtils::iteratorToArray($items); + } + if(!is_array($items)) { + throw new Exception\InvalidArgumentException( + 'The factory needs an associative array ' + . 'or a Traversable object as an argument when ' + . "it's used with one parameter" + ); + } + if(!isset($items['adapter']) && !isset($items['items'])) { + throw new Exception\InvalidArgumentException( + 'The factory needs an associative array ' + . 'or a Traversable object with keys ' + . '"adapter" and "items"' + ); + } + $adapter = $items['adapter']; + $items = $items['items']; + } if(!$adapter instanceof AdapterInterface && !$adapter instanceof AdapterAggregateInterface) { $adapter = self::getAdapterPluginManager()->get($adapter, $items); } - + return new Paginator($adapter); } - + /** * Change the adapter plugin manager * From 7854660fefa0538c218e8c0319f2bb38de4e337a Mon Sep 17 00:00:00 2001 From: blanchonvincent Date: Thu, 27 Sep 2012 22:39:06 +0200 Subject: [PATCH 08/12] Add tests for factory for one argument Add tests for factory for one argument --- test/Adapter/DbSelectTest.php | 1 - test/AdapterPluginManagerTest.php | 14 ++++----- test/FactoryTest.php | 49 +++++++++++++++++++++++++------ test/PaginatorTest.php | 2 +- 4 files changed, 48 insertions(+), 18 deletions(-) diff --git a/test/Adapter/DbSelectTest.php b/test/Adapter/DbSelectTest.php index 7e63fb1..4e73b6b 100644 --- a/test/Adapter/DbSelectTest.php +++ b/test/Adapter/DbSelectTest.php @@ -13,7 +13,6 @@ use Zend\Paginator\Adapter\DbSelect; use Zend\Db\Sql\Select; use Zend\Db\Sql\Expression; -use Zend\Db\Adapter\Adapter as DbAdapter; /** * @category Zend diff --git a/test/AdapterPluginManagerTest.php b/test/AdapterPluginManagerTest.php index a7ae11e..898217a 100644 --- a/test/AdapterPluginManagerTest.php +++ b/test/AdapterPluginManagerTest.php @@ -21,17 +21,17 @@ class AdapterPluginManagerTest extends \PHPUnit_Framework_TestCase { protected $adapaterPluginManager; - + /** @var \PHPUnit_Framework_MockObject_MockObject */ protected $mockSelect; - + protected $mockAdapter; - + protected function setUp() { $this->adapaterPluginManager = new AdapterPluginManager(); $this->mockSelect = $this->getMock('Zend\Db\Sql\Select'); - + $mockStatement = $this->getMock('Zend\Db\Adapter\Driver\StatementInterface'); $mockResult = $this->getMock('Zend\Db\Adapter\Driver\ResultInterface'); @@ -40,13 +40,13 @@ protected function setUp() $mockStatement->expects($this->any())->method('execute')->will($this->returnValue($mockResult)); $mockPlatform = $this->getMock('Zend\Db\Adapter\Platform\PlatformInterface'); $mockPlatform->expects($this->any())->method('getName')->will($this->returnValue('platform')); - + $this->mockAdapter = $this->getMockForAbstractClass( 'Zend\Db\Adapter\Adapter', array($mockDriver, $mockPlatform) ); } - + public function testCanRetrieveAdapterPlugin() { $plugin = $this->adapaterPluginManager->get('array', array(1, 2, 3)); @@ -59,4 +59,4 @@ public function testCanRetrieveAdapterPlugin() $this->assertInstanceOf('Zend\Paginator\Adapter\Null', $plugin); } -} \ No newline at end of file +} diff --git a/test/FactoryTest.php b/test/FactoryTest.php index 355251b..e13e2be 100644 --- a/test/FactoryTest.php +++ b/test/FactoryTest.php @@ -24,13 +24,13 @@ class FactoryTest extends \PHPUnit_Framework_TestCase { /** @var \PHPUnit_Framework_MockObject_MockObject */ protected $mockSelect; - + protected $mockAdapter; - + protected function setUp() { $this->mockSelect = $this->getMock('Zend\Db\Sql\Select'); - + $mockStatement = $this->getMock('Zend\Db\Adapter\Driver\StatementInterface'); $mockResult = $this->getMock('Zend\Db\Adapter\Driver\ResultInterface'); @@ -39,13 +39,13 @@ protected function setUp() $mockStatement->expects($this->any())->method('execute')->will($this->returnValue($mockResult)); $mockPlatform = $this->getMock('Zend\Db\Adapter\Platform\PlatformInterface'); $mockPlatform->expects($this->any())->method('getName')->will($this->returnValue('platform')); - + $this->mockAdapter = $this->getMockForAbstractClass( 'Zend\Db\Adapter\Adapter', array($mockDriver, $mockPlatform) ); } - + public function testCanFactoryPaginatorWithStringAdapterObject() { $datas = array(1, 2, 3); @@ -53,7 +53,7 @@ public function testCanFactoryPaginatorWithStringAdapterObject() $this->assertInstanceOf('Zend\Paginator\Adapter\ArrayAdapter', $paginator->getAdapter()); $this->assertEquals(count($datas), $paginator->getCurrentItemCount()); } - + public function testCanFactoryPaginatorWithStringAdapterName() { $datas = array(1, 2, 3); @@ -61,16 +61,47 @@ public function testCanFactoryPaginatorWithStringAdapterName() $this->assertInstanceOf('Zend\Paginator\Adapter\ArrayAdapter', $paginator->getAdapter()); $this->assertEquals(count($datas), $paginator->getCurrentItemCount()); } - + public function testCanFactoryPaginatorWithStringAdapterAggregate() { $paginator = Paginator\Factory::factory(null, new TestArrayAggregate); $this->assertInstanceOf('Zend\Paginator\Adapter\ArrayAdapter', $paginator->getAdapter()); } - + public function testCanFactoryPaginatorWithDbSelect() { $paginator = Paginator\Factory::factory(array($this->mockSelect, $this->mockAdapter), 'dbselect'); $this->assertInstanceOf('Zend\Paginator\Adapter\DbSelect', $paginator->getAdapter()); } -} \ No newline at end of file + + public function testCanFactoryPaginatorWithOneParameterWithArrayAdapter() + { + $datas = array( + 'items' => array(1, 2, 3), + 'adapter' => 'array', + ); + $paginator = Paginator\Factory::factory($datas); + $this->assertInstanceOf('Zend\Paginator\Adapter\ArrayAdapter', $paginator->getAdapter()); + $this->assertEquals(count($datas['items']), $paginator->getCurrentItemCount()); + } + + public function testCanFactoryPaginatorWithOneParameterWithDbAdapter() + { + $datas = array( + 'items' => array($this->mockSelect, $this->mockAdapter), + 'adapter' => 'dbselect', + ); + $paginator = Paginator\Factory::factory($datas); + $this->assertInstanceOf('Zend\Paginator\Adapter\DbSelect', $paginator->getAdapter()); + } + + public function testCanFactoryPaginatorWithOneBadParameter() + { + $datas = array( + array(1, 2, 3), + 'array', + ); + $this->setExpectedException('Zend\Paginator\Exception\InvalidArgumentException'); + $paginator = Paginator\Factory::factory($datas); + } +} diff --git a/test/PaginatorTest.php b/test/PaginatorTest.php index 3ff4d4c..e5c0f9c 100644 --- a/test/PaginatorTest.php +++ b/test/PaginatorTest.php @@ -13,7 +13,6 @@ use ReflectionMethod; use stdClass; use Zend\Cache\StorageFactory as CacheFactory; -use Zend\Cache\Storage\Adapter\AdapterInterface as CacheAdapter; use Zend\Config; use Zend\Db\Adapter as DbAdapter; use Zend\Db\Sql; @@ -83,6 +82,7 @@ protected function _getTmpDir() mkdir($tmpDir); } $this->cacheDir = $tmpDir; + return $tmpDir; } From b7be76913bf5482bf42513274a0047a20c42df1c Mon Sep 17 00:00:00 2001 From: blanchonvincent Date: Mon, 8 Oct 2012 21:04:58 +0200 Subject: [PATCH 09/12] CS space & returning instance CS space & returning instance --- src/AdapterPluginManager.php | 8 +++----- src/Factory.php | 8 ++++---- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/AdapterPluginManager.php b/src/AdapterPluginManager.php index 5b1babd..a2b6acc 100644 --- a/src/AdapterPluginManager.php +++ b/src/AdapterPluginManager.php @@ -54,17 +54,15 @@ protected function createFromInvokable($canonicalName, $requestedName) if (null === $this->creationOptions || (is_array($this->creationOptions) && empty($this->creationOptions)) ) { - $instance = new $invokable(); + return new $invokable(); } else { if($canonicalName == "dbselect" && is_array($this->creationOptions)) { $class = new \ReflectionClass($invokable); - $instance = $class->newInstanceArgs($this->creationOptions); + return $class->newInstanceArgs($this->creationOptions); } else { - $instance = new $invokable($this->creationOptions); + return new $invokable($this->creationOptions); } } - - return $instance; } /** diff --git a/src/Factory.php b/src/Factory.php index 4a78735..ace5279 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -24,18 +24,18 @@ abstract class Factory public static function factory($items, $adapter = null) { - if(null === $adapter) { + if (null === $adapter) { if ($items instanceof Traversable) { $items = ArrayUtils::iteratorToArray($items); } - if(!is_array($items)) { + if (!is_array($items)) { throw new Exception\InvalidArgumentException( 'The factory needs an associative array ' . 'or a Traversable object as an argument when ' . "it's used with one parameter" ); } - if(!isset($items['adapter']) && !isset($items['items'])) { + if (!isset($items['adapter']) && !isset($items['items'])) { throw new Exception\InvalidArgumentException( 'The factory needs an associative array ' . 'or a Traversable object with keys ' @@ -45,7 +45,7 @@ public static function factory($items, $adapter = null) $adapter = $items['adapter']; $items = $items['items']; } - if(!$adapter instanceof AdapterInterface && !$adapter instanceof AdapterAggregateInterface) { + if (!$adapter instanceof AdapterInterface && !$adapter instanceof AdapterAggregateInterface) { $adapter = self::getAdapterPluginManager()->get($adapter, $items); } From 8aab003e17556387d6ce3c1438b027b0ab8e64f8 Mon Sep 17 00:00:00 2001 From: blanchonvincent Date: Mon, 8 Oct 2012 21:23:48 +0200 Subject: [PATCH 10/12] Add service & add tests Add service & add tests --- test/AdapterPluginManagerTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/AdapterPluginManagerTest.php b/test/AdapterPluginManagerTest.php index 898217a..001e84b 100644 --- a/test/AdapterPluginManagerTest.php +++ b/test/AdapterPluginManagerTest.php @@ -11,6 +11,8 @@ namespace ZendTest\Paginator; use Zend\Paginator\AdapterPluginManager; +use Zend\ServiceManager\ServiceManager; +use Zend\Mvc\Service\ServiceManagerConfig; /** * @category Zend @@ -59,4 +61,17 @@ public function testCanRetrieveAdapterPlugin() $this->assertInstanceOf('Zend\Paginator\Adapter\Null', $plugin); } + public function testCanRetrievePluginManagerWithServiceManager() + { + $sm = $this->serviceManager = new ServiceManager( + new ServiceManagerConfig(array( + 'factories' => array( + 'PaginatorPluginManager' => 'Zend\Mvc\Service\PaginatorPluginManagerFactory', + ), + )) + ); + $sm->setService('Config', array()); + $adapterPluginManager = $sm->get('PaginatorPluginManager'); + $this->assertInstanceOf('Zend\Paginator\AdapterPluginManager', $adapterPluginManager); + } } From 56a88a12a07f7f73b2f59ff812c4dbe85c3a1fbd Mon Sep 17 00:00:00 2001 From: blanchonvincent Date: Wed, 10 Oct 2012 20:35:18 +0200 Subject: [PATCH 11/12] Code refactory to make easier Code refactory to make easier --- src/Factory.php | 90 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 63 insertions(+), 27 deletions(-) diff --git a/src/Factory.php b/src/Factory.php index ace5279..6aef74f 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -20,38 +20,74 @@ */ abstract class Factory { + /** + * Adapter plugin manager + * @var AdapterPluginManager + */ protected static $adapters; - public static function factory($items, $adapter = null) + /** + * Create adapter from items if necessary, and return paginator + * @param Traversable/array $items + * @return Paginator + */ + protected static function createAdapterFromItems($items) { - if (null === $adapter) { - if ($items instanceof Traversable) { - $items = ArrayUtils::iteratorToArray($items); - } - if (!is_array($items)) { - throw new Exception\InvalidArgumentException( - 'The factory needs an associative array ' - . 'or a Traversable object as an argument when ' - . "it's used with one parameter" - ); - } - if (!isset($items['adapter']) && !isset($items['items'])) { - throw new Exception\InvalidArgumentException( - 'The factory needs an associative array ' - . 'or a Traversable object with keys ' - . '"adapter" and "items"' - ); - } - $adapter = $items['adapter']; - $items = $items['items']; + if ($items instanceof Traversable) { + $items = ArrayUtils::iteratorToArray($items); } - if (!$adapter instanceof AdapterInterface && !$adapter instanceof AdapterAggregateInterface) { - $adapter = self::getAdapterPluginManager()->get($adapter, $items); + if (!is_array($items)) { + throw new Exception\InvalidArgumentException( + 'The factory needs an associative array ' + . 'or a Traversable object as an argument when ' + . "it's used with one parameter" + ); } + if (!isset($items['adapter']) && !isset($items['items'])) { + throw new Exception\InvalidArgumentException( + 'The factory needs an associative array ' + . 'or a Traversable object with keys ' + . '"adapter" and "items"' + ); + } + $adapter = $items['adapter']; + $items = $items['items']; + + $paginator = static::getAdapterFromManager($items, $adapter); + return $paginator; + } + /** + * Get adapter from manager if necessary, and return paginator + * @param mixed $items + * @param mixed $adapter + * @return Paginator + */ + protected static function getAdapterFromManager($items, $adapter) + { + if ($adapter instanceof AdapterInterface || $adapter instanceof AdapterAggregateInterface) { + return new Paginator($adapter); + } + $adapter = static::getAdapterPluginManager()->get($adapter, $items); return new Paginator($adapter); } + /** + * Create paginator with items and adapter + * @param mixed $items + * @param mixed $adapter + * @return Paginator + */ + public static function factory($items, $adapter = null) + { + if (null === $adapter) { + $paginator = static::createAdapterFromItems($items); + return $paginator; + } + $paginator = static::getAdapterFromManager($items, $adapter); + return $paginator; + } + /** * Change the adapter plugin manager * @@ -60,7 +96,7 @@ public static function factory($items, $adapter = null) */ public static function setAdapterPluginManager(AdapterPluginManager $adapters) { - self::$adapters = $adapters; + static::$adapters = $adapters; } /** @@ -70,9 +106,9 @@ public static function setAdapterPluginManager(AdapterPluginManager $adapters) */ public static function getAdapterPluginManager() { - if (self::$adapters === null) { - self::$adapters = new AdapterPluginManager(); + if (static::$adapters === null) { + static::$adapters = new AdapterPluginManager(); } - return self::$adapters; + return static::$adapters; } } From 0e3a7622fe104875e0faeebbd2a0aa3743304e93 Mon Sep 17 00:00:00 2001 From: blanchonvincent Date: Thu, 18 Oct 2012 21:40:03 +0200 Subject: [PATCH 12/12] Create factory for dbselect --- src/Adapter/Service/DbSelectFactory.php | 46 +++++++++++++++++++++++++ src/AdapterPluginManager.php | 37 +++++++++----------- src/ScrollingStylePluginManager.php | 1 - 3 files changed, 63 insertions(+), 21 deletions(-) create mode 100644 src/Adapter/Service/DbSelectFactory.php diff --git a/src/Adapter/Service/DbSelectFactory.php b/src/Adapter/Service/DbSelectFactory.php new file mode 100644 index 0000000..b5e8883 --- /dev/null +++ b/src/Adapter/Service/DbSelectFactory.php @@ -0,0 +1,46 @@ +creationOptions = $creationOptions; + } + + /** + * @param ServiceLocatorInterface $serviceLocator + * @return \Zend\Navigation\Navigation + */ + public function createService(ServiceLocatorInterface $serviceLocator) + { + $class = new \ReflectionClass('Zend\Paginator\Adapter\DbSelect'); + return $class->newInstanceArgs($this->creationOptions); + } +} diff --git a/src/AdapterPluginManager.php b/src/AdapterPluginManager.php index a2b6acc..6e82427 100644 --- a/src/AdapterPluginManager.php +++ b/src/AdapterPluginManager.php @@ -31,38 +31,35 @@ class AdapterPluginManager extends AbstractPluginManager */ protected $invokableClasses = array( 'array' => 'Zend\Paginator\Adapter\ArrayAdapter', - 'dbselect' => 'Zend\Paginator\Adapter\DbSelect', 'iterator' => 'Zend\Paginator\Adapter\Iterator', 'null' => 'Zend\Paginator\Adapter\Null', ); /** - * Attempt to create an instance via an invokable class + * Default set of adapter factories * - * Overrides parent implementation by passing $creationOptions to the - * constructor, if non-null. + * @var array + */ + protected $factories = array( + 'dbselect' => 'Zend\Paginator\Adapter\Service\DbSelectFactory' + ); + + /** + * Attempt to create an instance via a factory * * @param string $canonicalName * @param string $requestedName - * @return null|\stdClass - * @throws Exception\ServiceNotCreatedException If resolved class does not exist + * @return mixed + * @throws Exception\ServiceNotCreatedException If factory is not callable */ - protected function createFromInvokable($canonicalName, $requestedName) + protected function createFromFactory($canonicalName, $requestedName) { - $invokable = $this->invokableClasses[$canonicalName]; - - if (null === $this->creationOptions - || (is_array($this->creationOptions) && empty($this->creationOptions)) - ) { - return new $invokable(); - } else { - if($canonicalName == "dbselect" && is_array($this->creationOptions)) { - $class = new \ReflectionClass($invokable); - return $class->newInstanceArgs($this->creationOptions); - } else { - return new $invokable($this->creationOptions); - } + $factory = $this->factories[$canonicalName]; + if (is_string($factory) && class_exists($factory, true)) { + $factory = new $factory($this->creationOptions); + $this->factories[$canonicalName] = $factory; } + return parent::createFromFactory($canonicalName, $requestedName); } /** diff --git a/src/ScrollingStylePluginManager.php b/src/ScrollingStylePluginManager.php index 18a4e0f..e6d9c72 100644 --- a/src/ScrollingStylePluginManager.php +++ b/src/ScrollingStylePluginManager.php @@ -59,4 +59,3 @@ public function validatePlugin($plugin) )); } } -