Skip to content

Commit 9adc1ba

Browse files
authored
Small contract fixes (#10)
* Add object hydration method * Fix tests - findBy no more returns the collection * createLazyCollection method as replacement
1 parent 214c628 commit 9adc1ba

File tree

4 files changed

+50
-22
lines changed

4 files changed

+50
-22
lines changed

src/Bankiru/Api/Doctrine/EntityRepository.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Bankiru\Api\Doctrine\Mapping\ApiMetadata;
66
use Bankiru\Api\Doctrine\Proxy\ApiCollection;
7+
use Doctrine\Common\Collections\Collection;
78
use Doctrine\Common\Persistence\ObjectRepository;
89
use ScayTrase\Api\Rpc\RpcClientInterface;
910

@@ -76,7 +77,7 @@ public function findAll()
7677
*/
7778
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
7879
{
79-
return new ApiCollection($this->manager, $this->metadata, [$criteria, $orderBy, $limit, $offset]);
80+
return $this->createLazyCollection($criteria, $orderBy, $limit, $offset)->toArray();
8081
}
8182

8283
/**
@@ -101,6 +102,19 @@ public function getManager()
101102
return $this->manager;
102103
}
103104

105+
/**
106+
* @param array $criteria
107+
* @param array|null $orderBy
108+
* @param int|null $limit
109+
* @param int|null $offset
110+
*
111+
* @return Collection
112+
*/
113+
public function createLazyCollection(array $criteria, array $orderBy = null, $limit = null, $offset = null)
114+
{
115+
return new ApiCollection($this->manager, $this->metadata, [$criteria, $orderBy, $limit, $offset]);
116+
}
117+
104118
/**
105119
* @return RpcClientInterface
106120
*/
@@ -116,4 +130,16 @@ protected function getMetadata()
116130
{
117131
return $this->metadata;
118132
}
133+
134+
/**
135+
* Hydrates object from given data or merges it to already fetched object
136+
*
137+
* @param mixed $data
138+
*
139+
* @return object
140+
*/
141+
protected function hydrateObject($data)
142+
{
143+
return $this->getManager()->getUnitOfWork()->getOrCreateEntity($this->getClassName(), $data);
144+
}
119145
}

src/Bankiru/Api/Doctrine/Proxy/ApiCollection.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Bankiru\Api\Doctrine\Proxy;
44

5-
use Bankiru\Api\Doctrine\EntityManager;
5+
use Bankiru\Api\Doctrine\ApiEntityManager;
66
use Bankiru\Api\Doctrine\Mapping\ApiMetadata;
77
use Doctrine\Common\Collections\AbstractLazyCollection;
88
use Doctrine\Common\Collections\ArrayCollection;
@@ -11,7 +11,7 @@
1111

1212
class ApiCollection extends AbstractLazyCollection
1313
{
14-
/** @var EntityManager */
14+
/** @var ApiEntityManager */
1515
private $manager;
1616
/** @var ApiMetadata */
1717
private $metadata;
@@ -25,13 +25,13 @@ class ApiCollection extends AbstractLazyCollection
2525
/**
2626
* ApiCollection constructor.
2727
*
28-
* @param EntityManager $manager
29-
* @param ApiMetadata $class
30-
* @param array $searchArgs
31-
* @param Collection $collection
28+
* @param ApiEntityManager $manager
29+
* @param ApiMetadata $class
30+
* @param array $searchArgs
31+
* @param Collection $collection
3232
*/
3333
public function __construct(
34-
EntityManager $manager,
34+
ApiEntityManager $manager,
3535
ApiMetadata $class,
3636
array $searchArgs,
3737
Collection $collection = null
@@ -46,7 +46,7 @@ public function __construct(
4646

4747
/**
4848
* @param object $owner
49-
* @param $assoc
49+
* @param array $assoc
5050
*/
5151
public function setOwner($owner, array $assoc)
5252
{

src/Bankiru/Api/Tests/CollectionLoadingTest.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,27 @@
22

33
namespace Bankiru\Api\Tests;
44

5+
use Bankiru\Api\Doctrine\EntityRepository;
56
use Bankiru\Api\Doctrine\Proxy\ApiCollection;
67
use Bankiru\Api\Test\Entity\Sub\SubEntity;
78
use Doctrine\Common\Collections\ArrayCollection;
89
use GuzzleHttp\Psr7\Response;
910

1011
class CollectionLoadingTest extends AbstractEntityManagerTest
1112
{
12-
protected function getClientNames()
13-
{
14-
return [self::DEFAULT_CLIENT, 'test-reference-client'];
15-
}
16-
1713
public function testLazyCollections()
1814
{
15+
/** @var EntityRepository $repository */
1916
$repository = $this->getManager()->getRepository(SubEntity::class);
20-
/** @var SubEntity[]|ArrayCollection|ApiCollection $entities */
21-
$entities = $repository->findBy(['subPayload' => 'sub-payload']);
17+
/** @var ApiCollection $collection */
18+
$collection = $repository->createLazyCollection(['subPayload' => 'sub-payload']);
2219

23-
self::assertInstanceOf(ApiCollection::class, $entities);
24-
self::assertFalse($entities->isInitialized());
20+
self::assertInstanceOf(ApiCollection::class, $collection);
21+
self::assertFalse($collection->isInitialized());
2522

2623
try {
27-
$entities->count();
24+
$collection->count();
25+
self::fail('Should fail');
2826
} catch (\OutOfBoundsException $exception) {
2927
self::assertEquals('Mock queue is empty', $exception->getMessage());
3028
}
@@ -69,7 +67,7 @@ public function testFindBy()
6967

7068
/** @var SubEntity[]|ArrayCollection $entities */
7169
$entities = $repository->findBy(['subPayload' => 'sub-payload']);
72-
self::assertInstanceOf(\Countable::class, $entities);
70+
self::assertInternalType('array', $entities);
7371
self::assertCount(3, $entities);
7472

7573
foreach ($entities as $entity) {
@@ -83,4 +81,9 @@ public function testFindBy()
8381
}
8482
}
8583
}
84+
85+
protected function getClientNames()
86+
{
87+
return [self::DEFAULT_CLIENT, 'test-reference-client'];
88+
}
8689
}

src/Bankiru/Api/Tests/ReferenceLoadingTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,7 @@ public function testFindByReference()
191191
self::assertCount(1, $children);
192192
/** @var TestEntity $child */
193193

194-
$childrenArray = $children->toArray();
195-
$child = array_shift($childrenArray);
194+
$child = array_shift($children);
196195

197196
self::assertEquals($parent, $child->getParent());
198197
self::assertEquals($parent->getPayload(), $child->getParent()->getPayload());

0 commit comments

Comments
 (0)