Skip to content

Commit 4b9034b

Browse files
committed
assertions
1 parent 66bd1cb commit 4b9034b

File tree

3 files changed

+77
-30
lines changed

3 files changed

+77
-30
lines changed

tests/default/data/pdo-mysql.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,50 +10,58 @@ class Foo
1010
public function execute(PDO $pdo)
1111
{
1212
$stmt = $pdo->prepare('SELECT email, adaid FROM ada WHERE email <=> :email');
13-
assertType('PDOStatement', $stmt);
1413
$stmt->execute([':email' => null]);
15-
assertType('PDOStatement<array{email: string, 0: string, adaid: int<-32768, 32767>, 1: int<-32768, 32767>}>', $stmt);
14+
foreach ($stmt as $row) {
15+
assertType('array{email: string, 0: string, adaid: int<-32768, 32767>, 1: int<-32768, 32767>}', $row);
16+
}
1617
}
1718

1819
public function aggregateFunctions(PDO $pdo)
1920
{
2021
$query = 'SELECT MAX(adaid), MIN(adaid), COUNT(adaid), AVG(adaid) FROM ada WHERE adaid = 1';
2122
$stmt = $pdo->query($query, PDO::FETCH_ASSOC);
22-
assertType('PDOStatement<array{MAX(adaid): int<-32768, 32767>|null, MIN(adaid): int<-32768, 32767>|null, COUNT(adaid): int, AVG(adaid): numeric-string|null}>', $stmt);
23+
foreach ($stmt as $row) {
24+
assertType('array{MAX(adaid): int<-32768, 32767>|null, MIN(adaid): int<-32768, 32767>|null, COUNT(adaid): int, AVG(adaid): numeric-string|null}', $row);
25+
}
2326
}
2427

2528
public function placeholderInDataPrepared(PDO $pdo)
2629
{
2730
// double quotes within the query
2831
$query = 'SELECT adaid FROM ada WHERE email LIKE ":gesperrt%"';
2932
$stmt = $pdo->prepare($query);
30-
assertType('PDOStatement<array{adaid: int<-32768, 32767>, 0: int<-32768, 32767>}>', $stmt);
3133
$stmt->execute();
32-
assertType('PDOStatement<array{adaid: int<-32768, 32767>, 0: int<-32768, 32767>}>', $stmt);
34+
foreach ($stmt as $row) {
35+
assertType('array{adaid: int<-32768, 32767>, 0: int<-32768, 32767>}', $row);
36+
}
3337

3438
// single quotes within the query
3539
$query = "SELECT adaid FROM ada WHERE email LIKE ':gesperrt%'";
3640
$stmt = $pdo->prepare($query);
37-
assertType('PDOStatement<array{adaid: int<-32768, 32767>, 0: int<-32768, 32767>}>', $stmt);
3841
$stmt->execute();
39-
assertType('PDOStatement<array{adaid: int<-32768, 32767>, 0: int<-32768, 32767>}>', $stmt);
42+
foreach ($stmt as $row) {
43+
assertType('array{adaid: int<-32768, 32767>, 0: int<-32768, 32767>}', $row);
44+
}
4045
}
4146

4247
public function placeholderInDataQuery(PDO $pdo)
4348
{
4449
// double quotes within the query
4550
$query = 'SELECT adaid FROM ada WHERE email LIKE ":gesperrt%"';
4651
$stmt = $pdo->query($query, PDO::FETCH_ASSOC);
47-
assertType('PDOStatement<array{adaid: int<-32768, 32767>}>', $stmt);
52+
foreach ($stmt as $row) {
53+
assertType('array{adaid: int<-32768, 32767>>}', $row);
54+
}
4855
}
4956

5057
public function bug541(PDO $pdo)
5158
{
5259
$query = 'SELECT email, adaid FROM ada';
5360
$query .= 'WHERE email <=> :email';
5461
$stmt = $pdo->prepare($query);
55-
assertType('PDOStatement', $stmt);
5662
$stmt->execute([':email' => null]);
57-
assertType('PDOStatement<array{email: string, 0: string, adaid: int<-32768, 32767>, 1: int<-32768, 32767>}>', $stmt);
63+
foreach ($stmt as $row) {
64+
assertType('array{email: string, 0: string, adaid: int<-32768, 32767>, 1: int<-32768, 32767>}', $row);
65+
}
5866
}
5967
}

