Skip to content

Commit

Permalink
test: Add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Jan 12, 2024
1 parent 73c6128 commit c827cf1
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 1 deletion.
27 changes: 27 additions & 0 deletions tests/Fixtures/ArrayLike/LazyDoctrineCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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\ArrayLike;

use Doctrine\Common\Collections\AbstractLazyCollection;

/**
* @extends AbstractLazyCollection<array-key,mixed>
*/
class LazyDoctrineCollection extends AbstractLazyCollection
{
protected function doInitialize()
{
throw new \LogicException("Not expected to be initialized");
}
}
34 changes: 34 additions & 0 deletions tests/Fixtures/ArrayLike/ObjectWithCollectionProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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\ArrayLike;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Rekalogika\Mapper\Tests\Fixtures\Scalar\ObjectWithScalarProperties;

class ObjectWithCollectionProperty
{
/**
* @var Collection<int,ObjectWithScalarProperties>
*/
public Collection $property;

public function __construct()
{
$this->property = new ArrayCollection();
$this->property->add(new ObjectWithScalarProperties());
$this->property->add(new ObjectWithScalarProperties());
$this->property->add(new ObjectWithScalarProperties());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?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\ArrayLike;

class ObjectWithLazyDoctrineCollectionProperty
{
public LazyDoctrineCollection $property;

public function __construct()
{
$this->property = new LazyDoctrineCollection();
}
}
25 changes: 25 additions & 0 deletions tests/Fixtures/ArrayLikeDto/ObjectWithCollectionPropertyDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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\ArrayLikeDto;

use Doctrine\Common\Collections\Collection;
use Rekalogika\Mapper\Tests\Fixtures\Scalar\ObjectWithScalarPropertiesDto;

class ObjectWithCollectionPropertyDto
{
/**
* @var ?Collection<int,ObjectWithScalarPropertiesDto>
*/
public ?Collection $property = null;
}
50 changes: 49 additions & 1 deletion tests/IntegrationTest/TraversableToArrayAccessMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace Rekalogika\Mapper\Tests\IntegrationTest;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Rekalogika\Mapper\Exception\MissingMemberValueTypeException;
use Rekalogika\Mapper\Mapper;
use Rekalogika\Mapper\Tests\Common\AbstractIntegrationTest;
Expand All @@ -22,6 +24,7 @@
use Rekalogika\Mapper\Tests\Fixtures\ArrayLikeDto\ObjectWithArrayAccessPropertyDto;
use Rekalogika\Mapper\Tests\Fixtures\ArrayLikeDto\ObjectWithArrayPropertyDto;
use Rekalogika\Mapper\Tests\Fixtures\ArrayLikeDto\ObjectWithArrayPropertyDtoWithIntKey;
use Rekalogika\Mapper\Tests\Fixtures\ArrayLikeDto\ObjectWithCollectionPropertyDto;
use Rekalogika\Mapper\Tests\Fixtures\Scalar\ObjectWithScalarPropertiesDto;

class TraversableToArrayAccessMappingTest extends AbstractIntegrationTest
Expand Down Expand Up @@ -134,6 +137,28 @@ public function testTraversableToArrayDto(): void
$this->assertEquals(1.1, $one->d);
}

public function testTraversableToCollectionDto(): void
{
$source = new ObjectWithTraversableProperties();

$result = $this->mapper->map($source, ObjectWithCollectionPropertyDto::class);

$this->assertInstanceOf(ObjectWithCollectionPropertyDto::class, $result);
$property = $result->property;
$this->assertInstanceOf(Collection::class, $property);
$this->assertInstanceOf(ArrayCollection::class, $property);

$property = $result->property;

$member = $property?->get(1);
$this->assertInstanceOf(ObjectWithScalarPropertiesDto::class, $member);

$this->assertEquals(1, $member->a);
$this->assertEquals("string", $member->b);
$this->assertEquals(true, $member->c);
$this->assertEquals(1.1, $member->d);
}

public function testArrayToArrayAccessDto(): void
{
$source = new ObjectWithArrayProperty();
Expand Down Expand Up @@ -163,6 +188,29 @@ public function testArrayToArrayDto(): void
$this->assertEquals(1.1, $one->d);
}

public function testArrayToCollectionDto(): void
{
$source = new ObjectWithArrayProperty();

$result = $this->mapper->map($source, ObjectWithCollectionPropertyDto::class);

$this->assertInstanceOf(ObjectWithCollectionPropertyDto::class, $result);
$property = $result->property;
$this->assertInstanceOf(Collection::class, $property);
$this->assertInstanceOf(ArrayCollection::class, $property);

$property = $result->property;

$member = $property?->get(1);
$this->assertInstanceOf(ObjectWithScalarPropertiesDto::class, $member);

$this->assertEquals(1, $member->a);
$this->assertEquals("string", $member->b);
$this->assertEquals(true, $member->c);
$this->assertEquals(1.1, $member->d);
}


//
// preinitialized
//
Expand Down Expand Up @@ -225,7 +273,7 @@ public function testArrayToArrayDtoPreInit(): void
}

//
//
// source has string key, target has int key
//

public function testSourceStringKeyToTargetIntKey(): void
Expand Down
15 changes: 15 additions & 0 deletions tests/IntegrationTest/TraversableToTraversableMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Rekalogika\Mapper\Tests\Common\AbstractIntegrationTest;
use Rekalogika\Mapper\Tests\Fixtures\ArrayLike\ObjectWithArrayProperty;
use Rekalogika\Mapper\Tests\Fixtures\ArrayLike\ObjectWithLazyDoctrineCollectionProperty;
use Rekalogika\Mapper\Tests\Fixtures\ArrayLike\ObjectWithTraversableProperties;
use Rekalogika\Mapper\Tests\Fixtures\ArrayLikeDto\ObjectWithTraversablePropertyDto;
use Rekalogika\Mapper\Tests\Fixtures\Scalar\ObjectWithScalarPropertiesDto;
Expand Down Expand Up @@ -64,4 +65,18 @@ public function testArrayToTraversableDto(): void
$this->assertEquals(1.1, $item->d);
}
}

public function testLazy(): void
{
$source = new ObjectWithLazyDoctrineCollectionProperty();

$result = $this->mapper->map($source, ObjectWithTraversablePropertyDto::class);

$this->assertInstanceOf(ObjectWithTraversablePropertyDto::class, $result);
$this->assertNotNull($result->property);
$this->assertInstanceOf(\Generator::class, $result->property);

$this->expectException(\LogicException::class);
foreach ($result->property as $item);
}
}

0 comments on commit c827cf1

Please sign in to comment.