Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
magicsunday committed Mar 2, 2024
1 parent e345b78 commit 115643e
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 37 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"phpdocumentor/reflection-docblock": "^5.0",
"friendsofphp/php-cs-fixer": "^3.50",
"overtrue/phplint": "^3.4 || ^9.0",
"phpunit/phpunit": "^9.0 || ^10.0 || ^11.0",
"phpunit/phpunit": "^10.0 || ^11.0",
"phpstan/phpstan": "^1.10",
"phpstan/phpstan-phpunit": "^1.3",
"phpstan/phpstan-strict-rules": "^1.5",
Expand Down
6 changes: 6 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters:
ignoreErrors:
-
message: "#^Method MagicSunday\\\\JsonMapper\\:\\:makeInstance\\(\\) has parameter \\$constructorArguments with no value type specified in iterable type array\\.$#"
count: 1
path: src/JsonMapper.php
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
includes:
- %currentWorkingDirectory%/.build/vendor/phpstan/phpstan-strict-rules/rules.neon
- %currentWorkingDirectory%/.build/vendor/phpstan/phpstan-deprecation-rules/rules.neon
# - %currentWorkingDirectory%/phpstan-baseline.neon
- %currentWorkingDirectory%/phpstan-baseline.neon

parameters:
# You can currently choose from 10 levels (0 is the loosest and 9 is the strictest).
Expand Down
13 changes: 7 additions & 6 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
colors="true"
columns="max"
stderr="true"
bootstrap="./.build/vendor/autoload.php"
cacheDirectory=".phpunit.cache"
>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Integration Tests">
<directory>./test</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>./src</directory>
</include>
</source>
</phpunit>
19 changes: 19 additions & 0 deletions phpunit.xml.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
colors="true"
columns="max"
stderr="true"
bootstrap="./.build/vendor/autoload.php"
>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Integration Tests">
<directory>./test</directory>
</testsuite>
</testsuites>
</phpunit>
26 changes: 10 additions & 16 deletions src/JsonMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,11 @@ private function getReflectionProperty(string $className, string $propertyName):
*/
private function getReflectionClass(string $className): ?ReflectionClass
{
try {
return new ReflectionClass($className);
} catch (ReflectionException $exception) {
if (!class_exists($className)) {
return null;
}

return new ReflectionClass($className);
}

/**
Expand Down Expand Up @@ -421,12 +421,7 @@ private function getDefaultValue(string $className, string $propertyName)
return null;
}

if (method_exists($reflectionProperty, 'getDefaultValue')) {
// PHP 8+, use getDefaultValue() method
return $reflectionProperty->getDefaultValue();
}

return $reflectionProperty->getDeclaringClass()->getDefaultProperties()[$propertyName] ?? null;
return $reflectionProperty->getDefaultValue();
}

/**
Expand Down Expand Up @@ -515,7 +510,11 @@ private function setProperty(object $entity, string $name, $value): void
$parameters = $method->getParameters();

if ((count($parameters) === 1) && $parameters[0]->isVariadic()) {
$entity->$methodName(...$value);
$callable = [$entity, $methodName];

if (is_callable($callable)) {
call_user_func_array($callable, $value);
}

return;
}
Expand Down Expand Up @@ -602,12 +601,7 @@ private function getValue($json, Type $type)
*/
public function getCollectionValueType(Type $type): Type
{
// BC for symfony < 5.3
if (!method_exists($type, 'getCollectionValueTypes')) {
$collectionValueType = $type->getCollectionValueType();
} else {
$collectionValueType = $type->getCollectionValueTypes()[0] ?? null;
}
$collectionValueType = $type->getCollectionValueTypes()[0] ?? null;

return $collectionValueType ?? $this->defaultType;
}
Expand Down
4 changes: 3 additions & 1 deletion test/Classes/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace MagicSunday\Test\Classes;

use AllowDynamicProperties;

/**
* Class Person.
*
Expand All @@ -20,7 +22,7 @@
*
* @property int $oscars Dynamic created property
*/
#[\AllowDynamicProperties]
#[AllowDynamicProperties]
class Person
{
/**
Expand Down
24 changes: 12 additions & 12 deletions test/JsonMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class JsonMapperTest extends TestCase
/**
* @return string[][]
*/
public function mapArrayOrCollectionWithIntegerKeysJsonDataProvider(): array
public static function mapArrayOrCollectionWithIntegerKeysJsonDataProvider(): array
{
return [
'mapArray' => [
Expand Down Expand Up @@ -121,7 +121,7 @@ public function mapArrayOrCollectionWithStringKeys(): void
/**
* @return string[][]
*/
public function mapSimpleArrayJsonDataProvider(): array
public static function mapSimpleArrayJsonDataProvider(): array
{
return [
'mapSimpleArray' => [
Expand Down Expand Up @@ -160,7 +160,7 @@ public function mapSimpleArray(string $jsonString): void
/**
* @return string[][]
*/
public function mapSimpleCollectionJsonDataProvider(): array
public static function mapSimpleCollectionJsonDataProvider(): array
{
return [
'mapSimpleCollection' => [
Expand Down Expand Up @@ -199,7 +199,7 @@ public function mapSimpleCollection(string $jsonString): void
/**
* @return string[][]
*/
public function mapCustomTypeJsonDataProvider(): array
public static function mapCustomTypeJsonDataProvider(): array
{
return [
'mapCustomType' => [
Expand Down Expand Up @@ -247,7 +247,7 @@ static function ($value): ?CustomConstructor {
/**
* @return string[][]
*/
public function mapSimpleTypesJsonDataProvider(): array
public static function mapSimpleTypesJsonDataProvider(): array
{
return [
'mapCustomType' => [
Expand Down Expand Up @@ -292,7 +292,7 @@ public function mapSimpleTypesJson(string $jsonString): void
/**
* @return string[][]
*/
public function mapObjectUsingCustomClassNameJsonDataProvider(): array
public static function mapObjectUsingCustomClassNameJsonDataProvider(): array
{
return [
'mapCustomClassName' => [
Expand Down Expand Up @@ -553,7 +553,7 @@ public function mapNullToDefaultValueUsingAnnotation(): void
/**
* @return string[][]
*/
public function mapPlainArrayJsonDataProvider(): array
public static function mapPlainArrayJsonDataProvider(): array
{
return [
'mapPlainArray' => [
Expand Down Expand Up @@ -591,7 +591,7 @@ public function mapPlainArray(string $jsonString): void
/**
* @return string[][]
*/
public function mapPlainArrayKeyValueJsonDataProvider(): array
public static function mapPlainArrayKeyValueJsonDataProvider(): array
{
return [
'mapPlainArrayKeyValue' => [
Expand Down Expand Up @@ -628,9 +628,9 @@ public function mapPlainArrayKeyValue(string $jsonString): void

self::assertIsObject($result);
self::assertInstanceOf(stdClass::class, $result);
self::assertObjectHasAttribute('A', $result);
self::assertObjectHasProperty('A', $result);
self::assertSame(1, $result->A);
self::assertObjectHasAttribute('Z', $result);
self::assertObjectHasProperty('Z', $result);
self::assertSame(26, $result->Z);

// Map plain array with key <=> value pair to a custom class
Expand All @@ -642,9 +642,9 @@ public function mapPlainArrayKeyValue(string $jsonString): void

self::assertIsObject($result);
self::assertInstanceOf(MapPlainArrayKeyValueClass::class, $result);
self::assertObjectHasAttribute('a', $result);
self::assertObjectHasProperty('a', $result);
self::assertSame(1, $result->a);
self::assertObjectHasAttribute('z', $result);
self::assertObjectHasProperty('z', $result);
self::assertSame(26, $result->z);
}

Expand Down

0 comments on commit 115643e

Please sign in to comment.