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 diff --git a/src/PDO/PDO.php b/src/PDO/PDO.php index c9d0fb2..ddaaf36 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 @@ -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 @@ -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,19 @@ 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/src/Transaction.php b/src/Transaction.php index d23329e..eddd057 100644 --- a/src/Transaction.php +++ b/src/Transaction.php @@ -12,10 +12,15 @@ class Transaction protected $index; /** - * @var committed + * @var boolean */ protected $committed = false; + /** + * @var boolean + */ + protected $rolled_back = false; + /** * @var array */ @@ -79,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/src/Wye.php b/src/Wye.php index 3ffaa1c..e58390a 100644 --- a/src/Wye.php +++ b/src/Wye.php @@ -229,6 +229,49 @@ 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); + } + + /** + * 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); + } + + //************************************************************************** // STATEMENTS 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()); + } +} 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()); + } +} 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()); + } +} 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()); + } +} 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()); + } +}