Skip to content

Commit

Permalink
Merge branch 'nsvetozarevic-parameters-check'
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Jan 3, 2025
2 parents 5526eac + fbbda2a commit 30242c4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/Resolvers/DataFromArrayResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Spatie\LaravelData\Optional;
use Spatie\LaravelData\Support\DataClass;
use Spatie\LaravelData\Support\DataConfig;
use Spatie\LaravelData\Support\DataParameter;
use Spatie\LaravelData\Support\DataProperty;

/**
* @template TData of BaseData
Expand Down Expand Up @@ -94,11 +96,26 @@ protected function createData(
try {
return new $dataClass->name(...$parameters);
} catch (ArgumentCountError $error) {
throw CannotCreateData::constructorMissingParameters(
$dataClass,
$parameters,
$error
);
if ($this->isAnyParameterMissing($dataClass, array_keys($parameters))) {
throw CannotCreateData::constructorMissingParameters(
$dataClass,
$parameters,
$error
);
} else {
throw $error;
}
}
}

protected function isAnyParameterMissing(DataClass $dataClass, array $parameters): bool
{
return $dataClass
->constructorMethod
->parameters
->filter(fn (DataParameter|DataProperty $parameter) => ! $parameter->hasDefaultValue)
->pluck('name')
->diff($parameters)
->isNotEmpty();
}
}
13 changes: 13 additions & 0 deletions tests/CreationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Support\Facades\Route;
use Illuminate\Validation\ValidationException;

use Spatie\LaravelData\Tests\Fakes\DataWithArgumentCountErrorException;
use function Pest\Laravel\postJson;

use Spatie\LaravelData\Attributes\Computed;
Expand Down Expand Up @@ -751,6 +752,18 @@ public function __construct(
yield 'one param' => [['first' => 'First'], 'Could not create `Spatie\LaravelData\Tests\Fakes\MultiData`: the constructor requires 2 parameters, 1 given. Parameters given: first. Parameters missing: second.'],
]);

it('throws a readable exception message when the ArgumentCountError exception is thrown in the constructor', function () {
try {
DataWithArgumentCountErrorException::from(['string' => 'string']);
} catch (ArgumentCountError $e) {
expect($e->getMessage())->toBe('This function expects exactly 2 arguments, 1 given.');
expect($e->getFile())->toContain('/tests/Fakes/DataWithArgumentCountErrorException.php');
expect($e->getLine())->toBe(14);

return;
}
});

it('throws a readable exception message when the constructor of a nested data object fails', function () {
expect(fn () => NestedData::from([
'simple' => [],
Expand Down
16 changes: 16 additions & 0 deletions tests/Fakes/DataWithArgumentCountErrorException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Spatie\LaravelData\Tests\Fakes;

use ArgumentCountError;
use Spatie\LaravelData\Data;

class DataWithArgumentCountErrorException extends Data
{
public function __construct(
public string $string,
public string $optional = 'default',
) {
throw new ArgumentCountError('This function expects exactly 2 arguments, 1 given.');
}
}

0 comments on commit 30242c4

Please sign in to comment.