TransactionalTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * @copyright 2022 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author 2022 Christoph Wurst <christoph@winzerhof-wurst.at>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. namespace lib\AppFramework\Db;
  24. use OCP\AppFramework\Db\TTransactional;
  25. use OCP\IDBConnection;
  26. use PHPUnit\Framework\MockObject\MockObject;
  27. use RuntimeException;
  28. use Test\TestCase;
  29. class TransactionalTest extends TestCase {
  30. /** @var IDBConnection|MockObject */
  31. private IDBConnection $db;
  32. protected function setUp(): void {
  33. parent::setUp();
  34. $this->db = $this->createMock(IDBConnection::class);
  35. }
  36. public function testAtomicRollback(): void {
  37. $test = new class($this->db) {
  38. use TTransactional;
  39. private IDBConnection $db;
  40. public function __construct(IDBConnection $db) {
  41. $this->db = $db;
  42. }
  43. public function fail(): void {
  44. $this->atomic(function () {
  45. throw new RuntimeException('nope');
  46. }, $this->db);
  47. }
  48. };
  49. $this->db->expects(self::once())
  50. ->method('beginTransaction');
  51. $this->db->expects(self::once())
  52. ->method('rollback');
  53. $this->db->expects(self::never())
  54. ->method('commit');
  55. $this->expectException(RuntimeException::class);
  56. $test->fail();
  57. }
  58. public function testAtomicCommit(): void {
  59. $test = new class($this->db) {
  60. use TTransactional;
  61. private IDBConnection $db;
  62. public function __construct(IDBConnection $db) {
  63. $this->db = $db;
  64. }
  65. public function succeed(): int {
  66. return $this->atomic(function () {
  67. return 3;
  68. }, $this->db);
  69. }
  70. };
  71. $this->db->expects(self::once())
  72. ->method('beginTransaction');
  73. $this->db->expects(self::never())
  74. ->method('rollback');
  75. $this->db->expects(self::once())
  76. ->method('commit');
  77. $result = $test->succeed();
  78. self::assertEquals(3, $result);
  79. }
  80. }