1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

#10026 adapted DateTime tests for DateMalformedStringException in PHP 8.3

This commit is contained in:
Thomas Bley 2023-07-19 23:52:25 +02:00
parent 804087b5d5
commit cbee1e094e
3 changed files with 72 additions and 44 deletions

View File

@ -70,9 +70,9 @@ jobs:
echo "chunks=$(php -r 'echo json_encode(range(1, ${{ env.CHUNK_COUNT }} ));')" >> $GITHUB_OUTPUT echo "chunks=$(php -r 'echo json_encode(range(1, ${{ env.CHUNK_COUNT }} ));')" >> $GITHUB_OUTPUT
tests: tests:
name: "Tests - ${{ matrix.php-version }} ${{ matrix.chunk }}/${{ matrix.count }} ${{ matrix.operating-system }}" name: "Tests - PHP ${{ matrix.php-version }} ${{ matrix.chunk }}/${{ matrix.count }}"
runs-on: ${{ matrix.operating-system }} runs-on: ubuntu-latest
needs: needs:
- chunk-matrix - chunk-matrix
@ -81,11 +81,11 @@ jobs:
fail-fast: false fail-fast: false
matrix: matrix:
php-version: php-version:
- "7.4"
- "8.0" - "8.0"
- "8.1" - "8.1"
- "8.2" - "8.2"
- "8.3" - "8.3"
operating-system: [ubuntu-latest]
count: ${{ fromJson(needs.chunk-matrix.outputs.count) }} count: ${{ fromJson(needs.chunk-matrix.outputs.count) }}
chunk: ${{ fromJson(needs.chunk-matrix.outputs.chunks) }} chunk: ${{ fromJson(needs.chunk-matrix.outputs.chunks) }}

View File

@ -2,12 +2,79 @@
namespace Psalm\Tests; namespace Psalm\Tests;
use Psalm\Context;
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait; use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
class DateTimeTest extends TestCase class DateTimeTest extends TestCase
{ {
use ValidCodeAnalysisTestTrait; use ValidCodeAnalysisTestTrait;
public function testModifyWithInvalidConstant(): void
{
$context = new Context();
if (version_compare(PHP_VERSION, '8.3', '>')) {
$this->expectException(\DateMalformedStringException::class);
$this->expectExceptionMessage('DateTime::modify(): Failed to parse time string (foo) at position 0 (f)');
}
$this->addFile(
'somefile.php',
'<?php
/**
* @return "foo"|"bar"
*/
function getString(): string
{
return "foo";
}
$datetime = new DateTime();
$dateTimeImmutable = new DateTimeImmutable();
$a = $datetime->modify(getString());
$b = $dateTimeImmutable->modify(getString());',
);
$this->analyzeFile('somefile.php', $context);
$this->assertSame('false', $context->vars_in_scope['$a']->getId(true));
$this->assertSame('false', $context->vars_in_scope['$b']->getId(true));
}
public function testModifyWithBothConstant(): void
{
$context = new Context();
if (version_compare(PHP_VERSION, '8.3', '>')) {
$this->expectException(\DateMalformedStringException::class);
$this->expectExceptionMessage('DateTime::modify(): Failed to parse time string (bar) at position 0 (b)');
}
$this->addFile(
'somefile.php',
'<?php
/**
* @return "+1 day"|"bar"
*/
function getString(): string
{
return "+1 day";
}
$datetime = new DateTime();
$dateTimeImmutable = new DateTimeImmutable();
$a = $datetime->modify(getString());
$b = $dateTimeImmutable->modify(getString());',
);
$this->analyzeFile('somefile.php', $context);
$this->assertSame('DateTime|false', $context->vars_in_scope['$a']->getId(false));
$this->assertSame('DateTimeImmutable|false', $context->vars_in_scope['$b']->getId(false));
}
public function providerValidCodeParse(): iterable public function providerValidCodeParse(): iterable
{ {
return [ return [
@ -48,46 +115,6 @@ class DateTimeTest extends TestCase
'$b' => 'DateTimeImmutable', '$b' => 'DateTimeImmutable',
], ],
], ],
'modifyWithInvalidConstant' => [
'code' => '<?php
/**
* @return "foo"|"bar"
*/
function getString(): string
{
return "foo";
}
$datetime = new DateTime();
$dateTimeImmutable = new DateTimeImmutable();
$a = $datetime->modify(getString());
$b = $dateTimeImmutable->modify(getString());
',
'assertions' => [
'$a' => 'false',
'$b' => 'false',
],
],
'modifyWithBothConstant' => [
'code' => '<?php
/**
* @return "+1 day"|"bar"
*/
function getString(): string
{
return "+1 day";
}
$datetime = new DateTime();
$dateTimeImmutable = new DateTimeImmutable();
$a = $datetime->modify(getString());
$b = $dateTimeImmutable->modify(getString());
',
'assertions' => [
'$a' => 'DateTime|false',
'$b' => 'DateTimeImmutable|false',
],
],
'otherMethodAfterModify' => [ 'otherMethodAfterModify' => [
'code' => '<?php 'code' => '<?php
$datetime = new DateTime(); $datetime = new DateTime();

View File

@ -76,7 +76,8 @@ trait ValidCodeAnalysisTestTrait
$codebase->enterServerMode(); $codebase->enterServerMode();
$codebase->config->visitPreloadedStubFiles($codebase); $codebase->config->visitPreloadedStubFiles($codebase);
if (version_compare(PHP_VERSION, '8.2.0', '>=')) { // avoid MethodSignatureMismatch for __unserialize/() when extending DateTime
if (version_compare(PHP_VERSION, '8.2', '>')) {
$this->addStubFile( $this->addStubFile(
'stubOne.phpstub', 'stubOne.phpstub',
'<?php '<?php