From 6250b9911878e6e66ab9e264876f3cd333a438af Mon Sep 17 00:00:00 2001 From: Jarret Byrne Date: Sun, 30 Jul 2017 20:34:49 -0400 Subject: [PATCH 1/8] Add commitTransaction() method to Wye class --- src/Wye.php | 22 ++++++++++++++++ tests/Wye/CommitTransactionTest.php | 41 +++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 tests/Wye/CommitTransactionTest.php diff --git a/src/Wye.php b/src/Wye.php index 3ffaa1c..9254526 100644 --- a/src/Wye.php +++ b/src/Wye.php @@ -229,6 +229,28 @@ public static function beginTransaction() } + /** + * Marks the current transaction as committed and removes Wye from + * transaction mode. If no transaction exists, throws a PDOException. + * + * @todo Flesh out the details for the PDOException properly. + * + * @throws PDOException + * + * @return void + */ + public static function commitTransaction() + { + if (static::getInTransaction() !== true) { + throw new PDOException(); + } + + static::setInTransaction(false); + + static::currentTransaction()->setCommitted(true); + } + + //************************************************************************** // STATEMENTS diff --git a/tests/Wye/CommitTransactionTest.php b/tests/Wye/CommitTransactionTest.php new file mode 100644 index 0000000..c249ebd --- /dev/null +++ b/tests/Wye/CommitTransactionTest.php @@ -0,0 +1,41 @@ +setExpectedException(PDOException::class); + Wye::commitTransaction(); + } + + public function testMarksTransactionCommitted() + { + Wye::beginTransaction(); + + $transaction = Wye::currentTransaction(); + + $this->assertFalse($transaction->getCommitted()); + + Wye::commitTransaction(); + + $this->assertTrue($transaction->getCommitted()); + } + + public function testSetsInTransactionToFalse() + { + $this->assertFalse(Wye::getInTransaction()); + + Wye::beginTransaction(); + + $this->assertTrue(Wye::getInTransaction()); + + Wye::commitTransaction(); + + $this->assertFalse(Wye::getInTransaction()); + } +} From 9b1a080d8933374674c1c5e9ddd1b2623f61ae44 Mon Sep 17 00:00:00 2001 From: Jarret Byrne Date: Sun, 30 Jul 2017 20:35:11 -0400 Subject: [PATCH 2/8] Add commit() method of the PDO class --- src/PDO/PDO.php | 22 ++++++++++++++- tests/PDO/PDO/CommitTest.php | 55 ++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 tests/PDO/PDO/CommitTest.php diff --git a/src/PDO/PDO.php b/src/PDO/PDO.php index c9d0fb2..54c3df0 100644 --- a/src/PDO/PDO.php +++ b/src/PDO/PDO.php @@ -10,7 +10,7 @@ /** * @todo - * - Implement the `commit` method + * - Finish the `commit` method * - Implement the `errorCode` method * - Implement the `errorInfo` method * - Implement the `exec` method @@ -53,6 +53,21 @@ public function beginTransaction() return true; } + /** + * Mimic for PDO::commit(). Commits the currently active transaction, or + * throws a PDOException if no transaction exists. + * + * @todo Allow false to be returned. + * + * @return true + */ + public function commit() + { + $this->getWye()->commitTransaction(); + + return true; + } + /** * Mimic for PDO::lastInsertId(). Returns the last insert ID value for the * last used statement result. @@ -108,4 +123,9 @@ public function quote($string, $paramtype = null) return $this->wye()->quote($string); } + + public function rollBack() + { + + } } diff --git a/tests/PDO/PDO/CommitTest.php b/tests/PDO/PDO/CommitTest.php new file mode 100644 index 0000000..2f75db3 --- /dev/null +++ b/tests/PDO/PDO/CommitTest.php @@ -0,0 +1,55 @@ +setExpectedException(PDOException::class); + + Wye::makePDO()->commit(); + } + + public function testMarksLastTransactionCommitted() + { + $pdo = Wye::makePDO(); + + $pdo->beginTransaction(); + + $transaction = Wye::currentTransaction(); + + $this->assertFalse($transaction->getCommitted()); + + $pdo->commit(); + + $this->assertTrue($transaction->getCommitted()); + } + + public function testReturnsTrue() + { + $pdo = Wye::makePDO(); + + $pdo->beginTransaction(); + + $this->assertTrue($pdo->commit()); + } + + public function testMarksWyeNoLongerInTransaction() + { + $pdo = Wye::makePDO(); + + $this->assertFalse(Wye::getInTransaction()); + + $pdo->beginTransaction(); + + $this->assertTrue(Wye::getInTransaction()); + + $pdo->commit(); + + $this->assertFalse(Wye::getInTransaction()); + } +} From 6020af3d8fc8d10080390ca037b663f075612590 Mon Sep 17 00:00:00 2001 From: Jarret Byrne Date: Sun, 30 Jul 2017 20:45:34 -0400 Subject: [PATCH 3/8] Fix documentation in Transaction class --- src/Transaction.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Transaction.php b/src/Transaction.php index d23329e..34038a2 100644 --- a/src/Transaction.php +++ b/src/Transaction.php @@ -12,7 +12,7 @@ class Transaction protected $index; /** - * @var committed + * @var boolean */ protected $committed = false; From 802d1dff2af8bfbdb88baa25ed4d1c2b608392a0 Mon Sep 17 00:00:00 2001 From: Jarret Byrne Date: Sun, 30 Jul 2017 20:45:52 -0400 Subject: [PATCH 4/8] Add rolled_back property to Transaction class --- src/Transaction.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Transaction.php b/src/Transaction.php index 34038a2..8b36e17 100644 --- a/src/Transaction.php +++ b/src/Transaction.php @@ -16,6 +16,11 @@ class Transaction */ protected $committed = false; + /** + * @var boolean + */ + protected $rolled_back = false; + /** * @var array */ From e8840b00c8f128f384a4f43146d39cfebb5fadff Mon Sep 17 00:00:00 2001 From: Jarret Byrne Date: Sun, 30 Jul 2017 20:46:31 -0400 Subject: [PATCH 5/8] Add rolled_back getter/setter to Transaction --- src/Transaction.php | 25 +++++++++++++++++++++++++ tests/Transaction/GetRolledBackTest.php | 19 +++++++++++++++++++ tests/Transaction/SetRolledBackTest.php | 23 +++++++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 tests/Transaction/GetRolledBackTest.php create mode 100644 tests/Transaction/SetRolledBackTest.php diff --git a/src/Transaction.php b/src/Transaction.php index 8b36e17..eddd057 100644 --- a/src/Transaction.php +++ b/src/Transaction.php @@ -84,6 +84,31 @@ public function setCommitted($committed) } + //************************************************************************** + // ROLLED_BACK + //************************************************************************** + + /** + * Returns the value of the rolled_back property. + * + * @return boolean + */ + public function getRolledBack() + { + return $this->rolled_back; + } + + /** + * Sets the value of the rolled_back property. + * + * @param boolean $rolled_back + */ + public function setRolledBack($rolled_back) + { + $this->rolled_back = (bool) $rolled_back; + } + + //************************************************************************** // STATEMENTS diff --git a/tests/Transaction/GetRolledBackTest.php b/tests/Transaction/GetRolledBackTest.php new file mode 100644 index 0000000..4e6e875 --- /dev/null +++ b/tests/Transaction/GetRolledBackTest.php @@ -0,0 +1,19 @@ +assertFalse($transaction->getRolledBack()); + + $transaction->setRolledBack(true); + + $this->assertTrue($transaction->getRolledBack()); + } +} diff --git a/tests/Transaction/SetRolledBackTest.php b/tests/Transaction/SetRolledBackTest.php new file mode 100644 index 0000000..eb87935 --- /dev/null +++ b/tests/Transaction/SetRolledBackTest.php @@ -0,0 +1,23 @@ +assertFalse($transaction->getRolledBack()); + + $transaction->setRolledBack(true); + + $this->assertTrue($transaction->getRolledBack()); + + $transaction->setRolledBack(false); + + $this->assertFalse($transaction->getRolledBack()); + } +} From 421d7554c434b58b417bfe668a119bdfbccef506 Mon Sep 17 00:00:00 2001 From: Jarret Byrne Date: Sun, 30 Jul 2017 20:52:42 -0400 Subject: [PATCH 6/8] Add rollBackTransaction() method to Wye class --- src/Wye.php | 21 ++++++++++++++ tests/Wye/RollBackTransactionTest.php | 41 +++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 tests/Wye/RollBackTransactionTest.php diff --git a/src/Wye.php b/src/Wye.php index 9254526..e58390a 100644 --- a/src/Wye.php +++ b/src/Wye.php @@ -250,6 +250,27 @@ public static function commitTransaction() static::currentTransaction()->setCommitted(true); } + /** + * Marks the current transaction as rolled back and removes Wye from + * transaction mode. If no transaction exists, throws a PDOException. + * + * @todo Flesh out the details for the PDOException properly. + * + * @throws PDOException + * + * @return void + */ + public static function rollBackTransaction() + { + if (static::getInTransaction() !== true) { + throw new PDOException(); + } + + static::setInTransaction(false); + + static::currentTransaction()->setRolledBack(true); + } + //************************************************************************** diff --git a/tests/Wye/RollBackTransactionTest.php b/tests/Wye/RollBackTransactionTest.php new file mode 100644 index 0000000..52117a1 --- /dev/null +++ b/tests/Wye/RollBackTransactionTest.php @@ -0,0 +1,41 @@ +setExpectedException(PDOException::class); + Wye::rollBackTransaction(); + } + + public function testMarksTransactionRolledBack() + { + Wye::beginTransaction(); + + $transaction = Wye::currentTransaction(); + + $this->assertFalse($transaction->getRolledBack()); + + Wye::rollBackTransaction(); + + $this->assertTrue($transaction->getRolledBack()); + } + + public function testSetsInTransactionToFalse() + { + $this->assertFalse(Wye::getInTransaction()); + + Wye::beginTransaction(); + + $this->assertTrue(Wye::getInTransaction()); + + Wye::rollBackTransaction(); + + $this->assertFalse(Wye::getInTransaction()); + } +} From 7b8d5a943d228054a89a6ad02e5bd5afeaf7b353 Mon Sep 17 00:00:00 2001 From: Jarret Byrne Date: Sun, 30 Jul 2017 20:57:07 -0400 Subject: [PATCH 7/8] Add rollBack() method to PDO class --- src/PDO/PDO.php | 12 +++++++- tests/PDO/PDO/RollBackTest.php | 55 ++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 tests/PDO/PDO/RollBackTest.php diff --git a/src/PDO/PDO.php b/src/PDO/PDO.php index 54c3df0..ddaaf36 100644 --- a/src/PDO/PDO.php +++ b/src/PDO/PDO.php @@ -19,7 +19,7 @@ * - Implement the `inTransaction` method * - Finish the `lastInsertId` method * - Implement the `query` method - * - Implement the `rollBack` method + * - Finish the `rollBack` method * - Implement the `setAttribute` method */ class PDO extends BasePDO @@ -124,8 +124,18 @@ public function quote($string, $paramtype = null) return $this->wye()->quote($string); } + /** + * Mimic for PDO::rollBack(). Rolls back the currently active transaction, + * or throws a PDOException if no transaction exists. + * + * @todo Allow false to be returned. + * + * @return true + */ public function rollBack() { + $this->getWye()->rollBackTransaction(); + return true; } } diff --git a/tests/PDO/PDO/RollBackTest.php b/tests/PDO/PDO/RollBackTest.php new file mode 100644 index 0000000..9b8cbe2 --- /dev/null +++ b/tests/PDO/PDO/RollBackTest.php @@ -0,0 +1,55 @@ +setExpectedException(PDOException::class); + + Wye::makePDO()->rollBack(); + } + + public function testMarksLastTransactionRolledBack() + { + $pdo = Wye::makePDO(); + + $pdo->beginTransaction(); + + $transaction = Wye::currentTransaction(); + + $this->assertFalse($transaction->getRolledBack()); + + $pdo->rollBack(); + + $this->assertTrue($transaction->getRolledBack()); + } + + public function testReturnsTrue() + { + $pdo = Wye::makePDO(); + + $pdo->beginTransaction(); + + $this->assertTrue($pdo->rollBack()); + } + + public function testMarksWyeNoLongerInTransaction() + { + $pdo = Wye::makePDO(); + + $this->assertFalse(Wye::getInTransaction()); + + $pdo->beginTransaction(); + + $this->assertTrue(Wye::getInTransaction()); + + $pdo->rollBack(); + + $this->assertFalse(Wye::getInTransaction()); + } +} From 6a4d208d92f7d7e671c303e06f62a5c9dee0c892 Mon Sep 17 00:00:00 2001 From: Jarret Byrne Date: Sun, 30 Jul 2017 21:03:54 -0400 Subject: [PATCH 8/8] Update CHANGELOG.md --- CHANGELOG.md | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b6c8e64..9fa6458 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] -- No pending changes +### Added +- Mock for the `PDO::commit()` method +- Mock for the `PDO::rollBack()` method +- `rolled_back` property to `Stratedge\Wye\Transaction` +- `getRolledBack()` method to `Stratedge\Wye\Transaction` +- `setRolledBack()` method to `Stratedge\Wye\Transaction` ## [0.3.0] - 2017-05-23 ### Added @@ -17,9 +22,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Changed - Move documentation from README.md to Github wiki - [https://github.com/stratedge/wye/wiki](https://github.com/stratedge/wye/wiki) - Remove the `params` property from `Stratedge\Wye\PDO\PDOStatement` -- Remove `params` method from `Stratedge\Wye\PDO\PDOStatement` -- Remove `getParams` method from `Stratedge\Wye\PDO\PDOStatement` -- Remove `setParams` method from `Stratedge\Wye\PDO\PDOStatement` +- Remove `params()` method from `Stratedge\Wye\PDO\PDOStatement` +- Remove `getParams()` method from `Stratedge\Wye\PDO\PDOStatement` +- Remove `setParams()` method from `Stratedge\Wye\PDO\PDOStatement` - Drop support for HHVM <= 3.6 ## [0.2.0] - 2017-05-02