From 277c8e3f457e900a8adfe6be683d82fe994f5bdf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Kucha=C5=99?= <honza.kuchar@grifart.cz>
Date: Fri, 6 May 2016 21:00:41 +0200
Subject: [PATCH] tests: testing logic of interface moved into
 CursorInterfaceTest

---
 .../PostgresDriver/CursorInterfaceTest.php    | 271 ++++++++++++++++++
 .../Store/PostgresDriver/FastCursorTest.phpt  | 269 +----------------
 2 files changed, 273 insertions(+), 267 deletions(-)
 create mode 100644 tests/Store/PostgresDriver/CursorInterfaceTest.php

diff --git a/tests/Store/PostgresDriver/CursorInterfaceTest.php b/tests/Store/PostgresDriver/CursorInterfaceTest.php
new file mode 100644
index 0000000..4e83ebe
--- /dev/null
+++ b/tests/Store/PostgresDriver/CursorInterfaceTest.php
@@ -0,0 +1,271 @@
+<?php
+/**
+ * @testCase
+ */
+
+namespace Grifart\Mappi\Tests\Store\Store\PostgresDriver;
+
+use Grifart\Mappi\Store\PostgresDriver\CursorException;
+use Grifart\Mappi\Store\PostgresDriver\ICursor;
+use Grifart\Mappi\Tests\Store\BaseTest;
+use Tester\Assert;
+
+abstract class CursorInterfaceTest extends BaseTest
+{
+	/** @var ICursor */
+	protected $uut;
+
+	// initial position
+	public function test_givenCursor_whenGetCurrentRow_thenMustFail()
+	{
+		// must fail because initial position is 0
+		Assert::exception(function() {
+			$this->uut->fetchCurrentSingle();
+		}, CursorException::class);
+	}
+
+	public function test_givenInitialPosition_whenFetch_thenGetFirstRow()
+	{
+		Assert::same(1, $this->uut->fetchNextSingle());
+	}
+
+	// fetch( x == 0 )
+	public function test_givenSecondPosition_whenFetch0_thenGetCurrentRow()
+	{
+		// head:           .
+		// index: 0  1  2  3  4
+		// value:    1  2  3  4
+		$this->uut->moveTo(3);
+		Assert::same(3, $this->uut->fetchCurrentSingle());
+
+		$result = $this->uut->fetch(0); // rows backwards + read current
+
+		Assert::count(1, $result);
+		Assert::same(3, current($result[0]->toArray()));
+	}
+
+	// fetch( x > 0 )
+	public function test_givenSecondPosition_whenFetchForward_thenGetRowsAfterCursor()
+	{
+		// head:           .
+		// index: 0  1  2  3  4
+		// value:    1  2  3  4
+		$this->uut->moveTo(3);
+		Assert::same(3, $this->uut->fetchCurrentSingle());
+
+		// head:                 .
+		// index: 0  1  2  3  4  5  6
+		// value:    1  2  3  4  5  6
+		$result = $this->uut->fetch(2); // rows backwards + read current
+		// todo: split into fetch decorator for those special fetch* methods
+
+		Assert::count(2, $result);
+		Assert::same(4, current($result[0]->toArray()));
+		Assert::same(5, current($result[1]->toArray()));
+		Assert::same(5, $this->uut->fetchCurrentSingle());
+	}
+
+	// fetch( x < 0 )
+	public function test_givenSecondPosition_whenFetchBackwards_thenGetRowsBeforeCursor()
+	{
+		// head:           .
+		// index: 0  1  2  3  4
+		// value:    1  2  3  4
+		$this->uut->moveTo(3);
+		Assert::same(3, $this->uut->fetchCurrentSingle());
+
+		// head:     .
+		// index: 0  1  2  3  4
+		// value:    1  2  3  4
+		$result = $this->uut->fetch(-2); // rows backwards + read current
+		// todo: split into fetch decorator for those special fetch* methods
+
+		Assert::count(2, $result);
+		Assert::same(2, current($result[0]->toArray()));
+		Assert::same(1, current($result[1]->toArray()));
+		Assert::same(1, $this->uut->fetchCurrentSingle());
+	}
+
+
+	// moveToBeginning()
+	public function test_givenEndPosition_whenMoveToTheBeginning_thenFetchingNextRowWillBeFirstRow()
+	{
+		// Arrange
+		$this->uut->moveToLast();
+
+		// Act
+		$this->uut->moveToBeginning();
+
+		//Assert
+		Assert::same(1, $this->uut->fetchNextSingle());
+	}
+
+
+	// moveToFirst()
+	public function test_givenEndPosition_whenMoveToTheFirst_thenFetchingCurrentWillBeFirstRow()
+	{
+		// Arrange
+		$this->uut->moveToLast();
+
+		// Act
+		$this->uut->moveToFirst();
+
+		//Assert
+		Assert::same(1, $this->uut->fetchCurrentSingle());
+	}
+
+
+	// moveToEnd()
+	public function test_givenInitialPosition_whenMoveToEnd_thenFetchingOneBacwardsWillBeTheLastRow()
+	{
+		$this->uut->moveToEnd();
+
+		Assert::exception(function() {
+			$this->uut->fetchCurrentSingle();
+		}, CursorException::class, CursorException::MESSAGE_NO_DATA_TO_FETCH);
+		$data = $this->uut->fetch(-1);
+		Assert::count(1, $data);
+		Assert::same(1000, current($data[0]->toArray()));
+	}
+
+
+	// moveToLast()
+	public function test_givenInitialPosition_whenMoveToLast_thenFetchingCurrentWillBeLastRow()
+	{
+		$this->uut->moveToLast();
+		Assert::same(1000, $this->uut->fetchCurrentSingle());
+		Assert::exception(function() {
+			$this->uut->fetchNextSingle();
+		}, CursorException::class, CursorException::MESSAGE_NO_DATA_TO_FETCH);
+	}
+
+
+	// moveBy()
+	public function test_givenInitialPosition_whenMoveBy2_thenGetSecondValue()
+	{
+		$this->uut->moveBy(2);
+		Assert::same(2, $this->uut->fetchCurrentSingle());
+	}
+
+	public function test_givenSecondPosition_whenMoveOneBack_thenGetFirstValue()
+	{
+		$this->uut->moveTo(2);
+		$this->uut->moveBy(-1);
+		Assert::same(1, $this->uut->fetchCurrentSingle());
+	}
+
+
+	// fetchOneAt()
+	public function test_givenInitialPosition_whenFetch5_thenGetFive()
+	{
+		$data = $this->uut->fetchOneAt(5);
+		Assert::same(5, current($data->toArray()));
+	}
+
+	public function test_givenNonInitialPosition_whenFetch5_thenGetFive()
+	{
+		$this->uut->moveTo(234); // wherever
+
+		$data = $this->uut->fetchOneAt(5);
+		Assert::same(5, current($data->toArray()));
+	}
+
+	// fetchRemaining()
+	public function test_given5BeforeEndPosition_whenFetchRemaining_thenGetLastFile()
+	{
+		$this->uut->moveTo(995);
+
+		$result = $this->uut->fetchRemaining();
+
+		Assert::count(5, $result);
+		Assert::same(996,  current($result[0]->toArray()));
+		Assert::same(997,  current($result[1]->toArray()));
+		Assert::same(998,  current($result[2]->toArray()));
+		Assert::same(999,  current($result[3]->toArray()));
+		Assert::same(1000, current($result[4]->toArray()));
+
+		Assert::exception(function() {
+			$this->uut->fetchCurrentSingle();
+		}, CursorException::class, CursorException::MESSAGE_NO_DATA_TO_FETCH);
+	}
+
+
+	// fetchRemaining()
+	public function test_given5AfterStart_whenFetchForegoing_thenGetFirstFive()
+	{
+		$this->uut->moveTo(6);
+
+		$result = $this->uut->fetchForegoing();
+
+		Assert::count(5, $result);
+		Assert::same(5,  current($result[0]->toArray()));
+		Assert::same(4,  current($result[1]->toArray()));
+		Assert::same(3,  current($result[2]->toArray()));
+		Assert::same(2,  current($result[3]->toArray()));
+		Assert::same(1, current($result[4]->toArray()));
+
+		Assert::exception(function() {
+			$this->uut->fetchCurrentSingle();
+		}, CursorException::class, CursorException::MESSAGE_NO_DATA_TO_FETCH);
+	}
+
+
+	// edge cases:
+	public function test_givenInitialPosition_whenMoveToLeft_thenGetError()
+	{
+		// start: index === 0
+		Assert::exception(function() {
+			$this->uut->moveBy(-1);
+		}, CursorException::class, CursorException::MESSAGE_OVERFLOW);
+
+		// index === 0
+		$data = $this->uut->fetch(-1);
+		Assert::count(0, $data);
+
+		$firstValue = $this->uut->fetchNextSingle();
+		Assert::equal(1, $firstValue);
+
+	}
+
+	public function test_givenEndPosition_whenMoveToRight_thenGetError()
+	{
+		$this->uut->moveToEnd();
+
+		Assert::exception(function() {
+			$this->uut->moveBy(1);
+		}, CursorException::class, CursorException::MESSAGE_OVERFLOW);
+	}
+
+	public function test_givenFirstPosition_whenMoveToLeft_thenGetError()
+	{
+		$this->uut->moveToFirst();
+
+		Assert::exception(function() {
+			$this->uut->moveBy(-1);
+		}, CursorException::class, CursorException::MESSAGE_OVERFLOW);
+
+		// todo: unfortunately moveBy moved cursor into index=0 even when exception occured
+		// todo: this should be supported or should not modify state
+
+		Assert::exception(function() {
+			$this->uut->fetchCurrentSingle();
+		}, CursorException::class, CursorException::MESSAGE_NO_DATA_TO_FETCH);
+	}
+	
+	public function test_givenLastPosition_whenMoveToRight_thenGetError()
+	{
+		$this->uut->moveToLast();
+
+		Assert::exception(function() {
+			$this->uut->moveBy(1);
+		}, CursorException::class, CursorException::MESSAGE_OVERFLOW);
+
+		// todo: unfortunately moveBy moved cursor into index=0 even when exception occured
+		// todo: this should be supported or should not modify state
+
+		Assert::exception(function() {
+			$this->uut->fetchCurrentSingle();
+		}, CursorException::class, CursorException::MESSAGE_NO_DATA_TO_FETCH);
+	}
+
+}
diff --git a/tests/Store/PostgresDriver/FastCursorTest.phpt b/tests/Store/PostgresDriver/FastCursorTest.phpt
index 7d6da26..ea8a9b6 100644
--- a/tests/Store/PostgresDriver/FastCursorTest.phpt
+++ b/tests/Store/PostgresDriver/FastCursorTest.phpt
@@ -5,23 +5,13 @@
 
 namespace Grifart\Mappi\Tests\Store\Store\PostgresDriver;
 