tests/default/data/pdo-pgsql.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@ class Foo
1010
public function pgsqlTypes(PDO $pdo)
1111
{
1212
$stmt = $pdo->query('SELECT * FROM typemix', PDO::FETCH_ASSOC);
13-
assertType('PDOStatement<array{pid: int<1, 2147483647>, c_varchar5: string, c_varchar25: string|null, c_varchar255: string, c_date: string|null, c_time: string|null, c_datetime: string|null, c_timestamp: string|null, c_text: string|null, c_enum: mixed, c_bit255: int, c_bit25: int|null, c_bit: int|null, c_int: int<-2147483648, 2147483647>, c_smallint: int<-32768, 32767>, c_bigint: int, c_float: float, c_boolean: bool, c_json: string, c_json_nullable: string|null, c_jsonb: string, c_jsonb_nullable: string|null}>', $stmt);
13+
foreach ($stmt as $row) {
14+
assertType('array{pid: int<1, 2147483647>, c_varchar5: string, c_varchar25: string|null, c_varchar255: string, c_date: string|null, c_time: string|null, c_datetime: string|null, c_timestamp: string|null, c_text: string|null, c_enum: mixed, c_bit255: int, c_bit25: int|null, c_bit: int|null, c_int: int<-2147483648, 2147483647>, c_smallint: int<-32768, 32767>, c_bigint: int, c_float: float, c_boolean: bool, c_json: string, c_json_nullable: string|null, c_jsonb: string, c_jsonb_nullable: string|null}', $row);
15+
}
1416
}
1517

1618
public function aggregateFunctions(PDO $pdo)
1719
{
1820
$query = 'SELECT MAX(adaid), MIN(adaid), COUNT(adaid), AVG(adaid) FROM ada WHERE adaid = 1';
1921
$stmt = $pdo->query($query, PDO::FETCH_ASSOC);
20-
assertType('PDOStatement<array{max: int<-32768, 32767>|null, min: int<-32768, 32767>|null, count: int, avg: float|null}>', $stmt);
22+
foreach ($stmt as $row) {
23+
assertType('array{max: int<-32768, 32767>|null, min: int<-32768, 32767>|null, count: int, avg: float|null}', $row);
24+
}
2125
}
2226
}

tests/default/data/pdo-prepare.php

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ class Foo
1010
public function prepareSelected(PDO $pdo)
1111
{
1212
$stmt = $pdo->prepare('SELECT email, adaid FROM ada');
13-
assertType('PDOStatement<array{email: string, 0: string, adaid: int<-32768, 32767>, 1: int<-32768, 32767>}>', $stmt);
1413
$stmt->execute();
15-
assertType('PDOStatement<array{email: string, 0: string, adaid: int<-32768, 32767>, 1: int<-32768, 32767>}>', $stmt);
1614

1715
foreach ($stmt as $row) {
16+
assertType('array{email: string, 0: string, adaid: int<-32768, 32767>, 1: int<-32768, 32767>}', $row);
1817
assertType('int<-32768, 32767>', $row['adaid']);
1918
assertType('string', $row['email']);
2019
}
@@ -28,11 +27,17 @@ public function unionParam(PDO $pdo, $adaid, $email)
2827
{
2928
$stmt = $pdo->prepare('SELECT email, adaid FROM ada WHERE adaid = ?');
3029
$stmt->execute([$adaid]);
31-
assertType('PDOStatement<array{email: string, 0: string, adaid: int<-32768, 32767>, 1: int<-32768, 32767>}>', $stmt);
30+
31+
foreach ($stmt as $row) {
32+
assertType('array{email: string, 0: string, adaid: int<-32768, 32767>, 1: int<-32768, 32767>}', $row);
33+
}
3234

3335
$stmt = $pdo->query('SELECT email, adaid FROM ada WHERE email = ?');
3436
$stmt->execute([$email]);
35-
assertType('PDOStatement<array{email: string, 0: string, adaid: int<-32768, 32767>, 1: int<-32768, 32767>}>', $stmt);
37+
38+
foreach ($stmt as $row) {
39+
assertType('array{email: string, 0: string, adaid: int<-32768, 32767>, 1: int<-32768, 32767>}', $row);
40+
}
3641
}
3742

3843
public function queryBranches(PDO $pdo, bool $bool)
@@ -42,32 +47,41 @@ public function queryBranches(PDO $pdo, bool $bool)
4247
} else {
4348
$query = "SELECT email, adaid FROM ada WHERE email='[email protected]'";
4449
}
45-
4650
$stmt = $pdo->prepare($query);
47-
assertType('PDOStatement<array{email: string, 0: string, adaid: int<-32768, 32767>, 1: int<-32768, 32767>}>', $stmt);
51+
52+
foreach ($stmt as $row) {
53+
assertType('array{email: string, 0: string, adaid: int<-32768, 32767>, 1: int<-32768, 32767>}', $row);
54+
}
4855
}
4956

5057
public function placeholderInData(PDO $pdo)
5158
{
5259
$query = "SELECT adaid FROM ada WHERE email LIKE 'hello?%'";
5360
$stmt = $pdo->prepare($query);
54-
assertType('PDOStatement<array{adaid: int<-32768, 32767>, 0: int<-32768, 32767>}>', $stmt);
5561
$stmt->execute();
56-
assertType('PDOStatement<array{adaid: int<-32768, 32767>, 0: int<-32768, 32767>}>', $stmt);
62+
63+
foreach ($stmt as $row) {
64+
assertType('array{adaid: int<-32768, 32767>, 0: int<-32768, 32767>}', $row);
65+
}
5766

5867
$query = "SELECT adaid FROM ada WHERE email LIKE '%questions ?%'";
5968
$stmt = $pdo->prepare($query);
60-
assertType('PDOStatement<array{adaid: int<-32768, 32767>, 0: int<-32768, 32767>}>', $stmt);
6169
$stmt->execute();
62-
assertType('PDOStatement<array{adaid: int<-32768, 32767>, 0: int<-32768, 32767>}>', $stmt);
70+
71+
foreach ($stmt as $row) {
72+
assertType('array{adaid: int<-32768, 32767>, 0: int<-32768, 32767>}', $row);
73+
}
6374
}
6475

