Skip to content

Commit 7cbbd1b

Browse files
committed
fix and tests
1 parent f3bd8ba commit 7cbbd1b

2 files changed

Lines changed: 64 additions & 1 deletion

File tree

src/Illuminate/Foundation/Http/TypedFormRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static function from($input): static
3434
if (! $input instanceof Request) {
3535
$input = $input instanceof Arrayable ? $input->toArray() : $input;
3636

37-
$input = Request::create($input);
37+
$input = Request::create('', parameters: $input);
3838
}
3939

4040
return Container::getInstance()

tests/Integration/Http/TypedRequestTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,6 +1359,69 @@ public function testPassedValidationHookIsNotCalledOnFailure()
13591359
}
13601360
}
13611361

1362+
public function testFromBuildsFromArray()
1363+
{
1364+
$actual = MyTypedFormAutoRules::from(['name' => 'Taylor', 'age' => 30]);
1365+
1366+
$this->assertInstanceOf(MyTypedFormAutoRules::class, $actual);
1367+
$this->assertSame('Taylor', $actual->name);
1368+
$this->assertSame(30, $actual->age);
1369+
}
1370+
1371+
public function testFromFailsValidation()
1372+
{
1373+
$this->expectException(ValidationException::class);
1374+
1375+
MyTypedFormAutoRules::from(['name' => 'Taylor', 'age' => 'not-a-number']);
1376+
}
1377+
1378+
public function testFromSkipsAuthorization()
1379+
{
1380+
// MyUnauthorizedTypedForm::authorize() returns false,
1381+
// but from() should skip authorization entirely.
1382+
$actual = MyUnauthorizedTypedForm::from(['number' => 42, 'string' => 'hello']);
1383+
1384+
$this->assertInstanceOf(MyUnauthorizedTypedForm::class, $actual);
1385+
$this->assertSame(42, $actual->number);
1386+
$this->assertSame('hello', $actual->string);
1387+
}
1388+
1389+
public function testFromBuildsWithDefaults()
1390+
{
1391+
$actual = MyTypedFormAutoRules::from(['name' => 'Taylor', 'age' => 25]);
1392+
1393+
$this->assertNull($actual->bio);
1394+
$this->assertSame(SortDirection::Asc, $actual->sort);
1395+
$this->assertTrue($actual->active);
1396+
}
1397+
1398+
public function testFromBuildsNestedTypedFormRequest()
1399+
{
1400+
$actual = CreateOrderRequest::from([
1401+
'item' => 'Widget',
1402+
'address' => [
1403+
'street' => '123 Main St',
1404+
'city' => 'Springfield',
1405+
],
1406+
]);
1407+
1408+
$this->assertInstanceOf(CreateOrderRequest::class, $actual);
1409+
$this->assertSame('Widget', $actual->item);
1410+
$this->assertInstanceOf(Address::class, $actual->address);
1411+
$this->assertSame('123 Main St', $actual->address->street);
1412+
}
1413+
1414+
public function testFromAcceptsRequestInstance()
1415+
{
1416+
$request = Request::create('', parameters: ['name' => 'Taylor', 'age' => 30]);
1417+
1418+
$actual = MyTypedFormAutoRules::from($request);
1419+
1420+
$this->assertInstanceOf(MyTypedFormAutoRules::class, $actual);
1421+
$this->assertSame('Taylor', $actual->name);
1422+
$this->assertSame(30, $actual->age);
1423+
}
1424+
13621425
public function testStopOnFirstFailureAttributeReportsOnlyOneError()
13631426
{
13641427
// Both name and age are required but neither is provided.

0 commit comments

Comments
 (0)