Skip to content

Commit

Permalink
Merge pull request #149 from gsteel/8.2-fwd-compat
Browse files Browse the repository at this point in the history
Fix some 8.2 compatibility issues in `Date` validator
  • Loading branch information
Ocramius committed Sep 13, 2022
2 parents 3ebb924 + 08dd703 commit b0e8bc7
Show file tree
Hide file tree
Showing 16 changed files with 106 additions and 100 deletions.
39 changes: 5 additions & 34 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.24.0@06dd975cb55d36af80f242561738f16c5f58264f">
<files psalm-version="4.26.0@6998fabb2bf528b65777bf9941920888d23c03ac">
<file src="bin/update_hostname_validator.php">
<MissingClosureParamType occurrences="2">
<code>$domain</code>
Expand Down Expand Up @@ -579,6 +579,9 @@
<PossiblyUndefinedVariable occurrences="1">
<code>$temp</code>
</PossiblyUndefinedVariable>
<TypeDoesNotContainType occurrences="1">
<code>$errors === false</code>
</TypeDoesNotContainType>
</file>
<file src="src/DateStep.php">
<ArgumentTypeCoercion occurrences="1">
Expand Down Expand Up @@ -1431,7 +1434,7 @@
</PossiblyNullArgument>
<RedundantConditionGivenDocblockType occurrences="3">
<code>$file !== null</code>
<code>is_array($files)</code>
<code>is_countable($files)</code>
</RedundantConditionGivenDocblockType>
</file>
<file src="src/File/UploadFile.php">
Expand Down Expand Up @@ -2045,9 +2048,6 @@
<code>[$this, 'errorHandlerIgnore']</code>
<code>[$this, 'errorHandlerIgnore']</code>
</InvalidArgument>
<InvalidScalarArgument occurrences="1">
<code>1</code>
</InvalidScalarArgument>
<MixedArgument occurrences="2">
<code>$message</code>
<code>$options</code>
Expand All @@ -2059,9 +2059,6 @@
<code>null</code>
<code>null</code>
</NullArgument>
<PossiblyNullArgument occurrences="1">
<code>var_export($messages, 1)</code>
</PossiblyNullArgument>
</file>
<file src="test/BarcodeTest.php">
<ArgumentTypeCoercion occurrences="1">
Expand Down Expand Up @@ -2167,9 +2164,6 @@
<code>'users'</code>
<code>'users'</code>
</InvalidArgument>
<UndefinedPropertyAssignment occurrences="1">
<code>$mockHasResultRow-&gt;one</code>
</UndefinedPropertyAssignment>
</file>
<file src="test/Db/RecordExistsTest.php">
<DeprecatedMethod occurrences="2">
Expand All @@ -2188,9 +2182,6 @@
<PossiblyNullReference occurrences="1">
<code>$parameters</code>
</PossiblyNullReference>
<UndefinedPropertyAssignment occurrences="1">
<code>$mockHasResultRow-&gt;one</code>
</UndefinedPropertyAssignment>
</file>
<file src="test/Db/TestAsset/ConcreteDbValidator.php">
<PropertyNotSetInConstructor occurrences="2">
Expand Down Expand Up @@ -2684,31 +2675,11 @@
<ArgumentTypeCoercion occurrences="1">
<code>$classOrInstance</code>
</ArgumentTypeCoercion>
<InvalidPropertyAssignmentValue occurrences="1"/>
<MixedArgument occurrences="2"/>
<MixedInferredReturnType occurrences="2">
<code>array</code>
<code>array</code>
</MixedInferredReturnType>
<MixedMethodCall occurrences="6">
<code>will</code>
<code>will</code>
<code>willReturn</code>
<code>willReturn</code>
<code>willReturnCallback</code>
<code>willReturnCallback</code>
</MixedMethodCall>
<PossiblyNullPropertyAssignmentValue occurrences="1">
<code>null</code>
</PossiblyNullPropertyAssignmentValue>
<UndefinedInterfaceMethod occurrences="6">
<code>method</code>
<code>method</code>
<code>method</code>
<code>method</code>
<code>method</code>
<code>method</code>
</UndefinedInterfaceMethod>
</file>
<file src="test/UriTest.php">
<InvalidArgument occurrences="4">
Expand Down
4 changes: 4 additions & 0 deletions src/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ protected function convertString($value, $addErrors = true)
// Invalid dates can show up as warnings (ie. "2007-02-99")
// and still return a DateTime object.
$errors = DateTime::getLastErrors();
if ($errors === false) {
return $date;
}

