Skip to content

Commit 36311a0

Browse files
committed
fix(injector): initialize nullable promoted file properties
1 parent 77f5d23 commit 36311a0

4 files changed

Lines changed: 45 additions & 2 deletions

File tree

src/Injector/FileInjector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public function injectFile(object $obj, PropertyMapping $mapping): void
2323

2424
if (null !== $path) {
2525
$mapping->setFile($obj, new File($path, false));
26+
} else {
27+
$mapping->setFile($obj, null);
2628
}
2729
}
2830
}

src/Mapping/PropertyMapping.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ public function getFile(object $obj): ?File
7070
* Modifies the file property value for the given object.
7171
*
7272
* @param object $obj The object
73-
* @param File $file The new file
73+
* @param File|null $file The new file
7474
*
7575
* @throws \InvalidArgumentException
7676
* @throws \TypeError
7777
*/
78-
public function setFile(object $obj, File $file): void
78+
public function setFile(object $obj, ?File $file): void
7979
{
8080
$this->writeProperty($obj, 'file', $file);
8181
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Vich\UploaderBundle\Tests\Fixtures;
4+
5+
use Symfony\Component\HttpFoundation\File\File;
6+
7+
final class PromotedFileEntity
8+
{
9+
public function __construct(private ?File $file = null)
10+
{
11+
}
12+
13+
public function getFile(): ?File
14+
{
15+
return $this->file;
16+
}
17+
18+
public function setFile(?File $file): void
19+
{
20+
$this->file = $file;
21+
}
22+
}

tests/Injector/FileInjectorTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
use PHPUnit\Framework\MockObject\MockObject;
66
use Vich\UploaderBundle\Injector\FileInjector;
7+
use Vich\UploaderBundle\Mapping\PropertyMapping;
78
use Vich\UploaderBundle\Storage\GaufretteStorage;
89
use Vich\UploaderBundle\Tests\DummyEntity;
10+
use Vich\UploaderBundle\Tests\Fixtures\PromotedFileEntity;
911
use Vich\UploaderBundle\Tests\TestCase;
1012

1113
/**
@@ -66,4 +68,21 @@ public function testPropertyIsNullWhenFileNamePropertyIsNull(): void
6668
$inject = new FileInjector($this->storage);
6769
$inject->injectFile($obj, $fileMapping);
6870
}
71+
72+
public function testInitializesNullablePromotedFilePropertyWhenFileNamePropertyIsNull(): void
73+
{
74+
$obj = (new \ReflectionClass(PromotedFileEntity::class))->newInstanceWithoutConstructor();
75+
$mapping = new PropertyMapping('file', 'file_name');
76+
77+
$this->storage
78+
->expects(self::once())
79+
->method('resolvePath')
80+
->with($obj, 'file')
81+
->willReturn(null);
82+
83+
$inject = new FileInjector($this->storage);
84+
$inject->injectFile($obj, $mapping);
85+
86+
self::assertNull($obj->getFile());
87+
}
6988
}

0 commit comments

Comments
 (0)