Skip to content

Commit

Permalink
Add rollBack() method to PDO class
Browse files Browse the repository at this point in the history
  • Loading branch information
stratedge committed Jul 31, 2017
1 parent 421d755 commit 7b8d5a9
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/PDO/PDO.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -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;
}
}
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());
}
}

0 comments on commit 7b8d5a9

Please sign in to comment.