Skip to content

Commit

Permalink
createFromData
Browse files Browse the repository at this point in the history
  • Loading branch information
mvkasatkin committed Oct 4, 2017
1 parent 3681434 commit d51541c
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 7 deletions.
13 changes: 13 additions & 0 deletions src/entity/AbstractEntityRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions src/entity/AbstractEntityService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/entity/EntityRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 8 additions & 6 deletions tests/core/entity/AbstractEntityRepositoryDbTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Mvkasatkin\mocker\Mocker;
use WebComplete\core\condition\Condition;
use WebComplete\core\condition\ConditionDbParser;
use WebComplete\core\entity\AbstractEntity;
use WebComplete\core\entity\AbstractEntityRepositoryDb;
use WebComplete\core\factory\ObjectFactory;
use WebComplete\core\utils\hydrator\Hydrator;
Expand Down Expand Up @@ -33,12 +33,13 @@ public function testFindById()

$conn = $this->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()
Expand All @@ -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()
Expand Down Expand Up @@ -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;
Expand Down
15 changes: 14 additions & 1 deletion tests/core/entity/AbstractEntityRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,17 @@ public function testCreate()
$aer->create();
}

}
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);
}
}
5 changes: 5 additions & 0 deletions tests/core/entity/AbstractEntityServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit d51541c

Please sign in to comment.