Skip to content

Commit

Permalink
Merge pull request #7 from stratedge/feature/implement-commit-and-rol…
Browse files Browse the repository at this point in the history
…lback

Feature/implement commit and rollback
  • Loading branch information
stratedge authored Jul 31, 2017
2 parents b281429 + 6a4d208 commit 6cd3113
Show file tree
Hide file tree
Showing 10 changed files with 349 additions and 7 deletions.
13 changes: 9 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
34 changes: 32 additions & 2 deletions src/PDO/PDO.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
}
32 changes: 31 additions & 1 deletion src/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ class Transaction
protected $index;

/**
* @var committed
* @var boolean
*/
protected $committed = false;

/**
* @var boolean
*/
protected $rolled_back = false;

/**
* @var array
*/
Expand Down Expand Up @@ -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
Expand Down
43 changes: 43 additions & 0 deletions src/Wye.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
55 changes: 55 additions & 0 deletions tests/PDO/PDO/CommitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Tests\PDO\PDO;

use PDOException;
use Stratedge\Wye\Wye;

class CommitTest extends \Tests\TestCase
{
public function testNoTransactionThrowsException()
{
$this->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());
}
}
55 changes: 55 additions & 0 deletions tests/PDO/PDO/RollBackTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Tests\PDO\PDO;

use PDOException;
use Stratedge\Wye\Wye;

class RollBackTest extends \Tests\TestCase
{
public function testNoTransactionThrowsException()
{
$this->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());
}
}
19 changes: 19 additions & 0 deletions tests/Transaction/GetRolledBackTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Tests\Transaction;

use Stratedge\Wye\Wye;

class GetRolledBackTest extends \Tests\TestCase
{
public function testReturnsPropertyValue()
{
$transaction = Wye::makeTransaction();

$this->assertFalse($transaction->getRolledBack());

$transaction->setRolledBack(true);

$this->assertTrue($transaction->getRolledBack());
}
}
23 changes: 23 additions & 0 deletions tests/Transaction/SetRolledBackTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Tests\Transaction;

use Stratedge\Wye\Wye;

class SetRolledBackTest extends \Tests\TestCase
{
public function testSetsPropertyValue()
{
$transaction = Wye::makeTransaction();

$this->assertFalse($transaction->getRolledBack());

$transaction->setRolledBack(true);

$this->assertTrue($transaction->getRolledBack());

$transaction->setRolledBack(false);

$this->assertFalse($transaction->getRolledBack());
}
}
41 changes: 41 additions & 0 deletions tests/Wye/CommitTransactionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Tests\Wye;

use PDOException;
use Stratedge\Wye\Wye;

class CommitTransactionTest extends \Tests\TestCase
{
public function testNoTransactionThrowsException()
{
$this->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());
}
}
Loading

0 comments on commit 6cd3113

Please sign in to comment.