File tree 5 files changed +31
-15
lines changed
5 files changed +31
-15
lines changed Original file line number Diff line number Diff line change @@ -88,11 +88,8 @@ public function columnCount(): int
88
88
89
89
public function getColumnName (int $ index ): string
90
90
{
91
- if ($ this ->data === [] || $ index > count ($ this ->data [0 ])) {
92
- throw InvalidColumnIndex::new ($ index );
93
- }
94
-
95
- return array_keys ($ this ->data [0 ])[$ index ];
91
+ return array_keys ($ this ->data [0 ] ?? [])[$ index ]
92
+ ?? throw InvalidColumnIndex::new ($ index );
96
93
}
97
94
98
95
public function free (): void
Original file line number Diff line number Diff line change 9
9
use PDO ;
10
10
use PDOException ;
11
11
use PDOStatement ;
12
+ use ValueError ;
12
13
13
14
final class Result implements ResultInterface
14
15
{
@@ -78,15 +79,17 @@ public function getColumnName(int $index): string
78
79
{
79
80
try {
80
81
$ meta = $ this ->statement ->getColumnMeta ($ index );
81
-
82
- if ($ meta === false ) {
83
- throw InvalidColumnIndex::new ($ index );
84
- }
85
-
86
- return $ meta ['name ' ];
82
+ } catch (ValueError $ exception ) {
83
+ throw InvalidColumnIndex::new ($ index , $ exception );
87
84
} catch (PDOException $ exception ) {
88
85
throw Exception::new ($ exception );
89
86
}
87
+
88
+ if ($ meta === false ) {
89
+ throw InvalidColumnIndex::new ($ index );
90
+ }
91
+
92
+ return $ meta ['name ' ];
90
93
}
91
94
92
95
public function free (): void
Original file line number Diff line number Diff line change 6
6
7
7
use Doctrine \DBAL \Exception ;
8
8
use LogicException ;
9
+ use Throwable ;
9
10
10
11
use function sprintf ;
11
12
12
13
/** @psalm-immutable */
13
14
final class InvalidColumnIndex extends LogicException implements Exception
14
15
{
15
- public static function new (int $ index ): self
16
+ public static function new (int $ index, ? Throwable $ previous = null ): self
16
17
{
17
- return new self (sprintf ('Invalid column index "%s". ' , $ index ));
18
+ return new self (sprintf ('Invalid column index "%s". ' , $ index ), previous: $ previous );
18
19
}
19
20
}
Original file line number Diff line number Diff line change 5
5
namespace Doctrine \DBAL \Tests \Cache ;
6
6
7
7
use Doctrine \DBAL \Cache \ArrayResult ;
8
+ use Doctrine \DBAL \Exception \InvalidColumnIndex ;
9
+ use PHPUnit \Framework \Attributes \TestWith ;
8
10
use PHPUnit \Framework \TestCase ;
9
11
10
12
use function array_values ;
@@ -49,6 +51,16 @@ public function testColumnNames(): void
49
51
self ::assertSame ('active ' , $ statement ->getColumnName (1 ));
50
52
}
51
53
54
+ #[TestWith([2 ])]
55
+ #[TestWith([-1 ])]
56
+ public function testColumnNameWithInvalidIndex (int $ index ): void
57
+ {
58
+ $ statement = $ this ->createTestArrayStatement ();
59
+ $ this ->expectException (InvalidColumnIndex::class);
60
+
61
+ $ statement ->getColumnName ($ index );
62
+ }
63
+
52
64
public function testRowCount (): void
53
65
{
54
66
$ statement = $ this ->createTestArrayStatement ();
Original file line number Diff line number Diff line change 7
7
use Doctrine \DBAL \Exception \InvalidColumnIndex ;
8
8
use Doctrine \DBAL \Schema \Table ;
9
9
use Doctrine \DBAL \Tests \FunctionalTestCase ;
10
+ use PHPUnit \Framework \Attributes \TestWith ;
10
11
11
12
use function strtolower ;
12
13
@@ -36,7 +37,9 @@ public function testColumnNameWithResults(): void
36
37
self ::assertEquals ('alternate_name ' , strtolower ($ result ->getColumnName (1 )));
37
38
}
38
39
39
- public function testColumnNameWithInvalidIndex (): void
40
+ #[TestWith([2 ])]
41
+ #[TestWith([-1 ])]
42
+ public function testColumnNameWithInvalidIndex (int $ index ): void
40
43
{
41
44
$ sql = 'SELECT test_int, test_int AS alternate_name FROM result_metadata_table ' ;
42
45
@@ -47,7 +50,7 @@ public function testColumnNameWithInvalidIndex(): void
47
50
48
51
$ this ->expectException (InvalidColumnIndex::class);
49
52
50
- $ result ->getColumnName (2 );
53
+ $ result ->getColumnName ($ index );
51
54
}
52
55
53
56
public function testColumnNameWithoutResults (): void
You can’t perform that action at this time.
0 commit comments