1
0

DbalExceptionTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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\NonUniqueFieldNameException;
  19. use Doctrine\DBAL\Exception\NotNullConstraintViolationException;
  20. use Doctrine\DBAL\Exception\ServerException;
  21. use Doctrine\DBAL\Exception\SyntaxErrorException;
  22. use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
  23. use OC\DB\Exceptions\DbalException;
  24. class DbalExceptionTest extends \Test\TestCase {
  25. /** @var TheDriverException */
  26. protected $driverException;
  27. protected function setUp(): void {
  28. parent::setUp();
  29. $this->driverException = $this->createMock(TheDriverException::class);
  30. }
  31. /**
  32. * @dataProvider dataDriverException
  33. * @param string $class
  34. * @param int $reason
  35. */
  36. public function testDriverException(string $class, int $reason): void {
  37. $result = DbalException::wrap(new $class($this->driverException, null));
  38. $this->assertSame($reason, $result->getReason());
  39. }
  40. public function dataDriverException(): array {
  41. return [
  42. [ForeignKeyConstraintViolationException::class, DbalException::REASON_FOREIGN_KEY_VIOLATION],
  43. [NotNullConstraintViolationException::class, DbalException::REASON_NOT_NULL_CONSTRAINT_VIOLATION],
  44. [UniqueConstraintViolationException::class, DbalException::REASON_UNIQUE_CONSTRAINT_VIOLATION],
  45. [ConstraintViolationException::class, DbalException::REASON_CONSTRAINT_VIOLATION],
  46. [DatabaseObjectExistsException::class, DbalException::REASON_DATABASE_OBJECT_EXISTS],
  47. [DatabaseObjectNotFoundException::class, DbalException::REASON_DATABASE_OBJECT_NOT_FOUND],
  48. [DeadlockException::class, DbalException::REASON_DEADLOCK],
  49. [InvalidFieldNameException::class, DbalException::REASON_INVALID_FIELD_NAME],
  50. [NonUniqueFieldNameException::class, DbalException::REASON_NON_UNIQUE_FIELD_NAME],
  51. [SyntaxErrorException::class, DbalException::REASON_SYNTAX_ERROR],
  52. [ServerException::class, DbalException::REASON_SERVER],
  53. [DriverException::class, DbalException::REASON_DRIVER],
  54. ];
  55. }
  56. public function testConnectionException(): void {
  57. $result = DbalException::wrap(ConnectionException::noActiveTransaction());
  58. $this->assertSame(DbalException::REASON_CONNECTION_LOST, $result->getReason());
  59. }
  60. public function testInvalidArgumentException(): void {
  61. $result = DbalException::wrap(InvalidArgumentException::fromEmptyCriteria());
  62. $this->assertSame(DbalException::REASON_INVALID_ARGUMENT, $result->getReason());
  63. }
  64. }