-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: error when mapping null to existing target (#248)
- Loading branch information
Showing
11 changed files
with
203 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/Transformer/Exception/NullSourceButMandatoryTargetException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of rekalogika/mapper package. | ||
* | ||
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
namespace Rekalogika\Mapper\Transformer\Exception; | ||
|
||
use Rekalogika\Mapper\Context\Context; | ||
use Rekalogika\Mapper\Exception\RuntimeException; | ||
use Rekalogika\Mapper\Util\TypeFactory; | ||
use Rekalogika\Mapper\Util\TypeUtil; | ||
use Symfony\Component\PropertyInfo\Type; | ||
|
||
class NullSourceButMandatoryTargetException extends RuntimeException | ||
{ | ||
public function __construct( | ||
?Type $targetType, | ||
?\Throwable $previous = null, | ||
?Context $context = null, | ||
) { | ||
parent::__construct( | ||
message: \sprintf( | ||
'The source is null, the target is mandatory & expected to be of type "%s". But no transformer is able to handle this case.', | ||
TypeUtil::getTypeString($targetType ?? TypeFactory::mixed()), | ||
), | ||
previous: $previous, | ||
context: $context, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of rekalogika/mapper package. | ||
* | ||
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
namespace Rekalogika\Mapper\Tests\Fixtures\NullSource; | ||
|
||
final readonly class Source | ||
{ | ||
// @phpstan-ignore return.unusedType | ||
public function getProperty(): ?string | ||
{ | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of rekalogika/mapper package. | ||
* | ||
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
namespace Rekalogika\Mapper\Tests\Fixtures\NullSource; | ||
|
||
use Symfony\Component\Uid\Uuid; | ||
|
||
class TargetString | ||
{ | ||
private string $property; | ||
|
||
public function __construct() | ||
{ | ||
$this->property = (string) Uuid::v7(); | ||
} | ||
|
||
public function getProperty(): string | ||
{ | ||
return $this->property; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of rekalogika/mapper package. | ||
* | ||
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
namespace Rekalogika\Mapper\Tests\Fixtures\NullSource; | ||
|
||
use Symfony\Component\Uid\Uuid; | ||
|
||
class TargetUuid | ||
{ | ||
private Uuid $property; | ||
|
||
public function __construct() | ||
{ | ||
$this->property = Uuid::v7(); | ||
} | ||
|
||
public function getProperty(): Uuid | ||
{ | ||
return $this->property; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of rekalogika/mapper package. | ||
* | ||
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
namespace Rekalogika\Mapper\Tests\IntegrationTest; | ||
|
||
use Rekalogika\Mapper\Tests\Common\FrameworkTestCase; | ||
use Rekalogika\Mapper\Tests\Fixtures\NullSource\Source; | ||
use Rekalogika\Mapper\Tests\Fixtures\NullSource\TargetString; | ||
use Rekalogika\Mapper\Tests\Fixtures\NullSource\TargetUuid; | ||
|
||
class NullSourceTest extends FrameworkTestCase | ||
{ | ||
public function testNullSourceToUuid(): void | ||
{ | ||
$source = new Source(); | ||
$target = new TargetUuid(); | ||
$newTarget = $this->mapper->map($source, $target); | ||
|
||
$this->assertSame($target, $newTarget); | ||
} | ||
|
||
public function testNullSourceToString(): void | ||
{ | ||
$source = new Source(); | ||
$target = new TargetString(); | ||
$newTarget = $this->mapper->map($source, $target); | ||
|
||
$this->assertSame($target, $newTarget); | ||
} | ||
|
||
} |