DbalExceptionTest.php 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  5. *
  6. * This code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License, version 3,
  8. * as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License, version 3,
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>
  17. *
  18. */
  19. namespace Test\DB\Exception;
  20. use Doctrine\DBAL\ConnectionException;
  21. use Doctrine\DBAL\Driver\Exception as TheDriverException;
  22. use Doctrine\DBAL\Exception\ConstraintViolationException;
  23. use Doctrine\DBAL\Exception\DatabaseObjectExistsException;
  24. use Doctrine\DBAL\Exception\DatabaseObjectNotFoundException;
  25. use Doctrine\DBAL\Exception\DeadlockException;
  26. use Doctrine\DBAL\Exception\DriverException;
  27. use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
  28. use Doctrine\DBAL\Exception\InvalidArgumentException;
  29. use Doctrine\DBAL\Exception\InvalidFieldNameException;
  30. use Doctrine\DBAL\Exception\NonUniqueFieldNameException;
  31. use Doctrine\DBAL\Exception\NotNullConstraintViolationException;
  32. use Doctrine\DBAL\Exception\ServerException;
  33. use Doctrine\DBAL\Exception\SyntaxErrorException;
  34. use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
  35. use OC\DB\Exceptions\DbalException;
  36. class DbalExceptionTest extends \Test\TestCase {
  37. /** @var TheDriverException */
  38. protected $driverException;
  39. protected function setUp(): void {
  40. parent::setUp();
  41. $this->driverException = $this->createMock(TheDriverException::class);
  42. }
  43. /**
  44. * @dataProvider dataDriverException
  45. * @param string $class
  46. * @param int $reason
  47. */
  48. public function testDriverException(string $class, int $reason): void {
  49. $result = DbalException::wrap(new $class($this->driverException, null));
  50. $this->assertSame($reason, $result->getReason());
  51. }
  52. public function dataDriverException(): array {
  53. return [
  54. [ForeignKeyConstraintViolationException::class, DbalException::REASON_FOREIGN_KEY_VIOLATION],
  55. [NotNullConstraintViolationException::class, DbalException::REASON_NOT_NULL_CONSTRAINT_VIOLATION],
  56. [UniqueConstraintViolationException::class, DbalException::REASON_UNIQUE_CONSTRAINT_VIOLATION],
  57. [ConstraintViolationException::class, DbalException::REASON_CONSTRAINT_VIOLATION],
  58. [DatabaseObjectExistsException::class, DbalException::REASON_DATABASE_OBJECT_EXISTS],
  59. [DatabaseObjectNotFoundException::class, DbalException::REASON_DATABASE_OBJECT_NOT_FOUND],
  60. [DeadlockException::class, DbalException::REASON_DEADLOCK],
  61. [InvalidFieldNameException::class, DbalException::REASON_INVALID_FIELD_NAME],
  62. [NonUniqueFieldNameException::class, DbalException::REASON_NON_UNIQUE_FIELD_NAME],
  63. [SyntaxErrorException::class, DbalException::REASON_SYNTAX_ERROR],
  64. [ServerException::class, DbalException::REASON_SERVER],
  65. [DriverException::class, DbalException::REASON_DRIVER],
  66. ];
  67. }
  68. public function testConnectionException(): void {
  69. $result = DbalException::wrap(ConnectionException::noActiveTransaction());
  70. $this->assertSame(DbalException::REASON_CONNECTION_LOST, $result->getReason());
  71. }
  72. public function testInvalidArgumentException(): void {
  73. $result = DbalException::wrap(InvalidArgumentException::fromEmptyCriteria());
  74. $this->assertSame(DbalException::REASON_INVALID_ARGUMENT, $result->getReason());
  75. }
  76. }