Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Support PHP ^8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt committed Aug 31, 2021
1 parent 2f62916 commit 1d38761
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

All notable changes to `data-transfer-object` will be documented in this file

## 1.14.0 - 2021-08-31

- Support PHP ^8.0

## 1.13.3 - 2020-01-29

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
}
],
"require": {
"php": "^7.1"
"php": "^7.1|^8.0"
},
"require-dev": {
"larapack/dd": "^1.0",
"phpunit/phpunit": "^7.0"
"larapack/dd": "^1.1",
"phpunit/phpunit": "^7.0|^9.0"
},
"autoload": {
"psr-4": {
Expand Down
32 changes: 20 additions & 12 deletions tests/DataTransferObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ public function only_the_type_hinted_type_may_be_passed()
$this->markTestSucceeded();

$this->expectException(DataTransferObjectError::class);
$this->expectExceptionMessageRegExp('/Invalid type: expected `class@anonymous[^:]+::foo` to be of type `string`, instead got value ``, which is boolean/');

new class(['foo' => false]) extends DataTransferObject {
/** @var string */
public $foo;
};

$this->assertMatchesRegularExpression('/Invalid type: expected `class@anonymous[^:]+::foo` to be of type `string`, instead got value ``, which is boolean/', $this->getExpectedExceptionMessage());
}

/** @test */
Expand Down Expand Up @@ -81,22 +82,24 @@ public function default_values_are_supported()
public function null_is_allowed_only_if_explicitly_specified()
{
$this->expectException(DataTransferObjectError::class);
$this->expectExceptionMessageRegExp('/Invalid type: expected `class@anonymous[^:]+::foo` to be of type `string`, instead got value `null`, which is NULL/');

new class(['foo' => null]) extends DataTransferObject {
/** @var string */
public $foo;
};

$this->assertMatchesRegularExpression('/Invalid type: expected `class@anonymous[^:]+::foo` to be of type `string`, instead got value `null`, which is NULL/', $this->getExpectedExceptionMessage());
}

/** @test */
public function unknown_properties_throw_an_error()
{
$this->expectException(DataTransferObjectError::class);
$this->expectExceptionMessageRegExp('/Public properties `bar` not found on class@anonymous/');

new class(['bar' => null]) extends DataTransferObject {
};

$this->assertMatchesRegularExpression('/Public properties `bar` not found on class@anonymous/', $this->getExpectedExceptionMessage());
}

/** @test */
Expand All @@ -106,8 +109,8 @@ public function unknown_properties_show_a_comprehensive_error_message()
new class(['foo' => null, 'bar' => null]) extends DataTransferObject {
};
} catch (DataTransferObjectError $error) {
$this->assertContains('`foo`', $error->getMessage());
$this->assertContains('`bar`', $error->getMessage());
$this->assertStringContainsString('`foo`', $error->getMessage());
$this->assertStringContainsString('`bar`', $error->getMessage());
}
}

Expand Down Expand Up @@ -191,7 +194,6 @@ public function classes_are_supported()
$this->markTestSucceeded();

$this->expectException(DataTransferObjectError::class);
$this->expectExceptionMessageRegExp('/Invalid type: expected `class@anonymous[^:]+::foo` to be of type `\\\Spatie\\\DataTransferObject\\\Tests\\\TestClasses\\\DummyClass`, instead got value `class@anonymous[^`]+`, which is object/');

new class(['foo' => new class() {
},
Expand All @@ -200,6 +202,8 @@ public function classes_are_supported()
/** @var \Spatie\DataTransferObject\Tests\TestClasses\DummyClass */
public $foo;
};

$this->assertMatchesRegularExpression('/Invalid type: expected `class@anonymous[^:]+::foo` to be of type `\\\Spatie\\\DataTransferObject\\\Tests\\\TestClasses\\\DummyClass`, instead got value `class@anonymous[^`]+`, which is object/', $this->getExpectedExceptionMessage());
}

/** @test */
Expand All @@ -213,36 +217,39 @@ public function generic_collections_are_supported()
$this->markTestSucceeded();

$this->expectException(DataTransferObjectError::class);
$this->expectExceptionMessageRegExp('/Invalid type: expected `class@anonymous[^:]+::foo` to be of type `\\\Spatie\\\DataTransferObject\\\Tests\\\TestClasses\\\DummyClass\[\]`, instead got value `array`, which is array/');

new class(['foo' => [new OtherClass()]]) extends DataTransferObject {
/** @var \Spatie\DataTransferObject\Tests\TestClasses\DummyClass[] */
public $foo;
};

$this->assertMatchesRegularExpression('/Invalid type: expected `class@anonymous[^:]+::foo` to be of type `\\\Spatie\\\DataTransferObject\\\Tests\\\TestClasses\\\DummyClass\[\]`, instead got value `array`, which is array/', $this->getExpectedExceptionMessage());
}

/** @test */
public function an_exception_is_thrown_for_a_generic_collection_of_null()
{
$this->expectException(DataTransferObjectError::class);
$this->expectExceptionMessageRegExp('/Invalid type: expected `class@anonymous[^:]+::foo` to be of type `string\[\]`, instead got value `array`, which is array./');

new class(['foo' => [null]]) extends DataTransferObject {
/** @var string[] */
public $foo;
};

$this->assertMatchesRegularExpression('/Invalid type: expected `class@anonymous[^:]+::foo` to be of type `string\[\]`, instead got value `array`, which is array./', $this->getExpectedExceptionMessage());
}

/** @test */
public function an_exception_is_thrown_when_property_was_not_initialised()
{
$this->expectException(DataTransferObjectError::class);
$this->expectExceptionMessageRegExp('/Invalid type: expected `class@anonymous[^:]+::foo` to be of type `string`, instead got value `null`, which is NULL/');

new class([]) extends DataTransferObject {
/** @var string */
public $foo;
};

$this->assertMatchesRegularExpression('/Invalid type: expected `class@anonymous[^:]+::foo` to be of type `string`, instead got value `null`, which is NULL/', $this->getExpectedExceptionMessage());
}

/** @test */
Expand Down Expand Up @@ -424,12 +431,13 @@ public function iterable_is_supported()
public function an_exception_is_thrown_for_incoherent_iterator_type()
{
$this->expectException(DataTransferObjectError::class);
$this->expectExceptionMessageRegExp('/Invalid type: expected `class@anonymous[^:]+::strings` to be of type `iterable<string>`, instead got value `array`, which is array./');

new class(['strings' => ['foo', 1]]) extends DataTransferObject {
/** @var iterable<string> */
public $strings;
};

$this->assertMatchesRegularExpression('/Invalid type: expected `class@anonymous[^:]+::strings` to be of type `iterable<string>`, instead got value `array`, which is array./', $this->getExpectedExceptionMessage());
}

/** @test */
Expand Down Expand Up @@ -472,8 +480,8 @@ public function array_of_dtos()
$this->assertSame(1, $arrayOf[0]->testProperty);
$this->assertSame(2, $arrayOf[1]->testProperty);
}


/** @test */
public function ignore_static_public_properties()
{
Expand Down

0 comments on commit 1d38761

Please sign in to comment.