if ($errors['warning_count'] > 0) {
if ($addErrors) {
$this->error(self::FALSEFORMAT);
Expand Down
14 changes: 9 additions & 5 deletions test/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function testSetFormatIgnoresNull(): void
/**
* @return array[]
* @psalm-return array<array{
* 0: string|numeric|DateTime|object|array,
* 0: string|numeric|DateTime|object|array|null,
* 1: null|string,
* 2: bool,
* 3: bool
Expand Down Expand Up @@ -97,31 +97,35 @@ public function datesDataProvider(): array
[new DateTime(), null, true, false],
// invalid obj
[new stdClass(), null, false, false],
// Empty Values
[[], null, false, false],
['', null, false, false],
[null, null, false, false],
];
}

/**
* Ensures that the validator follows expected behavior
*
* @dataProvider datesDataProvider
* @param string|numeric|DateTime|object|array $input
* @param string|numeric|DateTime|object|array|null $input
*/
public function testBasic($input, ?string $format, bool $result, bool $resultStrict): void
{
$this->validator->setFormat($format);
/** @psalm-suppress ArgumentTypeCoercion */
/** @psalm-suppress ArgumentTypeCoercion, PossiblyNullArgument */
$this->assertEquals($result, $this->validator->isValid($input));
}

/**
* @dataProvider datesDataProvider
* @param string|numeric|DateTime|object|array $input
* @param string|numeric|DateTime|object|array|null $input
*/
public function testBasicStrictMode($input, ?string $format, bool $result, bool $resultStrict): void
{
$this->validator->setStrict(true);
$this->validator->setFormat($format);
/** @psalm-suppress ArgumentTypeCoercion */
/** @psalm-suppress ArgumentTypeCoercion, PossiblyNullArgument */
$this->assertSame($resultStrict, $this->validator->isValid($input));
}

Expand Down
5 changes: 4 additions & 1 deletion test/Db/NoRecordExistsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ protected function getMockHasResult()
$mockConnection = $this->createMock(ConnectionInterface::class);

// Mock has result
$mockHasResultRow = new ArrayObject();
$mockHasResultRow = new class extends ArrayObject {
public ?string $one = null;
};

$mockHasResultRow->one = 'one';

$mockHasResult = $this->createMock(ResultInterface::class);
Expand Down
5 changes: 4 additions & 1 deletion test/Db/RecordExistsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ protected function getMockHasResult()
$mockConnection = $this->createMock(ConnectionInterface::class);

// Mock has result
$mockHasResultRow = new ArrayObject();
$mockHasResultRow = new class extends ArrayObject {
public ?string $one = null;
};

$mockHasResultRow->one = 'one';

$mockHasResult = $this->createMock(ResultInterface::class);
Expand Down
14 changes: 8 additions & 6 deletions test/File/Crc32Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,14 @@ public function testBasic($options, $isValidParam, bool $expected, string $messa
*/
public function testLegacy($options, $isValidParam, bool $expected, string $messageKey): void
{
if (is_array($isValidParam)) {
$validator = new File\Crc32($options);
$this->assertEquals($expected, $validator->isValid($isValidParam['tmp_name'], $isValidParam));
if (! $expected) {
$this->assertArrayHasKey($messageKey, $validator->getMessages());
}
if (! is_array($isValidParam)) {
$this->markTestSkipped('An array is expected for legacy compat tests');
}

$validator = new File\Crc32($options);
$this->assertEquals($expected, $validator->isValid($isValidParam['tmp_name'], $isValidParam));
if (! $expected) {
$this->assertArrayHasKey($messageKey, $validator->getMessages());
}
}

Expand Down
14 changes: 8 additions & 6 deletions test/File/ExcludeExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,14 @@ public function testBasic($options, $isValidParam, bool $expected, string $messa
*/
public function testLegacy($options, $isValidParam, bool $expected, string $messageKey): void
{
if (is_array($isValidParam)) {
$validator = new File\ExcludeExtension($options);
$this->assertEquals($expected, $validator->isValid($isValidParam['tmp_name'], $isValidParam));
if (! $expected) {
$this->assertArrayHasKey($messageKey, $validator->getMessages());
}
if (! is_array($isValidParam)) {
$this->markTestSkipped('An array is expected for legacy compat tests');
}

$validator = new File\ExcludeExtension($options);
$this->assertEquals($expected, $validator->isValid($isValidParam['tmp_name'], $isValidParam));
if (! $expected) {
$this->assertArrayHasKey($messageKey, $validator->getMessages());
}
}

Expand Down
8 changes: 5 additions & 3 deletions test/File/ExistsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,12 @@ public function testBasic(string $options, $isValidParam, bool $expected): void
*/
public function testLegacy(string $options, $isValidParam, bool $expected): void
{
if (is_array($isValidParam)) {
$validator = new File\Exists($options);
$this->assertEquals($expected, $validator->isValid($isValidParam['tmp_name'], $isValidParam));
if (! is_array($isValidParam)) {
$this->markTestSkipped('An array is expected for legacy compat tests');
}

$validator = new File\Exists($options);
$this->assertEquals($expected, $validator->isValid($isValidParam['tmp_name'], $isValidParam));
}

/**
Expand Down
14 changes: 8 additions & 6 deletions test/File/ExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,14 @@ public function testBasic($options, $isValidParam, bool $expected, string $messa
*/
public function testLegacy($options, $isValidParam, bool $expected, string $messageKey): void
{
if (is_array($isValidParam)) {
$validator = new File\Extension($options);
$this->assertEquals($expected, $validator->isValid($isValidParam['tmp_name'], $isValidParam));
if (! $expected) {
$this->assertArrayHasKey($messageKey, $validator->getMessages());
}
if (! is_array($isValidParam)) {
$this->markTestSkipped('An array is expected for legacy compat tests');
}

$validator = new File\Extension($options);
$this->assertEquals($expected, $validator->isValid($isValidParam['tmp_name'], $isValidParam));
if (! $expected) {
$this->assertArrayHasKey($messageKey, $validator->getMessages());
}
}

Expand Down
14 changes: 8 additions & 6 deletions test/File/HashTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,14 @@ public function testBasic($options, $isValidParam, bool $expected, string $messa
*/
public function testLegacy($options, $isValidParam, bool $expected, string $messageKey): void
{
if (is_array($isValidParam)) {
$validator = new File\Hash($options);
$this->assertEquals($expected, $validator->isValid($isValidParam['tmp_name'], $isValidParam));
if (! $expected) {
$this->assertArrayHasKey($messageKey, $validator->getMessages());
}
if (! is_array($isValidParam)) {
$this->markTestSkipped('An array is expected for legacy compat tests');
}

$validator = new File\Hash($options);
$this->assertEquals($expected, $validator->isValid($isValidParam['tmp_name'], $isValidParam));
if (! $expected) {
$this->assertArrayHasKey($messageKey, $validator->getMessages());
}
}

Expand Down
23 changes: 12 additions & 11 deletions test/File/ImageSizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,17 +156,18 @@ public function testBasic(array $options, $isValidParam, bool $expected, $messag
*/
public function testLegacy(array $options, $isValidParam, bool $expected, $messageKeys): void
{
// Test legacy Laminas\Transfer API
if (is_array($isValidParam)) {
$validator = new File\ImageSize($options);
$this->assertEquals($expected, $validator->isValid($isValidParam['tmp_name'], $isValidParam));
if (! $expected) {
if (! is_array($messageKeys)) {
$messageKeys = [$messageKeys];
}
foreach ($messageKeys as $messageKey) {
$this->assertArrayHasKey($messageKey, $validator->getMessages());
}
if (! is_array($isValidParam)) {
$this->markTestSkipped('An array is expected for legacy compat tests');
}

$validator = new File\ImageSize($options);
$this->assertEquals($expected, $validator->isValid($isValidParam['tmp_name'], $isValidParam));
if (! $expected) {
if (! is_array($messageKeys)) {
$messageKeys = [$messageKeys];
}
foreach ($messageKeys as $messageKey) {
$this->assertArrayHasKey($messageKey, $validator->getMessages());
}
}
}
Expand Down
14 changes: 8 additions & 6 deletions test/File/Md5Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,14 @@ public function testBasic($options, $isValidParam, bool $expected, string $messa
*/
public function testLegacy($options, $isValidParam, bool $expected, string $messageKey): void
{
if (is_array($isValidParam)) {
$validator = new File\Md5($options);
$this->assertEquals($expected, $validator->isValid($isValidParam['tmp_name'], $isValidParam));
if (! $expected) {
$this->assertArrayHasKey($messageKey, $validator->getMessages());
}
if (! is_array($isValidParam)) {
$this->markTestSkipped('An array is expected for legacy compat tests');
}

$validator = new File\Md5($options);
$this->assertEquals($expected, $validator->isValid($isValidParam['tmp_name'], $isValidParam));
if (! $expected) {
$this->assertArrayHasKey($messageKey, $validator->getMessages());
}
}

Expand Down
8 changes: 5 additions & 3 deletions test/File/NotExistsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@ public function testBasic(string $options, $isValidParam, bool $expected): void
*/
public function testLegacy(string $options, $isValidParam, bool $expected): void
{
if (is_array($isValidParam)) {
$validator = new File\NotExists($options);
$this->assertEquals($expected, $validator->isValid($isValidParam['tmp_name'], $isValidParam));
if (! is_array($isValidParam)) {
$this->markTestSkipped('An array is expected for legacy compat tests');
}

$validator = new File\NotExists($options);
$this->assertEquals($expected, $validator->isValid($isValidParam['tmp_name'], $isValidParam));
}

/**
Expand Down
14 changes: 8 additions & 6 deletions test/File/Sha1Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,14 @@ public function testBasic($options, $isValidParam, bool $expected, string $messa
*/
public function testLegacy($options, $isValidParam, bool $expected, string $messageKey): void
{
if (is_array($isValidParam)) {
$validator = new File\Sha1($options);
$this->assertEquals($expected, $validator->isValid($isValidParam['tmp_name'], $isValidParam));
if (! $expected) {
$this->assertArrayHasKey($messageKey, $validator->getMessages());
}
if (! is_array($isValidParam)) {
$this->markTestSkipped('An array is expected for legacy compat tests');
}

$validator = new File\Sha1($options);
$this->assertEquals($expected, $validator->isValid($isValidParam['tmp_name'], $isValidParam));
if (! $expected) {
$this->assertArrayHasKey($messageKey, $validator->getMessages());
}
}

Expand Down
8 changes: 5 additions & 3 deletions test/File/SizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,12 @@ public function testBasic($options, $isValidParam, bool $expected): void
*/
public function testLegacy($options, $isValidParam, bool $expected): void
{
if (is_array($isValidParam)) {
$validator = new File\Size($options);
$this->assertEquals($expected, $validator->isValid($isValidParam['tmp_name'], $isValidParam));
if (! is_array($isValidParam)) {
$this->markTestSkipped('An array is expected for legacy compat tests');
}

$validator = new File\Size($options);
$this->assertEquals($expected, $validator->isValid($isValidParam['tmp_name'], $isValidParam));
}

/**
Expand Down

0 comments on commit b0e8bc7

Please sign in to comment.