Skip to content

Commit 0f5e82f

Browse files
janedbalondrejmirtes
authored andcommitted
Fix test in dbal4
1 parent 4f2113a commit 0f5e82f

File tree

2 files changed

+45
-13
lines changed

2 files changed

+45
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,67 @@
11
<?php declare(strict_types = 1);
22

3-
namespace PHPStan\Platform\MatrixEntity;
3+
namespace PHPStan\Platform\Entity;
44

55
use Doctrine\ORM\Mapping as ORM;
66

77
/**
88
* @ORM\Table(name="test")
99
* @ORM\Entity
1010
*/
11-
class TestEntity
11+
#[ORM\Table(name: 'test')]
12+
#[ORM\Entity]
13+
class PlatformEntity
1214
{
1315

1416
/**
1517
* @ORM\Id
18+
* @ORM\Column(type="string",nullable=false)
19+
* @var string
20+
*/
21+
#[ORM\Id]
22+
#[ORM\Column(type: 'string', nullable: false)]
23+
public $id;
24+
25+
/**
1626
* @ORM\Column(type="string", name="col_string", nullable=false)
1727
* @var string
1828
*/
29+
#[ORM\Column(type: 'string', name: 'col_string', nullable: false)]
1930
public $col_string;
2031

2132
/**
22-
* @ORM\Id
2333
* @ORM\Column(type="boolean", name="col_bool", nullable=false)
2434
* @var bool
2535
*/
36+
#[ORM\Column(type: 'boolean', name: 'col_bool', nullable: false)]
2637
public $col_bool;
2738

2839
/**
29-
* @ORM\Id
3040
* @ORM\Column(type="float", name="col_float", nullable=false)
3141
* @var float
3242
*/
43+
#[ORM\Column(type: 'float', name: 'col_float', nullable: false)]
3344
public $col_float;
3445

3546
/**
36-
* @ORM\Id
3747
* @ORM\Column(type="decimal", name="col_decimal", nullable=false, scale=1, precision=2)
3848
* @var string
3949
*/
50+
#[ORM\Column(type: 'decimal', name: 'col_decimal', nullable: false, scale: 1, precision: 2)]
4051
public $col_decimal;
4152

4253
/**
43-
* @ORM\Id
4454
* @ORM\Column(type="integer", name="col_int", nullable=false)
4555
* @var int
4656
*/
57+
#[ORM\Column(type: 'integer', name: 'col_int', nullable: false)]
4758
public $col_int;
4859

4960
/**
50-
* @ORM\Id
5161
* @ORM\Column(type="bigint", name="col_bigint", nullable=false)
52-
* @var string
62+
* @var int|string
5363
*/
64+
#[ORM\Column(type: 'bigint', name: 'col_bigint', nullable: false)]
5465
public $col_bigint;
5566

5667
}

tests/Platform/QueryResultTypeWalkerFetchTypeMatrixTest.php

+26-5
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,21 @@
33
namespace PHPStan\Platform;
44

55
use Cache\Adapter\PHPArray\ArrayCachePool;
6+
use Composer\InstalledVersions;
7+
use Composer\Semver\VersionParser;
68
use Doctrine\Common\Annotations\AnnotationReader;
79
use Doctrine\DBAL\Connection;
810
use Doctrine\DBAL\DriverManager;
911
use Doctrine\DBAL\Exception as DbalException;
1012
use Doctrine\ORM\Configuration;
1113
use Doctrine\ORM\EntityManager;
1214
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
15+
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
1316
use Doctrine\ORM\Tools\SchemaTool;
1417
use LogicException;
1518
use mysqli;
1619
use PDO;
17-
use PHPStan\Platform\MatrixEntity\TestEntity;
20+
use PHPStan\Platform\Entity\PlatformEntity;
1821
use PHPStan\Testing\PHPStanTestCase;
1922
use PHPStan\Type\ConstantTypeHelper;
2023
use PHPStan\Type\Doctrine\DescriptorRegistry;
@@ -25,7 +28,9 @@
2528
use SQLite3;
2629
use function array_column;
2730
use function array_combine;
31+
use function array_fill;
2832
use function array_keys;
33+
use function class_exists;
2934
use function function_exists;
3035
use function get_debug_type;
3136
use function getenv;
@@ -89,7 +94,13 @@ public function testFetchedTypes(
8994
$config->setAutoGenerateProxyClasses(false);
9095
$config->setSecondLevelCacheEnabled(false);
9196
$config->setMetadataCache(new ArrayCachePool());
92-
$config->setMetadataDriverImpl(new AnnotationDriver(new AnnotationReader(), [__DIR__ . '/MatrixEntity']));
97+
98+
if (InstalledVersions::satisfies(new VersionParser(), 'doctrine/orm', '3.*')) {
99+
$config->setMetadataDriverImpl(new AttributeDriver([__DIR__ . '/Entity']));
100+
} else {
101+
$config->setMetadataDriverImpl(new AnnotationDriver(new AnnotationReader(), [__DIR__ . '/Entity']));
102+
}
103+
93104
$entityManager = new EntityManager($connection, $config);
94105

95106
} catch (DbalException $e) {
@@ -104,7 +115,8 @@ public function testFetchedTypes(
104115
$schemaTool->dropSchema($classes);
105116
$schemaTool->createSchema($classes);
106117

107-
$entity = new TestEntity();
118+
$entity = new PlatformEntity();
119+
$entity->id = '1';
108120
$entity->col_bool = true;
109121
$entity->col_float = 0.125;
110122
$entity->col_decimal = '0.1';
@@ -125,7 +137,7 @@ public function testFetchedTypes(
125137
if ($expectedType === null) {
126138
continue; // e.g. no such function
127139
}
128-
$dql = sprintf($columnsQueryTemplate, $select, TestEntity::class);
140+
$dql = sprintf($columnsQueryTemplate, $select, PlatformEntity::class);
129141

130142
$query = $entityManager->createQuery($dql);
131143
$result = $query->getSingleResult();
@@ -214,7 +226,7 @@ public function provideCases(): iterable
214226
'1' => ['int', 'int', 'int', 'int', 'string', 'string'],
215227
'2147483648' => ['int', 'int', 'int', 'int', 'string', 'string'],
216228
't.col_int' => ['int', 'int', 'int', 'int', 'int', 'int'],
217-
't.col_bigint' => ['string', 'string', 'string', 'string', 'string', 'string'],
229+
't.col_bigint' => self::hasDbal4() ? array_fill(0, 6, 'int') : array_fill(0, 6, 'string'),
218230
'SUM(t.col_int)' => ['string', 'int', 'int', 'int', 'string', 'string'],
219231
'SUM(t.col_bigint)' => ['string', 'int', 'string', 'string', 'string', 'string'],
220232
"LENGTH('')" => ['int', 'int', 'int', 'int', 'int', 'int'],
@@ -408,4 +420,13 @@ private function getNativeConnection(Connection $connection)
408420
throw new LogicException('Unable to get native connection');
409421
}
410422

423+
private static function hasDbal4(): bool
424+
{
425+
if (!class_exists(InstalledVersions::class)) {
426+
return false;
427+
}
428+
429+
return InstalledVersions::satisfies(new VersionParser(), 'doctrine/dbal', '4.*');
430+
}
431+
411432
}

0 commit comments

Comments
 (0)