Skip to content

Commit 35d6261

Browse files
committed
test: Add hook to override the default Context.
1 parent 9648694 commit 35d6261

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 0.9.0
44

55
* fix(`MapperInterface`): Fix type-hint mismatch.
6+
* test: Add hook to override the default `Context`.
67

78
## 0.8.1
89

tests/Common/AbstractFrameworkTest.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Doctrine\ORM\Tools\SchemaTool;
1818
use Doctrine\Persistence\ManagerRegistry;
1919
use PHPUnit\Framework\TestCase;
20+
use Rekalogika\Mapper\Context\Context;
2021
use Rekalogika\Mapper\Debug\TraceableTransformer;
2122
use Rekalogika\Mapper\MapperInterface;
2223
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -34,7 +35,11 @@ public function setUp(): void
3435
$kernel = new TestKernel();
3536
$kernel->boot();
3637
$this->container = $kernel->getContainer();
37-
$this->mapper = $this->get(MapperInterface::class);
38+
39+
$this->mapper = new MapperDecorator(
40+
$this->get(MapperInterface::class),
41+
$this->getMapperContext()
42+
);
3843
}
3944

4045
/**
@@ -62,6 +67,11 @@ public function get(string $serviceId): object
6267
return $result;
6368
}
6469

70+
protected function getMapperContext(): Context
71+
{
72+
return Context::create();
73+
}
74+
6575
/**
6676
* @param class-string $class
6777
*/

tests/Common/MapperDecorator.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of rekalogika/mapper package.
7+
*
8+
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
9+
*
10+
* For the full copyright and license information, please view the LICENSE file
11+
* that was distributed with this source code.
12+
*/
13+
14+
namespace Rekalogika\Mapper\Tests\Common;
15+
16+
use Rekalogika\Mapper\Context\Context;
17+
use Rekalogika\Mapper\MapperInterface;
18+
19+
final class MapperDecorator implements MapperInterface
20+
{
21+
public function __construct(
22+
private MapperInterface $decorated,
23+
private Context $defaultContext
24+
) {
25+
}
26+
27+
/**
28+
* @template T of object
29+
* @param class-string<T>|T $target
30+
* @return T
31+
*/
32+
public function map(
33+
object $source,
34+
object|string $target,
35+
?Context $context = null
36+
): object {
37+
return $this->decorated->map($source, $target, $context ?? $this->defaultContext);
38+
}
39+
}

0 commit comments

Comments
 (0)