Skip to content

Commit 37391bc

Browse files
authored
feat(mapper): Add clock getter (#FRAM-196) (#107)
1 parent ed73243 commit 37391bc

3 files changed

Lines changed: 25 additions & 2 deletions

File tree

‎CHANGELOG‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
v2.3.0
22
------
33

4+
* Feat: Add `Bdf\Prime\Mapper\Mapper::clock()` to get the clock instance
45
* Feat: Add transaction handling methods on `Bdf\Prime\Connection\TransactionManagerInterface`
56
* Change: `EntityRepository::transaction()` do not rely on nested transaction if not enabled on the connection
67
* Feat: Add custom criteria system :

‎src/Mapper/Mapper.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Bdf\Prime\Behaviors\BehaviorInterface;
66
use Bdf\Prime\Cache\CacheInterface;
77
use Bdf\Prime\Clock\ClockAwareInterface;
8+
use Bdf\Prime\Clock\NativeClock;
89
use Bdf\Prime\Entity\Criteria;
910
use Bdf\Prime\Entity\Hydrator\MapperHydrator;
1011
use Bdf\Prime\Entity\Hydrator\MapperHydratorInterface;
@@ -480,6 +481,15 @@ final public function setClock(ClockInterface $clock): void
480481
$this->clock = $clock;
481482
}
482483

484+
/**
485+
* Get the current clock instance
486+
* If no clock has been set using {@see Mapper::setClock()}, the default clock {@see NativeClock::instance()} will be returned
487+
*/
488+
final public function clock(): ClockInterface
489+
{
490+
return $this->clock ?? NativeClock::instance();
491+
}
492+
483493
/**
484494
* Define the criteria class
485495
* By default, it is the entity class with "Criteria" suffix if exists, else base Criteria class

‎tests/Mapper/MapperTest.php‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Bdf\Prime\Behaviors\Behavior;
77
use Bdf\Prime\Bench\HydratorGeneration;
88
use Bdf\Prime\Clock\ClockAwareInterface;
9+
use Bdf\Prime\Clock\NativeClock;
910
use Bdf\Prime\Customer;
1011
use Bdf\Prime\CustomerCriteria;
1112
use Bdf\Prime\CustomerMapper;
@@ -16,7 +17,6 @@
1617
use Bdf\Prime\Entity\Hydrator\MapperHydratorInterface;
1718
use Bdf\Prime\Exception\DBALException;
1819
use Bdf\Prime\IdGenerators\AbstractGenerator;
19-
use Bdf\Prime\IdGenerators\GeneratorInterface;
2020
use Bdf\Prime\IdGenerators\GuidGenerator;
2121
use Bdf\Prime\IdGenerators\NullGenerator;
2222
use Bdf\Prime\IdGenerators\TableGenerator;
@@ -83,8 +83,20 @@ public function test_default()
8383
$this->assertTrue($mapper->hasSchemaManager());
8484
$this->assertInstanceOf(MapperHydrator::class, $mapper->hydrator());
8585
$this->assertNull($mapper->allowUnknownAttribute());
86+
$this->assertSame(NativeClock::instance(), $mapper->clock());
8687
}
87-
88+
89+
public function test_setClock()
90+
{
91+
$mapper = new TestEntityMapper(Prime::service(), TestEntity::class);
92+
$mapper->build();
93+
94+
$clock = $this->createMock(ClockInterface::class);
95+
$mapper->setClock($clock);
96+
97+
$this->assertSame($clock, $mapper->clock());
98+
}
99+
88100
/**
89101
*
90102
*/

0 commit comments

Comments
 (0)