Skip to content

Commit

Permalink
fix carbon mapper with null values
Browse files Browse the repository at this point in the history
  • Loading branch information
keithbrink committed Nov 19, 2024
1 parent b1d4f14 commit ec94f72
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" colors="true" processIsolation="false" testdox="true" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false" displayDetailsOnTestsThatTriggerWarnings="true" displayDetailsOnTestsThatTriggerDeprecations="true">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" colors="true" processIsolation="false" testdox="true" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false" displayDetailsOnTestsThatTriggerWarnings="true" displayDetailsOnTestsThatTriggerDeprecations="true" requireCoverageMetadata="true">
<coverage>
<report>
<clover outputFile="tests/resources/clover.xml"/>
Expand Down
8 changes: 6 additions & 2 deletions src/Data/Base/Mappers/CarbonToDateStringMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)]
class CarbonToDateStringMapper implements Mapper
{
public function map(mixed $value): string
public function map(mixed $value): ?string
{
if (!($value instanceof CarbonInterface)) {
if (is_null($value)) {
return null;
}

if (! ($value instanceof CarbonInterface)) {
throw new \InvalidArgumentException('Value must be an instance of Carbon');
}

Expand Down
8 changes: 8 additions & 0 deletions tests/Unit/Data/Base/Mappers/CarbonToDateStringMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@ public function testThrowsExceptionIfNotCarbon(): void
$mapper = new CarbonToDateStringMapper();
$mapper->map('2021-01-01');
}

public function testReturnsNullIfNull(): void
{
$mapper = new CarbonToDateStringMapper();
$result = $mapper->map(null);

$this->assertNull($result);
}
}
3 changes: 3 additions & 0 deletions tests/Unit/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Jasara\AmznSPA\Tests\Unit;

use PHPUnit\Framework\Attributes\CoversFunction;

#[CoversFunction('array_keys_to_snake')]
class HelpersTest extends UnitTestCase
{
public function testArrayKeysToSnake()
Expand Down

0 comments on commit ec94f72

Please sign in to comment.