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);
|
2017-12-22 03:47:19 +00:00
|
|
|
namespace JKingWeb\Arsse\TestCase\Db;
|
2017-08-29 14:50:31 +00:00
|
|
|
|
2017-07-22 19:29:12 +00:00
|
|
|
use JKingWeb\Arsse\Db\Transaction;
|
2017-12-22 03:47:19 +00:00
|
|
|
use JKingWeb\Arsse\Db\Exception;
|
2017-07-22 19:29:12 +00:00
|
|
|
|
2017-08-29 15:16:37 +00:00
|
|
|
/**
|
2017-07-22 19:29:12 +00:00
|
|
|
* @covers \JKingWeb\Arsse\Db\Transaction */
|
2017-12-22 03:47:19 +00:00
|
|
|
class TestTransaction extends \JKingWeb\Arsse\Test\AbstractTest {
|
2017-07-22 19:29:12 +00:00
|
|
|
protected $drv;
|
|
|
|
|
2019-10-16 18:42:43 +00:00
|
|
|
public function setUp(): void {
|
2021-02-27 20:24:02 +00:00
|
|
|
parent::setUp();
|
|
|
|
$drv = $this->mock(\JKingWeb\Arsse\Db\SQLite3\Driver::class);
|
|
|
|
$drv->savepointRelease->returns(true);
|
|
|
|
$drv->savepointUndo->returns(true);
|
|
|
|
$drv->savepointCreate->returns(1, 2);
|
2017-07-22 19:29:12 +00:00
|
|
|
$this->drv = $drv;
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testManipulateTransactions(): void {
|
2021-02-27 20:24:02 +00:00
|
|
|
$drv = $this->drv->get();
|
|
|
|
$tr1 = new Transaction($drv);
|
|
|
|
$tr2 = new Transaction($drv);
|
|
|
|
$this->drv->savepointCreate->twice()->called();
|
2017-07-22 19:29:12 +00:00
|
|
|
$this->assertSame(1, $tr1->getIndex());
|
|
|
|
$this->assertSame(2, $tr2->getIndex());
|
|
|
|
unset($tr1);
|
2021-02-27 20:24:02 +00:00
|
|
|
$this->drv->savepointUndo->calledWith(1);
|
2017-07-22 19:29:12 +00:00
|
|
|
unset($tr2);
|
2021-02-27 20:24:02 +00:00
|
|
|
$this->drv->savepointUndo->calledWith(2);
|
2017-07-22 19:29:12 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testCloseTransactions(): void {
|
2021-02-27 20:24:02 +00:00
|
|
|
$drv = $this->drv->get();
|
|
|
|
$tr1 = new Transaction($drv);
|
|
|
|
$tr2 = new Transaction($drv);
|
2017-07-22 19:29:12 +00:00
|
|
|
$this->assertTrue($tr1->isPending());
|
|
|
|
$this->assertTrue($tr2->isPending());
|
|
|
|
$tr1->commit();
|
|
|
|
$this->assertFalse($tr1->isPending());
|
|
|
|
$this->assertTrue($tr2->isPending());
|
2021-02-27 20:24:02 +00:00
|
|
|
$this->drv->savepointRelease->calledWith(1);
|
2017-07-22 19:29:12 +00:00
|
|
|
$tr2->rollback();
|
|
|
|
$this->assertFalse($tr1->isPending());
|
|
|
|
$this->assertFalse($tr2->isPending());
|
2021-02-27 20:24:02 +00:00
|
|
|
$this->drv->savepointUndo->calledWith(2);
|
2017-07-22 19:29:12 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testIgnoreRollbackErrors(): void {
|
2021-02-27 20:24:02 +00:00
|
|
|
$this->drv->savepointUndo->throws(new Exception("savepointStale"));
|
|
|
|
$drv = $this->drv->get();
|
|
|
|
$tr1 = new Transaction($drv);
|
|
|
|
$tr2 = new Transaction($drv);
|
2017-07-22 19:29:12 +00:00
|
|
|
unset($tr1, $tr2); // no exception should bubble up
|
2021-02-27 20:24:02 +00:00
|
|
|
$this->drv->savepointUndo->calledWith(1);
|
|
|
|
$this->drv->savepointUndo->calledWith(2);
|
2017-07-22 19:29:12 +00:00
|
|
|
}
|
2017-08-29 14:50:31 +00:00
|
|
|
}
|