2017-07-22 19:29:12 +00:00
|
|
|
<?php
|
2017-11-17 01:23:18 +00:00
|
|
|
/** @license MIT
|
|
|
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
|
|
|
* See LICENSE and AUTHORS files for details */
|
|
|
|
|
2017-07-22 19:29:12 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
namespace JKingWeb\Arsse;
|
2017-08-29 14:50:31 +00:00
|
|
|
|
2017-07-22 19:29:12 +00:00
|
|
|
use JKingWeb\Arsse\Db\Transaction;
|
|
|
|
use Phake;
|
|
|
|
|
2017-08-29 15:16:37 +00:00
|
|
|
/**
|
2017-07-22 19:29:12 +00:00
|
|
|
* @covers \JKingWeb\Arsse\Db\Transaction */
|
|
|
|
class TestTransaction extends Test\AbstractTest {
|
|
|
|
protected $drv;
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function setUp() {
|
2017-07-22 19:29:12 +00:00
|
|
|
$this->clearData();
|
|
|
|
$drv = Phake::mock(Db\SQLite3\Driver::class);
|
|
|
|
Phake::when($drv)->savepointRelease->thenReturn(true);
|
|
|
|
Phake::when($drv)->savepointUndo->thenReturn(true);
|
|
|
|
Phake::when($drv)->savepointCreate->thenReturn(1)->thenReturn(2);
|
|
|
|
$this->drv = $drv;
|
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testManipulateTransactions() {
|
2017-07-22 19:29:12 +00:00
|
|
|
$tr1 = new Transaction($this->drv);
|
|
|
|
$tr2 = new Transaction($this->drv);
|
|
|
|
Phake::verify($this->drv, Phake::times(2))->savepointCreate;
|
|
|
|
$this->assertSame(1, $tr1->getIndex());
|
|
|
|
$this->assertSame(2, $tr2->getIndex());
|
|
|
|
unset($tr1);
|
|
|
|
Phake::verify($this->drv)->savepointUndo(1);
|
|
|
|
unset($tr2);
|
|
|
|
Phake::verify($this->drv)->savepointUndo(2);
|
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testCloseTransactions() {
|
2017-07-22 19:29:12 +00:00
|
|
|
$tr1 = new Transaction($this->drv);
|
|
|
|
$tr2 = new Transaction($this->drv);
|
|
|
|
$this->assertTrue($tr1->isPending());
|
|
|
|
$this->assertTrue($tr2->isPending());
|
|
|
|
$tr1->commit();
|
|
|
|
$this->assertFalse($tr1->isPending());
|
|
|
|
$this->assertTrue($tr2->isPending());
|
|
|
|
Phake::verify($this->drv)->savepointRelease(1);
|
|
|
|
$tr2->rollback();
|
|
|
|
$this->assertFalse($tr1->isPending());
|
|
|
|
$this->assertFalse($tr2->isPending());
|
|
|
|
Phake::verify($this->drv)->savepointUndo(2);
|
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testIgnoreRollbackErrors() {
|
2017-11-06 03:13:44 +00:00
|
|
|
Phake::when($this->drv)->savepointUndo->thenThrow(new Db\Exception("savepointStale"));
|
2017-07-22 19:29:12 +00:00
|
|
|
$tr1 = new Transaction($this->drv);
|
|
|
|
$tr2 = new Transaction($this->drv);
|
|
|
|
unset($tr1, $tr2); // no exception should bubble up
|
|
|
|
Phake::verify($this->drv)->savepointUndo(1);
|
|
|
|
Phake::verify($this->drv)->savepointUndo(2);
|
|
|
|
}
|
2017-08-29 14:50:31 +00:00
|
|
|
}
|