Skip to content

Platform test: compare inferred types with real SQL engine results #572

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions .github/workflows/platform-matrix-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# https://help.github.com/en/categories/automating-your-workflow-with-github-actions

name: "Platform matrix test"

on:
pull_request:
push:
branches:
- "1.4.x"

jobs:
tests:
name: "Platform matrix test"
runs-on: "ubuntu-latest"
env:
MYSQL_HOST: '127.0.0.1'
PGSQL_HOST: '127.0.0.1'

strategy:
fail-fast: false
matrix:
php-version:
- "8.0"
- "8.1"

steps:
- name: "Checkout"
uses: actions/checkout@v4

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
coverage: "none"
php-version: "${{ matrix.php-version }}"
ini-file: development
extensions: pdo, mysqli, pgsql, pdo_mysql, pdo_pgsql, pdo_sqlite, mongodb

- name: "Install dependencies"
run: "composer install --no-interaction --no-progress"

- name: "Run platform matrix test"
run: vendor/bin/phpunit --group=platform

services:
postgres:
image: "postgres:latest"
env:
POSTGRES_PASSWORD: "secret"
POSTGRES_USER: root
POSTGRES_DB: foo
ports:
- "5432:5432"

mysql:
image: "mysql:latest"
env:
MYSQL_ALLOW_EMPTY_PASSWORD: yes
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: foo
ports:
- "3306:3306"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
/build-cs
/vendor
/composer.lock
/.env
.phpunit.result.cache
6 changes: 6 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,9 @@ parameters:
message: '#^Call to function method_exists\(\) with ''Doctrine\\\\ORM\\\\EntityManager'' and ''create'' will always evaluate to true\.$#'
path: src/Doctrine/Mapping/ClassMetadataFactory.php
reportUnmatched: false
-
messages:
- '#^Call to function method_exists\(\) with Doctrine\\DBAL\\Connection and ''getNativeConnection'' will always evaluate to true\.$#'
- '#^Cannot call method getWrappedResourceHandle\(\) on class\-string\|object\.$#'
path: tests/Platform/QueryResultTypeWalkerFetchTypeMatrixTest.php
reportUnmatched: false
6 changes: 6 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,11 @@
</testsuite>
</testsuites>

<groups>
<exclude>
<group>platform</group>
</exclude>
</groups>

<logging/>
</phpunit>
3 changes: 2 additions & 1 deletion src/Type/Doctrine/Descriptors/BooleanType.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public function getDatabaseInternalType(): Type
{
return TypeCombinator::union(
new ConstantIntegerType(0),
new ConstantIntegerType(1)
new ConstantIntegerType(1),
new \PHPStan\Type\BooleanType()
);
}

Expand Down
8 changes: 6 additions & 2 deletions src/Type/Doctrine/Query/QueryResultTypeWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Doctrine\ORM\Query\SqlWalker;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantFloatType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
Expand Down Expand Up @@ -1113,8 +1114,11 @@ public function walkLiteral($literal): string
break;

case AST\Literal::BOOLEAN:
$value = strtolower($literal->value) === 'true' ? 1 : 0;
$type = new ConstantIntegerType($value);
$value = strtolower($literal->value) === 'true';
$type = TypeCombinator::union(
new ConstantIntegerType($value ? 1 : 0),
new ConstantBooleanType($value)
);
break;

case AST\Literal::NUMERIC:
Expand Down
56 changes: 56 additions & 0 deletions tests/Platform/MatrixEntity/TestEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php declare(strict_types = 1);

namespace PHPStan\Platform\MatrixEntity;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Table(name="test")
* @ORM\Entity
*/
class TestEntity
{

/**
* @ORM\Id
* @ORM\Column(type="string", name="col_string", nullable=false)
* @var string
*/
public $col_string;

/**
* @ORM\Id
* @ORM\Column(type="boolean", name="col_bool", nullable=false)
* @var bool
*/
public $col_bool;

/**
* @ORM\Id
* @ORM\Column(type="float", name="col_float", nullable=false)
* @var float
*/
public $col_float;

/**
* @ORM\Id
* @ORM\Column(type="decimal", name="col_decimal", nullable=false, scale=1, precision=2)
* @var string
*/
public $col_decimal;

/**
* @ORM\Id
* @ORM\Column(type="integer", name="col_int", nullable=false)
* @var int
*/
public $col_int;

/**
* @ORM\Id
* @ORM\Column(type="bigint", name="col_bigint", nullable=false)
* @var string
*/
public $col_bigint;

}
Loading