diff --git a/composer.json b/composer.json index f8d12bd..1823888 100644 --- a/composer.json +++ b/composer.json @@ -27,8 +27,8 @@ }, "require": { "php": "^5.3", - "eldnp/export": "^0.1.2", - "ext-pdo": "*" + "ext-pdo": "*", + "eldnp/export.map": "^0.2.1" }, "require-dev": { "phpunit/PHPUnit": "^4.8", diff --git a/lib/PaginatedDataSource.php b/lib/PaginatedDataSource.php index ecfc9ec..754f614 100644 --- a/lib/PaginatedDataSource.php +++ b/lib/PaginatedDataSource.php @@ -24,14 +24,14 @@ use Eldnp\Export\DataSource\Pdo\Exception\LogicException; use Eldnp\Export\DataSource\Pdo\Exception\RuntimeException; -use Eldnp\Export\DataSourceInterface; +use Eldnp\Export\Map\AbstractMapDataSource; /** * Class PaginatedDataSource * * @package Eldnp\Export\DataSource\Pdo */ -class PaginatedDataSource implements DataSourceInterface +class PaginatedDataSource extends AbstractMapDataSource { /** * @var \PDO @@ -58,13 +58,6 @@ class PaginatedDataSource implements DataSourceInterface */ private $batchSize; - /** - * @see constants \PDO::FETCH_... - * - * @var int|null - */ - private $fetchStyle; - /** * @var \PDOStatement */ @@ -98,16 +91,14 @@ class PaginatedDataSource implements DataSourceInterface * @param string $leftPlaceholder * @param string $rightPlaceholder * @param int $batchSize - * @param int|null $fetchStyle */ - public function __construct(\PDO $pdo, $query, $leftPlaceholder, $rightPlaceholder, $batchSize, $fetchStyle = null) + public function __construct(\PDO $pdo, $query, $leftPlaceholder, $rightPlaceholder, $batchSize) { $this->pdo = $pdo; $this->query = $query; $this->leftPlaceholder = $leftPlaceholder; $this->rightPlaceholder = $rightPlaceholder; $this->setBatchSize($batchSize); - $this->fetchStyle = $fetchStyle; } /** @@ -169,10 +160,7 @@ private function buildNextStatement() return $statement; } - /** - * @inheritdoc - */ - public function current() + protected function currentMap() { return $this->currentRawData; } @@ -183,7 +171,7 @@ public function current() public function next() { $allowNextStatement = true; - while (false === $this->currentRawData = $this->currentStatement->fetch($this->fetchStyle)) { + while (false === $this->currentRawData = $this->currentStatement->fetch(\PDO::FETCH_ASSOC)) { if (!$allowNextStatement) { return; } diff --git a/lib/PdoStatementDataSource.php b/lib/StatementDataSource.php similarity index 83% rename from lib/PdoStatementDataSource.php rename to lib/StatementDataSource.php index f34305a..e19059c 100644 --- a/lib/PdoStatementDataSource.php +++ b/lib/StatementDataSource.php @@ -23,27 +23,20 @@ namespace Eldnp\Export\DataSource\Pdo; use Eldnp\Export\DataSource\Pdo\Exception\LogicException; -use Eldnp\Export\DataSourceInterface; +use Eldnp\Export\Map\AbstractMapDataSource; /** * Class PdoStatementDataSource * * @package Eldnp\Export\DataSource\Pdo */ -class PdoStatementDataSource implements DataSourceInterface +class StatementDataSource extends AbstractMapDataSource { /** * @var \PDOStatement */ private $statement; - /** - * @see constants \PDO::FETCH_... - * - * @var int|null - */ - private $fetchStyle; - /** * @var mixed|false */ @@ -63,15 +56,16 @@ class PdoStatementDataSource implements DataSourceInterface * PdoStatementDataSource constructor. * * @param \PDOStatement $statement - * @param int|null $fetchStyle */ - public function __construct(\PDOStatement $statement, $fetchStyle = null) + public function __construct(\PDOStatement $statement) { $this->statement = $statement; - $this->fetchStyle = $fetchStyle; } - public function current() + /** + * @inheritdoc + */ + public function currentMap() { return $this->rawData; } @@ -81,7 +75,7 @@ public function current() */ public function next() { - $this->rawData = $this->statement->fetch($this->fetchStyle); + $this->rawData = $this->statement->fetch(\PDO::FETCH_ASSOC); } /** diff --git a/test/FixturePdoFactory.php b/test/FixturePdoFactory.php new file mode 100644 index 0000000..e8473fa --- /dev/null +++ b/test/FixturePdoFactory.php @@ -0,0 +1,91 @@ +. + * + * @see https://github.com/eldnp/export.data-source.pdo for the canonical source repository + * @copyright Copyright (c) 2017 Oleg Verevskoy + * @license https://github.com/eldnp/export.data-source.pdo/blob/master/LICENSE GNU GENERAL PUBLIC LICENSE Version 3 + */ + +namespace EldnpTest\Export\DataSource\Pdo; + +/** + * Class FixturePdoFactory + * + * @package EldnpTest\Export\DataSource\Pdo + */ +class FixturePdoFactory +{ + /** + * @param \PDO $pdo + * @param string $query + * + * @throws \RuntimeException + */ + private static function executeQuery(\PDO $pdo, $query) + { + if (false === $pdo->exec($query)) { + throw new \RuntimeException(sprintf( + 'An error occurred while running the request. Error: %s. Query: %s', + implode(', ', $pdo->errorInfo()), + $query + )); + } + } + + /** + * @param \PDO $pdo + */ + private static function createScheme(\PDO $pdo) + { + self::executeQuery($pdo, <<< SQL +create table awesome_table +( + id INT not null primary key, + field_one INT not null, + field_two INT +) +SQL + ); + } + + /** + * @param \PDO $pdo + * @param int $numRows + */ + private static function populateAwesomeTable(\PDO $pdo, $numRows) + { + for ($i = 1; $i <= $numRows; $i++) { + $query = 'insert into awesome_table (id, field_one, field_two) values ' + . sprintf('(%d, %d, %s)', $i, rand(0, $numRows), rand(0, 10) > 3 ? rand(0, $numRows) : 'null') + ; + self::executeQuery($pdo, $query); + } + } + + /** + * @param int $numRows + * + * @return \PDO + */ + public static function factory($numRows) + { + $pdo = new \PDO("sqlite::memory:"); + self::createScheme($pdo); + self::populateAwesomeTable($pdo, $numRows); + return $pdo; + } +} \ No newline at end of file diff --git a/test/Generator.php b/test/Generator.php deleted file mode 100644 index a2234b5..0000000 --- a/test/Generator.php +++ /dev/null @@ -1,54 +0,0 @@ -. - * - * @see https://github.com/eldnp/export.data-source.pdo for the canonical source repository - * @copyright Copyright (c) 2017 Oleg Verevskoy - * @license https://github.com/eldnp/export.data-source.pdo/blob/master/LICENSE GNU GENERAL PUBLIC LICENSE Version 3 - */ - -namespace EldnpTest\Export\DataSource\Pdo; - -/** - * Class Generator - * - * @package EldnpTest\Export\DataSource\Pdo - */ -class Generator -{ - /** - * @var int - */ - private $capacity; - - /** - * Generator constructor. - * @param int $capacity - */ - public function __construct($capacity) - { - $this->capacity = $capacity; - } - - public function __invoke() - { - if ($index = $this->capacity--) { - return array("key{$index}" => "value{$index}"); - } - - return false; - } -} diff --git a/test/PaginatedDataSourceTest.php b/test/PaginatedDataSourceTest.php index f2a28b7..b815d03 100644 --- a/test/PaginatedDataSourceTest.php +++ b/test/PaginatedDataSourceTest.php @@ -41,42 +41,9 @@ class PaginatedDataSourceTest extends TestCase */ private $pdo; - /** - * @param string $query - * - * @throws \RuntimeException - */ - private function executeQuery($query) - { - if (false === $this->pdo->exec($query)) { - throw new \RuntimeException(sprintf( - 'An error occurred while running the request. Error: %s. Query: %s', - implode(', ', $this->pdo->errorInfo()), - $query - )); - } - } - protected function setUp() { - parent::setUp(); - $this->pdo = new \PDO("sqlite::memory:"); - $this->executeQuery(<<< SQL -create table awesome_table -( - id INT not null primary key, - field_one INT not null, - field_two INT -) -SQL - ); - $maxRows = self::TEST_ROWS_COUNT; - for ($i = 1; $i <= $maxRows; $i++) { - $this->executeQuery( - 'insert into awesome_table (id, field_one, field_two) values ' . - sprintf('(%d, %d, %s)', $i, rand(0, $maxRows), rand(0, 10) > 3 ? rand(0, $maxRows) : 'null') - ); - } + $this->pdo = FixturePdoFactory::factory(self::TEST_ROWS_COUNT); } private function buildDataSource( diff --git a/test/PdoStatementDataSourceTest.php b/test/StatementDataSourceTest.php similarity index 65% rename from test/PdoStatementDataSourceTest.php rename to test/StatementDataSourceTest.php index d5d4837..89b8fea 100644 --- a/test/PdoStatementDataSourceTest.php +++ b/test/StatementDataSourceTest.php @@ -23,41 +23,22 @@ namespace EldnpTest\Export\DataSource\Pdo; use Eldnp\Export\DataSource\Pdo\Exception\LogicException; -use Eldnp\Export\DataSource\Pdo\PdoStatementDataSource; +use Eldnp\Export\DataSource\Pdo\StatementDataSource; use PHPUnit\Framework\TestCase; -use Prophecy\Argument; /** * Class PdoStatementDataSourceTest * * @package EldnpTest\Export\DataSource\Pdo */ -class PdoStatementDataSourceTest extends TestCase +class StatementDataSourceTest extends TestCase { - /** - * @param int $elementsCount - * @return \PDOStatement - */ - private function createStatement($elementsCount) - { - $prophecy = $this->prophesize('\PDOStatement'); - $prophecy - ->__call('fetch', array(Argument::any())) - ->will(new Generator($elementsCount)) - ->shouldBeCalled() - ; - /** @var \PDOStatement $statement */ - $statement = $prophecy->reveal(); - return $statement; - } - public function currentDataProvider() { + $query = 'select * from awesome_table'; return array( - array($this->createStatement(0), 0), - array($this->createStatement(1), 1), - array($this->createStatement(2), 2), - array($this->createStatement(3), 3), + array(FixturePdoFactory::factory(0)->query($query), 0), + array(FixturePdoFactory::factory(10)->query($query), 10), ); } @@ -69,7 +50,7 @@ public function currentDataProvider() */ public function testCurrent(\PDOStatement $statement, $expectedElementsCount) { - $dataSource = new PdoStatementDataSource($statement); + $dataSource = new StatementDataSource($statement); $objectsCounter = 0; foreach ($dataSource as $key => $value) { $objectsCounter++; @@ -82,11 +63,8 @@ public function testCurrent(\PDOStatement $statement, $expectedElementsCount) */ public function testCurrentExceptionIfRewind() { - $dataSource = new PdoStatementDataSource($this->createStatement(0)); + $dataSource = new StatementDataSource(FixturePdoFactory::factory(0)->query('select 1 where 1 != 1')); $dataSource->rewind(); - while ($dataSource->valid()) { - $dataSource->next(); - } $dataSource->rewind(); } }