Skip to content

Commit dd71401

Browse files
authored
Platform test: compare inferred types with real SQL engine results
1 parent 6a25c9d commit dd71401

15 files changed

+660
-11
lines changed

Diff for: .github/workflows/platform-matrix-test.yml

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# https://help.github.com/en/categories/automating-your-workflow-with-github-actions
2+
3+
name: "Platform matrix test"
4+
5+
on:
6+
pull_request:
7+
push:
8+
branches:
9+
- "1.4.x"
10+
11+
jobs:
12+
tests:
13+
name: "Platform matrix test"
14+
runs-on: "ubuntu-latest"
15+
env:
16+
MYSQL_HOST: '127.0.0.1'
17+
PGSQL_HOST: '127.0.0.1'
18+
19+
strategy:
20+
fail-fast: false
21+
matrix:
22+
php-version:
23+
- "8.0"
24+
- "8.1"
25+
26+
steps:
27+
- name: "Checkout"
28+
uses: actions/checkout@v4
29+
30+
- name: "Install PHP"
31+
uses: "shivammathur/setup-php@v2"
32+
with:
33+
coverage: "none"
34+
php-version: "${{ matrix.php-version }}"
35+
ini-file: development
36+
extensions: pdo, mysqli, pgsql, pdo_mysql, pdo_pgsql, pdo_sqlite, mongodb
37+
38+
- name: "Install dependencies"
39+
run: "composer install --no-interaction --no-progress"
40+
41+
- name: "Run platform matrix test"
42+
run: vendor/bin/phpunit --group=platform
43+
44+
services:
45+
postgres:
46+
image: "postgres:latest"
47+
env:
48+
POSTGRES_PASSWORD: "secret"
49+
POSTGRES_USER: root
50+
POSTGRES_DB: foo
51+
ports:
52+
- "5432:5432"
53+
54+
mysql:
55+
image: "mysql:latest"
56+
env:
57+
MYSQL_ALLOW_EMPTY_PASSWORD: yes
58+
MYSQL_ROOT_PASSWORD: secret
59+
MYSQL_DATABASE: foo
60+
ports:
61+
- "3306:3306"

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
/build-cs
33
/vendor
44
/composer.lock
5+
/.env
56
.phpunit.result.cache

Diff for: phpstan.neon

+6
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,9 @@ parameters:
4343
message: '#^Call to function method_exists\(\) with ''Doctrine\\\\ORM\\\\EntityManager'' and ''create'' will always evaluate to true\.$#'
4444
path: src/Doctrine/Mapping/ClassMetadataFactory.php
4545
reportUnmatched: false
46+
-
47+
messages:
48+
- '#^Call to function method_exists\(\) with Doctrine\\DBAL\\Connection and ''getNativeConnection'' will always evaluate to true\.$#'
49+
- '#^Cannot call method getWrappedResourceHandle\(\) on class\-string\|object\.$#'
50+
path: tests/Platform/QueryResultTypeWalkerFetchTypeMatrixTest.php
51+
reportUnmatched: false

Diff for: phpunit.xml

+6
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,11 @@
3636
</testsuite>
3737
</testsuites>
3838

39+
<groups>
40+
<exclude>
41+
<group>platform</group>
42+
</exclude>
43+
</groups>
44+
3945
<logging/>
4046
</phpunit>

Diff for: src/Type/Doctrine/Descriptors/BooleanType.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public function getDatabaseInternalType(): Type
2828
{
2929
return TypeCombinator::union(
3030
new ConstantIntegerType(0),
31-
new ConstantIntegerType(1)
31+
new ConstantIntegerType(1),
32+
new \PHPStan\Type\BooleanType()
3233
);
3334
}
3435

Diff for: src/Type/Doctrine/Query/QueryResultTypeWalker.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Doctrine\ORM\Query\SqlWalker;
1515
use PHPStan\ShouldNotHappenException;
1616
use PHPStan\Type\BooleanType;
17+
use PHPStan\Type\Constant\ConstantBooleanType;
1718
use PHPStan\Type\Constant\ConstantFloatType;
1819
use PHPStan\Type\Constant\ConstantIntegerType;
1920
use PHPStan\Type\Constant\ConstantStringType;
@@ -1113,8 +1114,11 @@ public function walkLiteral($literal): string
11131114
break;
11141115

11151116
case AST\Literal::BOOLEAN:
1116-
$value = strtolower($literal->value) === 'true' ? 1 : 0;
1117-
$type = new ConstantIntegerType($value);
1117+
$value = strtolower($literal->value) === 'true';
1118+
$type = TypeCombinator::union(
1119+
new ConstantIntegerType($value ? 1 : 0),
1120+
new ConstantBooleanType($value)
1121+
);
11181122
break;
11191123

11201124
case AST\Literal::NUMERIC:

Diff for: tests/Platform/MatrixEntity/TestEntity.php

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Platform\MatrixEntity;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
7+
/**
8+
* @ORM\Table(name="test")
9+
* @ORM\Entity
10+
*/
11+
class TestEntity
12+
{
13+
14+
/**
15+
* @ORM\Id
16+
* @ORM\Column(type="string", name="col_string", nullable=false)
17+
* @var string
18+
*/
19+
public $col_string;
20+
21+
/**
22+
* @ORM\Id
23+
* @ORM\Column(type="boolean", name="col_bool", nullable=false)
24+
* @var bool
25+
*/
26+
public $col_bool;
27+
28+
/**
29+
* @ORM\Id
30+
* @ORM\Column(type="float", name="col_float", nullable=false)
31+
* @var float
32+
*/
33+
public $col_float;
34+
35+
/**
36+
* @ORM\Id
37+
* @ORM\Column(type="decimal", name="col_decimal", nullable=false, scale=1, precision=2)
38+
* @var string
39+
*/
40+
public $col_decimal;
41+
42+
/**
43+
* @ORM\Id
44+
* @ORM\Column(type="integer", name="col_int", nullable=false)
45+
* @var int
46+
*/
47+
public $col_int;
48+
49+
/**
50+
* @ORM\Id
51+
* @ORM\Column(type="bigint", name="col_bigint", nullable=false)
52+
* @var string
53+
*/
54+
public $col_bigint;
55+
56+
}

0 commit comments

Comments
 (0)