Skip to content

Commit

Permalink
fix: issue 232
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Oct 15, 2024
1 parent ebcf851 commit 7df82a0
Show file tree
Hide file tree
Showing 9 changed files with 251 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
handle the current target value in all cases
* test: test lazy constructor
* fix: double mapping if a property is in the constructor and also has a setter
* fix: issue [#232](https://github.com/rekalogika/mapper/issues/232)232

## 1.11.0

Expand Down
12 changes: 12 additions & 0 deletions tests/config/rekalogika-mapper/generated-mappings.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,18 @@
target: \Rekalogika\Mapper\Tests\Fixtures\InternalClass\ObjectWithInternalClassDto::class
);

$mappingCollection->addObjectMapping(
// tests/src/IntegrationTest/Issue232Test.php on line 27
source: \Rekalogika\Mapper\Tests\Fixtures\Issue232\BigDecimalSource::class,
target: \Rekalogika\Mapper\Tests\Fixtures\Issue232\BigDecimalTarget::class
);

$mappingCollection->addObjectMapping(
// tests/src/IntegrationTest/Issue232Test.php on line 35
source: \Rekalogika\Mapper\Tests\Fixtures\Issue232\CurrencySource::class,
target: \Rekalogika\Mapper\Tests\Fixtures\Issue232\CurrencyTarget::class
);

$mappingCollection->addObjectMapping(
// tests/src/IntegrationTest/LazyObjectTest.php on lines 99, 109
source: \Rekalogika\Mapper\Tests\Fixtures\LazyObject\ObjectWithId::class,
Expand Down
21 changes: 21 additions & 0 deletions tests/src/Fixtures/Issue232/BigDecimalSource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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\Issue232;

final readonly class BigDecimalSource
{
public function __construct(
public string $amount,
) {}
}
23 changes: 23 additions & 0 deletions tests/src/Fixtures/Issue232/BigDecimalTarget.php
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\Issue232;

use Brick\Math\BigDecimal;

final readonly class BigDecimalTarget
{
public function __construct(
public BigDecimal $amount,
) {}
}
21 changes: 21 additions & 0 deletions tests/src/Fixtures/Issue232/CurrencySource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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\Issue232;

final readonly class CurrencySource
{
public function __construct(
public string $currency,
) {}
}
23 changes: 23 additions & 0 deletions tests/src/Fixtures/Issue232/CurrencyTarget.php
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\Issue232;

use Brick\Money\Currency;

final readonly class CurrencyTarget
{
public function __construct(
public Currency $currency,
) {}
}
39 changes: 39 additions & 0 deletions tests/src/IntegrationTest/Issue232Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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\Issue232\BigDecimalSource;
use Rekalogika\Mapper\Tests\Fixtures\Issue232\BigDecimalTarget;
use Rekalogika\Mapper\Tests\Fixtures\Issue232\CurrencySource;
use Rekalogika\Mapper\Tests\Fixtures\Issue232\CurrencyTarget;

class Issue232Test extends FrameworkTestCase
{
public function testBigDecimal(): void
{
$source = new BigDecimalSource('100');
$target = $this->mapper->map($source, BigDecimalTarget::class);

$this->assertEquals('100', $target->amount->toScale(0)->jsonSerialize());
}

public function testCurrency(): void
{
$source = new CurrencySource('USD');
$target = $this->mapper->map($source, CurrencyTarget::class);

$this->assertEquals('USD', $target->currency->getCurrencyCode());
}
}
65 changes: 65 additions & 0 deletions tests/src/Services/Issue232/BigDecimalTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?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\Services\Issue232;

use Brick\Math\BigDecimal;
use Rekalogika\Mapper\Context\Context;
use Rekalogika\Mapper\Transformer\TransformerInterface;
use Rekalogika\Mapper\Transformer\TypeMapping;
use Rekalogika\Mapper\Util\TypeFactory;
use Symfony\Component\PropertyInfo\Type;

final readonly class BigDecimalTransformer implements TransformerInterface
{
public function transform(
mixed $source,
mixed $target,
?Type $sourceType,
?Type $targetType,
Context $context,
): BigDecimal|string {
if ($source instanceof BigDecimal) {
return (string) $source;
}

/**
* @psalm-suppress MixedArgument
* @phpstan-ignore argument.type
*/
return BigDecimal::of($source)->toBigDecimal();
}

public function getSupportedTransformation(): iterable
{
yield new TypeMapping(
TypeFactory::string(),
TypeFactory::objectOfClass(BigDecimal::class),
);

yield new TypeMapping(
TypeFactory::int(),
TypeFactory::objectOfClass(BigDecimal::class),
);

yield new TypeMapping(
TypeFactory::float(),
TypeFactory::objectOfClass(BigDecimal::class),
);

yield new TypeMapping(
TypeFactory::objectOfClass(BigDecimal::class),
TypeFactory::string(),
);
}
}
46 changes: 46 additions & 0 deletions tests/src/Services/Issue232/CurrencyTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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\Services\Issue232;

use Brick\Money\Currency;
use Rekalogika\Mapper\Context\Context;
use Rekalogika\Mapper\Transformer\TransformerInterface;
use Rekalogika\Mapper\Transformer\TypeMapping;
use Rekalogika\Mapper\Util\TypeFactory;
use Symfony\Component\PropertyInfo\Type;

final readonly class CurrencyTransformer implements TransformerInterface
{
public function transform(
mixed $source,
mixed $target,
?Type $sourceType,
?Type $targetType,
Context $context,
): mixed {
/**
* @psalm-suppress MixedArgument
* @phpstan-ignore argument.type
*/
return Currency::of($source);
}

public function getSupportedTransformation(): iterable
{
yield new TypeMapping(
TypeFactory::string(),
TypeFactory::objectOfClass(Currency::class),
);
}
}

0 comments on commit 7df82a0

Please sign in to comment.