6576
public function arrayParam(PDO $pdo)
6677
{
6778
$query = 'SELECT adaid FROM ada WHERE adaid IN (:adaids)';
6879
$stmt = $pdo->prepare($query);
6980
$stmt->execute(['adaids' => [1, 2, 3]]);
70-
assertType('PDOStatement<array{adaid: int<-32768, 32767>, 0: int<-32768, 32767>}>', $stmt);
81+
82+
foreach ($stmt as $row) {
83+
assertType('array{adaid: int<-32768, 32767>, 0: int<-32768, 32767>}', $row);
84+
}
7185
}
7286

7387
public function unspecifiedArray(PDO $pdo, array $idsToUpdate, string $time)
@@ -78,7 +92,10 @@ public function unspecifiedArray(PDO $pdo, array $idsToUpdate, string $time)
7892
'ids' => $idsToUpdate,
7993
'time' => $time,
8094
]);
81-
assertType('PDOStatement<array{adaid: int<-32768, 32767>, 0: int<-32768, 32767>}>', $stmt);
95+
96+
foreach ($stmt as $row) {
97+
assertType('array{adaid: int<-32768, 32767>, 0: int<-32768, 32767>}', $row);
98+
}
8299
}
83100

84101
/**
@@ -92,7 +109,10 @@ public function unspecifiedList(PDO $pdo, array $idsToUpdate, string $time)
92109
'ids' => $idsToUpdate,
93110
'time' => $time,
94111
]);
95-
assertType('PDOStatement<array{adaid: int<-32768, 32767>, 0: int<-32768, 32767>}>', $stmt);
112+
113+
foreach ($stmt as $row) {
114+
assertType('array{adaid: int<-32768, 32767>, 0: int<-32768, 32767>}', $row);
115+
}
96116
}
97117

98118
/**
@@ -106,7 +126,10 @@ public function specifiedList(PDO $pdo, array $idsToUpdate, string $time)
106126
'ids' => $idsToUpdate,
107127
'time' => $time,
108128
]);
109-
assertType('PDOStatement<array{adaid: int<-32768, 32767>, 0: int<-32768, 32767>}>', $stmt);
129+
130+
foreach ($stmt as $row) {
131+
assertType('array{adaid: int<-32768, 32767>, 0: int<-32768, 32767>}', $row);
132+
}
110133
}
111134

112135
/**
@@ -119,7 +142,10 @@ public function numberType(PDO $pdo, $idsToUpdate)
119142
$stmt->execute([
120143
'ids' => $idsToUpdate,
121144
]);
122-
assertType('PDOStatement<array{adaid: int<-32768, 32767>, 0: int<-32768, 32767>}>', $stmt);
145+
146+
foreach ($stmt as $row) {
147+
assertType('array{adaid: int<-32768, 32767>, 0: int<-32768, 32767>}', $row);
148+
}
123149
}
124150

125151
/**
@@ -133,7 +159,10 @@ public function specifiedArray(PDO $pdo, array $idsToUpdate, string $time)
133159
'ids' => $idsToUpdate,
134160
'time' => $time,
135161
]);
136-
assertType('PDOStatement<array{adaid: int<-32768, 32767>, 0: int<-32768, 32767>}>', $stmt);
162+
163+
foreach ($stmt as $row) {
164+
assertType('array{adaid: int<-32768, 32767>, 0: int<-32768, 32767>}', $row);
165+
}
137166
}
138167

139168
/**
@@ -147,7 +176,10 @@ public function specifiedIterable(PDO $pdo, iterable $idsToUpdate, string $time)
147176
'ids' => $idsToUpdate,
148177
'time' => $time,
149178
]);
150-
assertType('PDOStatement<array{adaid: int<-32768, 32767>, 0: int<-32768, 32767>}>', $stmt);
179+
180+
foreach ($stmt as $row) {
181+
assertType('array{adaid: int<-32768, 32767>, 0: int<-32768, 32767>}', $row);
182+
}
151183
}
152184

153185
public function noInferenceOnBug196(PDO $pdo, array $minorPhpVersions, \DateTimeImmutable $updateDate)
@@ -161,8 +193,11 @@ public function noInferenceOnBug196(PDO $pdo, array $minorPhpVersions, \DateTime
161193
'SELECT '.implode(', ', $sumQueries).' FROM ada WHERE adaid = :package'
162194
);
163195
$stmt->execute(['package' => 'abc']);
196+
164197
// this query is too dynamic for being analyzed.
165198
// make sure we don't infer a wrong type.
166-
assertType('PDOStatement', $stmt);
199+
foreach ($stmt as $row) {
200+
assertType('mixed', $row);
201+
}
167202
}
168203
}

0 commit comments

Comments
 (0)