1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-31 21:12:41 +00:00
Arsse/tests/cases/Db/TestTransaction.php

67 lines
2.3 KiB
PHP
Raw Permalink Normal View History

<?php
/** @license MIT
* Copyright 2017 J. King, Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
2021-04-14 15:17:01 +00:00
2017-12-22 03:47:19 +00:00
namespace JKingWeb\Arsse\TestCase\Db;
2017-08-29 14:50:31 +00:00
use JKingWeb\Arsse\Db\Transaction;
2017-12-22 03:47:19 +00:00
use JKingWeb\Arsse\Db\Exception;
use PHPUnit\Framework\Attributes\CoversClass;
#[CoversClass(\JKingWeb\Arsse\Db\Transaction::class)]
2017-12-22 03:47:19 +00:00
class TestTransaction extends \JKingWeb\Arsse\Test\AbstractTest {
protected $drv;
2019-10-16 18:42:43 +00:00
public function setUp(): void {
2024-12-25 13:24:12 +00:00
self::clearData();
$drv = \Phake::mock(\JKingWeb\Arsse\Db\SQLite3\Driver::class);
\Phake::when($drv)->savepointRelease->thenReturn(true);
\Phake::when($drv)->savepointUndo->thenReturn(true);
2024-12-25 03:58:58 +00:00
\Phake::when($drv)->savepointCreate->thenReturn(1)->thenReturn(2);
$this->drv = $drv;
}
2020-01-20 18:52:48 +00:00
public function testManipulateTransactions(): void {
$drv = $this->drv;
2021-02-27 20:24:02 +00:00
$tr1 = new Transaction($drv);
$tr2 = new Transaction($drv);
2024-12-25 13:24:12 +00:00
\Phake::verify($this->drv, \Phake::times(2))->savepointCreate(\Phake::anyParameters());
$this->assertSame(1, $tr1->getIndex());
$this->assertSame(2, $tr2->getIndex());
unset($tr1);
unset($tr2);
2024-12-25 13:24:12 +00:00
\Phake::verify($this->drv)->savepointUndo(1);
\Phake::verify($this->drv)->savepointUndo(2);
}
2020-01-20 18:52:48 +00:00
public function testCloseTransactions(): void {
$drv = $this->drv;
2021-02-27 20:24:02 +00:00
$tr1 = new Transaction($drv);
$tr2 = new Transaction($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);
}
2020-01-20 18:52:48 +00:00
public function testIgnoreRollbackErrors(): void {
\Phake::when($this->drv)->savepointUndo->thenThrow(new Exception("savepointStale"));
2024-12-25 13:24:12 +00:00
$tr1 = new Transaction($this->drv);
2024-12-27 14:45:22 +00:00
$tr2 = new Transaction($this->drv);
2024-12-25 13:24:12 +00:00
unset($tr1); // no exception should bubble up
2024-12-27 14:45:22 +00:00
unset($tr2); // no exception should bubble up
\Phake::verify($this->drv)->savepointUndo(1);
2024-12-27 14:45:22 +00:00
\Phake::verify($this->drv)->savepointUndo(2);
}
2017-08-29 14:50:31 +00:00
}