Skip to content

Commit

Permalink
test: Add a test/doc to cover usage without an InboundProperty attrib…
Browse files Browse the repository at this point in the history
…ute (#1)
  • Loading branch information
Pixelshaped authored Jul 1, 2024
1 parent 8b75f38 commit 926e2ee
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ This bundle comes with several attributes that you can use to add mapping to you
- Use it as a Property attribute if you have some use for it ([see example](tests/Examples/Valid/Complex/CustomerDTO.php)).
- Specify the mapped property name directly on the attribute ([see example](tests/Examples/Valid/Complex/InvoiceDTO.php)). This is mandatory when used as a Class attribute.
- Specify the mapped property name separately with the `InboundProperty` attribute, Doctrine-style ([see example](tests/Examples/Valid/ReferencesArray/RootDTO.php)).
- `#[InboundProperty("mapped_property_name")]`: The name of the key on the associative arrays contained by your result set. This is optional if your DTO's property names are already matching the result set.
- `#[InboundProperty("mapped_property_name")]`: The name of the key on the associative arrays contained by your result set. This is optional if your DTO's property names are already matching the result set ([see example](tests/Examples/Valid/WithoutAttributeDTO.php)).
- `#[ReferencesArray(NestedDTO::class)]`: An array of `NestedDTO` will be created using the mapping information contained in `NestedDTO`.
- `#[ColumnArray("mapped_property_name")]` the column `mapped_property_name` of your result set will be mapped as an array of scalar properties (such as IDs).

Expand Down
16 changes: 16 additions & 0 deletions tests/Examples/Valid/WithoutAttributeDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);

namespace Pixelshaped\FlatMapperBundle\Tests\Examples\Valid;

use Pixelshaped\FlatMapperBundle\Attributes\Identifier;

class WithoutAttributeDTO
{
public function __construct(
#[Identifier]
public int $id,
public string $foo,
public int $bar,
) {}
}
20 changes: 20 additions & 0 deletions tests/FlatMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Pixelshaped\FlatMapperBundle\Tests\Examples\Valid\Complex\ProductDTO;
use Pixelshaped\FlatMapperBundle\Tests\Examples\Valid\ReferencesArray\LeafDTO;
use Pixelshaped\FlatMapperBundle\Tests\Examples\Valid\ReferencesArray\RootDTO as ValidRootDTO;
use Pixelshaped\FlatMapperBundle\Tests\Examples\Valid\WithoutAttributeDTO;
use RuntimeException;

#[CoversMethod(FlatMapper::class, 'createMapping')]
Expand Down Expand Up @@ -176,6 +177,25 @@ public function testDeepNestedDTOs(): void
$this->assertEquals(138619, $customerDto->getTotalPurchases());
}

public function testMapWithoutAttributeDTO(): void
{
$results = [
['id' => 1, 'foo' => 'Foo 1', 'bar' => 1],
['id' => 2, 'foo' => 'Foo 2', 'bar' => 2],
];

$flatMapperResults = ((new FlatMapper())->map(WithoutAttributeDTO::class, $results));

$rootDto1 = new WithoutAttributeDTO(1, "Foo 1", 1);
$rootDto2 = new WithoutAttributeDTO(2, "Foo 2", 2);
$handmadeResult = [1 => $rootDto1, 2 => $rootDto2];

$this->assertSame(
var_export($flatMapperResults, true),
var_export($handmadeResult, true)
);
}

public function testMapEmptyData(): void
{
$flatMapperResults = ((new FlatMapper())->map(ColumnArrayDTO::class, []));
Expand Down

0 comments on commit 926e2ee

Please sign in to comment.