|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace ClickHouseDB\Tests; |
| 6 | + |
| 7 | +use ClickHouseDB\Exception\DatabaseException; |
| 8 | +use ClickHouseDB\Statement; |
| 9 | +use ClickHouseDB\Transport\CurlerRequest; |
| 10 | +use ClickHouseDB\Transport\CurlerResponse; |
| 11 | +use Generator; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | + |
| 14 | +/** |
| 15 | + * Class StatementTest |
| 16 | + * @group StatementTest |
| 17 | + */ |
| 18 | +final class StatementTest extends TestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @dataProvider dataProvider |
| 22 | + */ |
| 23 | + public function testParseErrorClickHouse( |
| 24 | + string $errorMessage, |
| 25 | + string $exceptionMessage, |
| 26 | + int $exceptionCode |
| 27 | + ): void { |
| 28 | + $requestMock = $this->createMock(CurlerRequest::class); |
| 29 | + $responseMock = $this->createMock(CurlerResponse::class); |
| 30 | + |
| 31 | + $responseMock->expects($this->any())->method('body')->will($this->returnValue($errorMessage)); |
| 32 | + $responseMock->expects($this->any())->method('error_no')->will($this->returnValue(0)); |
| 33 | + $responseMock->expects($this->any())->method('error')->will($this->returnValue(false)); |
| 34 | + |
| 35 | + $requestMock->expects($this->any())->method('response')->will($this->returnValue($responseMock)); |
| 36 | + |
| 37 | + $statement = new Statement($requestMock); |
| 38 | + $this->assertInstanceOf(Statement::class, $statement); |
| 39 | + |
| 40 | + $this->expectException(DatabaseException::class); |
| 41 | + $this->expectDeprecationMessage($exceptionMessage); |
| 42 | + $this->expectExceptionCode($exceptionCode); |
| 43 | + |
| 44 | + $statement->error(); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @return Generator |
| 49 | + */ |
| 50 | + public function dataProvider(): Generator |
| 51 | + { |
| 52 | + yield 'Unknown setting readonly' => [ |
| 53 | + 'Code: 115. DB::Exception: Unknown setting readonly[0], e.what() = DB::Exception', |
| 54 | + 'Unknown setting readonly[0]', |
| 55 | + 115, |
| 56 | + ]; |
| 57 | + |
| 58 | + yield 'Unknown user x' => [ |
| 59 | + 'Code: 192. DB::Exception: Unknown user x, e.what() = DB::Exception', |
| 60 | + 'Unknown user x', |
| 61 | + 192, |
| 62 | + ]; |
| 63 | + |
| 64 | + yield 'Table default.ZZZZZ doesn\'t exist.' => [ |
| 65 | + 'Code: 60. DB::Exception: Table default.ZZZZZ doesn\'t exist., e.what() = DB::Exception', |
| 66 | + 'Table default.ZZZZZ doesn\'t exist.', |
| 67 | + 60, |
| 68 | + ]; |
| 69 | + |
| 70 | + yield 'Authentication failed' => [ |
| 71 | + 'Code: 516. DB::Exception: test_username: Authentication failed: password is incorrect or there is no user with such name. (AUTHENTICATION_FAILED) (version 22.8.3.13 (official build))', |
| 72 | + 'test_username: Authentication failed: password is incorrect or there is no user with such name. (AUTHENTICATION_FAILED)', |
| 73 | + 516 |
| 74 | + ]; |
| 75 | + } |
| 76 | +} |
0 commit comments