Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add a test/doc to cover usage without an InboundProperty attribute #1

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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