diff --git a/src/entity/AbstractEntityRepository.php b/src/entity/AbstractEntityRepository.php index 4e610f3..f629380 100644 --- a/src/entity/AbstractEntityRepository.php +++ b/src/entity/AbstractEntityRepository.php @@ -38,6 +38,19 @@ public function create(): AbstractEntity return $result; } + /** + * @param array $data + * @param array|null $map + * + * @return AbstractEntity + */ + public function createFromData(array $data, array $map = null): AbstractEntity + { + $result = $this->factory->createFromData($data, $map); + /** @var AbstractEntity $result */ + return $result; + } + /** * Adjust data before save * @param $data diff --git a/src/entity/AbstractEntityService.php b/src/entity/AbstractEntityService.php index 6f4c85c..55af581 100644 --- a/src/entity/AbstractEntityService.php +++ b/src/entity/AbstractEntityService.php @@ -39,6 +39,19 @@ public function create(): AbstractEntity return $this->repository->create(); } + /** + * Proxy method + * + * @param array $data + * @param array|null $map + * + * @return AbstractEntity + */ + public function createFromData(array $data, array $map = null): AbstractEntity + { + return $this->repository->createFromData($data, $map); + } + /** * Proxy method * @param $id diff --git a/src/entity/EntityRepositoryInterface.php b/src/entity/EntityRepositoryInterface.php index 2d01dc3..70487a4 100644 --- a/src/entity/EntityRepositoryInterface.php +++ b/src/entity/EntityRepositoryInterface.php @@ -18,6 +18,14 @@ public function transaction(\Closure $closure); */ public function create(): AbstractEntity; + /** + * @param array $data + * @param array|null $map + * + * @return AbstractEntity + */ + public function createFromData(array $data, array $map = null): AbstractEntity; + /** * @param $id * @return AbstractEntity|null diff --git a/tests/core/entity/AbstractEntityRepositoryDbTest.php b/tests/core/entity/AbstractEntityRepositoryDbTest.php index aad4c54..4be41ae 100644 --- a/tests/core/entity/AbstractEntityRepositoryDbTest.php +++ b/tests/core/entity/AbstractEntityRepositoryDbTest.php @@ -1,10 +1,10 @@ createMock(\Doctrine\DBAL\Connection::class); + $entity = Mocker::create(AbstractEntity::class); $of = $this->createMock(ObjectFactory::class); - $of->method('createFromData')->willReturn(2); + $of->method('createFromData')->willReturn($entity); $rep = $this->createRep($of, null, null, $conn, ['selectQuery']); $rep->expects($this->once())->method('selectQuery')->willReturn($qb); - $this->assertEquals(2, $rep->findById(1)); + $this->assertEquals($entity, $rep->findById(1)); } public function testFindOne() @@ -53,13 +54,14 @@ public function testFindOne() $conn = $this->createMock(\Doctrine\DBAL\Connection::class); + $entity = Mocker::create(AbstractEntity::class); $of = $this->createMock(ObjectFactory::class); - $of->method('createFromData')->with(['a' => 1, 'arr' => [1,2,3], 'arr2' => null], ['a' => 'b'])->willReturn(2); + $of->method('createFromData')->with(['a' => 1, 'arr' => [1,2,3], 'arr2' => null], ['a' => 'b'])->willReturn($entity); $rep = $this->createRep($of, null, null, $conn, ['selectQuery']); $rep->expects($this->once())->method('selectQuery')->willReturn($qb); Mocker::setProperty($rep, 'map', ['a' => 'b']); - $this->assertEquals(2, $rep->findOne(new Condition())); + $this->assertEquals($entity, $rep->findOne(new Condition())); } public function testFindAll() @@ -223,7 +225,7 @@ protected function createRep($of = null, $h = null, $p = null, $c = null, $mock } -class AbstractEntityRepositoryDbTestEntity extends \WebComplete\core\entity\AbstractEntity { +class AbstractEntityRepositoryDbTestEntity extends AbstractEntity { public $a; public $arr; public $arr2; diff --git a/tests/core/entity/AbstractEntityRepositoryTest.php b/tests/core/entity/AbstractEntityRepositoryTest.php index 6ff903a..56513d9 100644 --- a/tests/core/entity/AbstractEntityRepositoryTest.php +++ b/tests/core/entity/AbstractEntityRepositoryTest.php @@ -19,4 +19,17 @@ public function testCreate() $aer->create(); } -} \ No newline at end of file + public function testCreateFromData() + { + $data = [1,2,3]; + $map = ['a' => 'b']; + + $of = $this->createMock(ObjectFactory::class); + $of->expects($this->once())->method('createFromData')->with($data, $map) + ->willReturn($this->createMock(AbstractEntity::class)); + $hydrator = new Hydrator(); + $aer = $this->getMockForAbstractClass(AbstractEntityRepository::class, [$of, $hydrator]); + /** @var AbstractEntityRepository $aer */ + $aer->createFromData($data, $map); + } +} diff --git a/tests/core/entity/AbstractEntityServiceTest.php b/tests/core/entity/AbstractEntityServiceTest.php index 36405a7..d3a6650 100644 --- a/tests/core/entity/AbstractEntityServiceTest.php +++ b/tests/core/entity/AbstractEntityServiceTest.php @@ -21,6 +21,11 @@ public function testProxyCreate() $this->createAService(['create'])->create(); } + public function testProxyCreateFromData() + { + $this->createAService(['createFromData'])->createFromData([], []); + } + public function testProxyFindById() { $this->createAService(['findById'])->findById(1);