DbalExceptionTest.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace Test\DB\Exception;
  8. use Doctrine\DBAL\ConnectionException;
  9. use Doctrine\DBAL\Driver\Exception as TheDriverException;
  10. use Doctrine\DBAL\Exception\ConstraintViolationException;
  11. use Doctrine\DBAL\Exception\DatabaseObjectExistsException;
  12. use Doctrine\DBAL\Exception\DatabaseObjectNotFoundException;
  13. use Doctrine\DBAL\Exception\DeadlockException;
  14. use Doctrine\DBAL\Exception\DriverException;
  15. use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
  16. use Doctrine\DBAL\Exception\InvalidArgumentException;
  17. use Doctrine\DBAL\Exception\InvalidFieldNameException;
  18. use Doctrine\DBAL\Exception\LockWaitTimeoutException;
  19. use Doctrine\DBAL\Exception\NonUniqueFieldNameException;
  20. use Doctrine\DBAL\Exception\NotNullConstraintViolationException;
  21. use Doctrine\DBAL\Exception\ServerException;
  22. use Doctrine\DBAL\Exception\SyntaxErrorException;
  23. use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
  24. use OC\DB\Exceptions\DbalException;
  25. class DbalExceptionTest extends \Test\TestCase {
  26. /** @var TheDriverException */
  27. protected $driverException;
  28. protected function setUp(): void {
  29. parent::setUp();
  30. $this->driverException = $this->createMock(TheDriverException::class);
  31. }
  32. /**
  33. * @dataProvider dataDriverException
  34. * @param string $class
  35. * @param int $reason
  36. */
  37. public function testDriverException(string $class, int $reason): void {
  38. $result = DbalException::wrap(new $class($this->driverException, null));
  39. $this->assertSame($reason, $result->getReason());
  40. }
  41. public function dataDriverException(): array {
  42. return [
  43. [LockWaitTimeoutException::class, DbalException::REASON_LOCK_WAIT_TIMEOUT],
  44. [ForeignKeyConstraintViolationException::class, DbalException::REASON_FOREIGN_KEY_VIOLATION],
  45. [NotNullConstraintViolationException::class, DbalException::REASON_NOT_NULL_CONSTRAINT_VIOLATION],
  46. [UniqueConstraintViolationException::class, DbalException::REASON_UNIQUE_CONSTRAINT_VIOLATION],
  47. [ConstraintViolationException::class, DbalException::REASON_CONSTRAINT_VIOLATION],
  48. [DatabaseObjectExistsException::class, DbalException::REASON_DATABASE_OBJECT_EXISTS],
  49. [DatabaseObjectNotFoundException::class, DbalException::REASON_DATABASE_OBJECT_NOT_FOUND],
  50. [DeadlockException::class, DbalException::REASON_DEADLOCK],
  51. [InvalidFieldNameException::class, DbalException::REASON_INVALID_FIELD_NAME],
  52. [NonUniqueFieldNameException::class, DbalException::REASON_NON_UNIQUE_FIELD_NAME],
  53. [SyntaxErrorException::class, DbalException::REASON_SYNTAX_ERROR],
  54. [ServerException::class, DbalException::REASON_SERVER],
  55. [DriverException::class, DbalException::REASON_DRIVER],
  56. ];
  57. }
  58. public function testConnectionException(): void {
  59. $result = DbalException::wrap(ConnectionException::noActiveTransaction());
  60. $this->assertSame(DbalException::REASON_CONNECTION_LOST, $result->getReason());
  61. }
  62. public function testInvalidArgumentException(): void {
  63. $result = DbalException::wrap(InvalidArgumentException::fromEmptyCriteria());
  64. $this->assertSame(DbalException::REASON_INVALID_ARGUMENT, $result->getReason());
  65. }
  66. }