-use Grifart\Mappi\Store\PostgresDriver\BasicCursor;
-use Grifart\Mappi\Store\PostgresDriver\CursorException;
 use Grifart\Mappi\Store\PostgresDriver\FastCursor;
-use Grifart\Mappi\Tests\Store\BaseTest;
-use Tester\Assert;
-use Tester\Environment;
 
 require_once __DIR__ . "/../../bootstrap.php";
+require_once __DIR__ . "/CursorInterfaceTest.php";
 
-class FastCursorTest extends BaseTest
+class FastCursorTest extends CursorInterfaceTest
 {
-	/** @var FastCursor */
-	private $uut;
-
-	/**
-	 * @inheritDoc
-	 */
 	protected function setUp()
 	{
 		global $connection, $SQL_thousandRowsAscending;
@@ -38,261 +28,6 @@ class FastCursorTest extends BaseTest
 
 		parent::tearDown();
 	}
-
-
-	// initial position
-	public function test_givenCursor_whenGetCurrentRow_thenMustFail()
-	{
-		// must fail because initial position is 0
-		Assert::exception(function() {
-			$this->uut->fetchCurrentSingle();
-		}, CursorException::class);
-	}
-
-	public function test_givenInitialPosition_whenFetch_thenGetFirstRow()
-	{
-		Assert::same(1, $this->uut->fetchNextSingle());
-	}
-
-	// fetch( x == 0 )
-	public function test_givenSecondPosition_whenFetch0_thenGetCurrentRow()
-	{
-		// head:           .
-		// index: 0  1  2  3  4
-		// value:    1  2  3  4
-		$this->uut->moveTo(3);
-		Assert::same(3, $this->uut->fetchCurrentSingle());
-
-		$result = $this->uut->fetch(0); // rows backwards + read current
-
-		Assert::count(1, $result);
-		Assert::same(3, current($result[0]->toArray()));
-	}
-
-	// fetch( x > 0 )
-	public function test_givenSecondPosition_whenFetchForward_thenGetRowsAfterCursor()
-	{
-		// head:           .
-		// index: 0  1  2  3  4
-		// value:    1  2  3  4
-		$this->uut->moveTo(3);
-		Assert::same(3, $this->uut->fetchCurrentSingle());
-
-		// head:                 .
-		// index: 0  1  2  3  4  5  6
-		// value:    1  2  3  4  5  6
-		$result = $this->uut->fetch(2); // rows backwards + read current
-		// todo: split into fetch decorator for those special fetch* methods
-
-		Assert::count(2, $result);
-		Assert::same(4, current($result[0]->toArray()));
-		Assert::same(5, current($result[1]->toArray()));
-		Assert::same(5, $this->uut->fetchCurrentSingle());
-	}
-
-	// fetch( x < 0 )
-	public function test_givenSecondPosition_whenFetchBackwards_thenGetRowsBeforeCursor()
-	{
-		// head:           .
-		// index: 0  1  2  3  4
-		// value:    1  2  3  4
-		$this->uut->moveTo(3);
-		Assert::same(3, $this->uut->fetchCurrentSingle());
-
-		// head:     .
-		// index: 0  1  2  3  4
-		// value:    1  2  3  4
-		$result = $this->uut->fetch(-2); // rows backwards + read current
-		// todo: split into fetch decorator for those special fetch* methods
-
-		Assert::count(2, $result);
-		Assert::same(2, current($result[0]->toArray()));
-		Assert::same(1, current($result[1]->toArray()));
-		Assert::same(1, $this->uut->fetchCurrentSingle());
-	}
-
-
-	// moveToBeginning()
-	public function test_givenEndPosition_whenMoveToTheBeginning_thenFetchingNextRowWillBeFirstRow()
-	{
-		// Arrange
-		$this->uut->moveToLast();
-
-		// Act
-		$this->uut->moveToBeginning();
-
-		//Assert
-		Assert::same(1, $this->uut->fetchNextSingle());
-	}
-
-
-	// moveToFirst()
-	public function test_givenEndPosition_whenMoveToTheFirst_thenFetchingCurrentWillBeFirstRow()
-	{
-		// Arrange
-		$this->uut->moveToLast();
-
-		// Act
-		$this->uut->moveToFirst();
-
-		//Assert
-		Assert::same(1, $this->uut->fetchCurrentSingle());
-	}
-
-
-	// moveToEnd()
-	public function test_givenInitialPosition_whenMoveToEnd_thenFetchingOneBacwardsWillBeTheLastRow()
-	{
-		$this->uut->moveToEnd();
-
-		Assert::exception(function() {
-			$this->uut->fetchCurrentSingle();
-		}, CursorException::class, CursorException::MESSAGE_NO_DATA_TO_FETCH);
-		$data = $this->uut->fetch(-1);
-		Assert::count(1, $data);
-		Assert::same(1000, current($data[0]->toArray()));
-	}
-
-
-	// moveToLast()
-	public function test_givenInitialPosition_whenMoveToLast_thenFetchingCurrentWillBeLastRow()
-	{
-		$this->uut->moveToLast();
-		Assert::same(1000, $this->uut->fetchCurrentSingle());
-		Assert::exception(function() {
-			$this->uut->fetchNextSingle();
-		}, CursorException::class, CursorException::MESSAGE_NO_DATA_TO_FETCH);
-	}
-
-
-	// moveBy()
-	public function test_givenInitialPosition_whenMoveBy2_thenGetSecondValue()
-	{
-		$this->uut->moveBy(2);
-		Assert::same(2, $this->uut->fetchCurrentSingle());
-	}
-
-	public function test_givenSecondPosition_whenMoveOneBack_thenGetFirstValue()
-	{
-		$this->uut->moveTo(2);
-		$this->uut->moveBy(-1);
-		Assert::same(1, $this->uut->fetchCurrentSingle());
-	}
-
-
-	// fetchOneAt()
-	public function test_givenInitialPosition_whenFetch5_thenGetFive()
-	{
-		$data = $this->uut->fetchOneAt(5);
-		Assert::same(5, current($data->toArray()));
-	}
-
-	public function test_givenNonInitialPosition_whenFetch5_thenGetFive()
-	{
-		$this->uut->moveTo(234); // wherever
-
-		$data = $this->uut->fetchOneAt(5);
-		Assert::same(5, current($data->toArray()));
-	}
-
-	// fetchRemaining()
-	public function test_given5BeforeEndPosition_whenFetchRemaining_thenGetLastFile()
-	{
-		$this->uut->moveTo(995);
-
-		$result = $this->uut->fetchRemaining();
-
-		Assert::count(5, $result);
-		Assert::same(996,  current($result[0]->toArray()));
-		Assert::same(997,  current($result[1]->toArray()));
-		Assert::same(998,  current($result[2]->toArray()));
-		Assert::same(999,  current($result[3]->toArray()));
-		Assert::same(1000, current($result[4]->toArray()));
-
-		Assert::exception(function() {
-			$this->uut->fetchCurrentSingle();
-		}, CursorException::class, CursorException::MESSAGE_NO_DATA_TO_FETCH);
-	}
-
-
-	// fetchRemaining()
-	public function test_given5AfterStart_whenFetchForegoing_thenGetFirstFive()
-	{
-		$this->uut->moveTo(6);
-
-		$result = $this->uut->fetchForegoing();
-
-		Assert::count(5, $result);
-		Assert::same(5,  current($result[0]->toArray()));
-		Assert::same(4,  current($result[1]->toArray()));
-		Assert::same(3,  current($result[2]->toArray()));
-		Assert::same(2,  current($result[3]->toArray()));
-		Assert::same(1, current($result[4]->toArray()));
-
-		Assert::exception(function() {
-			$this->uut->fetchCurrentSingle();
-		}, CursorException::class, CursorException::MESSAGE_NO_DATA_TO_FETCH);
-	}
-
-
-	// edge cases:
-	public function test_givenInitialPosition_whenMoveToLeft_thenGetError()
-	{
-		// start: index === 0
-		Assert::exception(function() {
-			$this->uut->moveBy(-1);
-		}, CursorException::class, CursorException::MESSAGE_OVERFLOW);
-
-		// index === 0
-		$data = $this->uut->fetch(-1);
-		Assert::count(0, $data);
-
-		$firstValue = $this->uut->fetchNextSingle();
-		Assert::equal(1, $firstValue);
-
-	}
-
-	public function test_givenEndPosition_whenMoveToRight_thenGetError()
-	{
-		$this->uut->moveToEnd();
-
-		Assert::exception(function() {
-			$this->uut->moveBy(1);
-		}, CursorException::class, CursorException::MESSAGE_OVERFLOW);
-	}
-
-	public function test_givenFirstPosition_whenMoveToLeft_thenGetError()
-	{
-		$this->uut->moveToFirst();
-
-		Assert::exception(function() {
-			$this->uut->moveBy(-1);
-		}, CursorException::class, CursorException::MESSAGE_OVERFLOW);
-
-		// todo: unfortunately moveBy moved cursor into index=0 even when exception occured
-		// todo: this should be supported or should not modify state
-
-		Assert::exception(function() {
-			$this->uut->fetchCurrentSingle();
-		}, CursorException::class, CursorException::MESSAGE_NO_DATA_TO_FETCH);
-	}
-	
-	public function test_givenLastPosition_whenMoveToRight_thenGetError()
-	{
-		$this->uut->moveToLast();
-
-		Assert::exception(function() {
-			$this->uut->moveBy(1);
-		}, CursorException::class, CursorException::MESSAGE_OVERFLOW);
-
-		// todo: unfortunately moveBy moved cursor into index=0 even when exception occured
-		// todo: this should be supported or should not modify state
-
-		Assert::exception(function() {
-			$this->uut->fetchCurrentSingle();
-		}, CursorException::class, CursorException::MESSAGE_NO_DATA_TO_FETCH);
-	}
-
 }
 
 (new FastCursorTest())->run();
-- 
GitLab