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