Skip to content

Commit a05a31c

Browse files
author
Marco Bunge
committed
Update unit of work
1 parent 3624ed1 commit a05a31c

File tree

3 files changed

+82
-15
lines changed

3 files changed

+82
-15
lines changed

src/UnitOfWork.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ final class UnitOfWork
3030
/**
3131
* @var object[]
3232
*/
33-
private $completed = [];
33+
private $modified = [];
3434

3535
/**
36-
* @var \SplObjectStorage
36+
* @var array
3737
*/
38-
private $completedState = null;
38+
private $processed = [];
3939

4040
/**
4141
* @var Connection
@@ -55,15 +55,14 @@ final class UnitOfWork
5555
public function __construct(Connection $connection)
5656
{
5757
$this->connection = $connection;
58-
$this->completedState = new \SplObjectStorage();
5958
}
6059

6160
/**
6261
* @return \object[]
6362
*/
64-
public function getCompleted()
63+
public function getModified()
6564
{
66-
return $this->completed;
65+
return $this->modified;
6766
}
6867

6968
/**
@@ -219,8 +218,10 @@ protected function process($label, &$entities, callable $task)
219218
{
220219
foreach ($entities as $key => $entity) {
221220
$task($entity);
222-
$this->completedState[$entity] = $label;
223-
$this->completed[] = $entity;
221+
$this->processed[$label][] = $entity;
222+
if(IdentityMap::REMOVED !== $label){
223+
$this->modified[] = $entity;
224+
}
224225
unset($entities[$key]);
225226
}
226227
}

tests/IntegrationTest.php

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use Hawkbit\Database\ConnectionManager;
1515
use Hawkbit\Database\Tests\Stubs\PostEntity;
1616
use Hawkbit\Database\Tests\Stubs\PostMapper;
17+
use Hawkbit\Database\Tests\Stubs\UserEntity;
18+
use Hawkbit\Database\Tests\Stubs\UserMapper;
1719

1820
class IntegrationTest extends \PHPUnit_Framework_TestCase
1921
{
@@ -35,6 +37,7 @@ protected function setUp()
3537
]);
3638

3739
$connection->getMapperLocator()->register(PostMapper::class);
40+
$connection->getMapperLocator()->register(UserMapper::class);
3841

3942
$connection->exec('CREATE TABLE post (id INTEGER PRIMARY KEY, content TEXT)');
4043
$connection->exec('CREATE TABLE user (id INTEGER PRIMARY KEY, username VARCHAR)');
@@ -109,8 +112,11 @@ public function testMapperIntegration()
109112
public function testUoWIntegration()
110113
{
111114
$connection = $this->connection;
112-
/** @var PostMapper $mapper */
113-
$mapper = $connection->loadMapper(PostEntity::class);
115+
/** @var PostMapper $postMapper */
116+
$postMapper = $connection->loadMapper(PostEntity::class);
117+
118+
/** @var UserMapper $userMapper */
119+
$userMapper = $connection->loadMapper(UserEntity::class);
114120
$unitOfWork = $connection->createUnitOfWork();
115121

116122
$contentFromAnyOtherSource = [
@@ -119,25 +125,57 @@ public function testUoWIntegration()
119125
'Hello 2',
120126
];
121127

128+
$userFromAnyOtherSource = [
129+
'Jake',
130+
'Andy',
131+
'Dave',
132+
];
133+
122134
foreach ($contentFromAnyOtherSource as $content) {
123135
/** @var PostEntity $entity */
124-
$entity = $mapper->createEntity();
136+
$entity = $postMapper->createEntity();
125137
$entity->setContent($content);
126138
$unitOfWork->create($entity);
127139
}
128140

141+
foreach ($userFromAnyOtherSource as $content) {
142+
/** @var UserEntity $entity */
143+
$entity = $userMapper->createEntity();
144+
$entity->setUsername($content);
145+
$unitOfWork->create($entity);
146+
}
147+
129148
$this->assertTrue($unitOfWork->commit());
130149

131150
// test find commited entities
132151
foreach ($contentFromAnyOtherSource as $content) {
133-
$foundEntity = $mapper->select(function (QueryBuilder $query) use ($content) {
152+
/** @var PostEntity $foundPostEntity */
153+
$foundPostEntity = $postMapper->select(function (QueryBuilder $query) use ($content) {
134154
$query->where($query->expr()->eq('content', $query->createPositionalParameter($content, Type::STRING)));
135155
}, ['*'], true);
136156

137157
// test object has been found in database
138-
$foundResult = $mapper->getGateway()->select()->where('content = ?')->setParameter(0, $content, Type::STRING)->execute()->fetch();
139-
$this->assertEquals((int)$foundResult['id'], $foundEntity->getId());
140-
$this->assertEquals($foundResult['content'], $foundEntity->getContent());
158+
$foundResult = $postMapper->getGateway()->select()->where('content = ?')->setParameter(0, $content, Type::STRING)->execute()->fetch();
159+
$this->assertEquals((int)$foundResult['id'], $foundPostEntity->getId());
160+
$this->assertEquals($foundResult['content'], $foundPostEntity->getContent());
161+
}
162+
163+
// test find commited entities
164+
foreach ($userFromAnyOtherSource as $user) {
165+
166+
/** @var UserEntity $foundUserEntity */
167+
$foundUserEntity = $userMapper->select(function (QueryBuilder $query) use ($user) {
168+
$query->where($query->expr()->eq('username', $query->createPositionalParameter($user, Type::STRING)));
169+
}, ['*'], true);
170+
171+
// test object has been found in database
172+
$foundResult = $userMapper->getGateway()->select()->where('username = ?')->setParameter(0, $user, Type::STRING)->execute()->fetch();
173+
$this->assertEquals((int)$foundResult['id'], $foundUserEntity->getId());
174+
$this->assertEquals($foundResult['username'], $foundUserEntity->getUsername());
175+
}
176+
177+
if(true){
178+
141179
}
142180
}
143181
}

tests/Stubs/UserMapper.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: marco.bunge
5+
* Date: 05.12.2016
6+
* Time: 10:05
7+
*/
8+
9+
namespace Hawkbit\Database\Tests\Stubs;
10+
11+
12+
use Doctrine\DBAL\Schema\Column;
13+
use Doctrine\DBAL\Types\Type;
14+
use Hawkbit\Database\AbstractMapper;
15+
16+
class UserMapper extends AbstractMapper
17+
{
18+
public function define()
19+
{
20+
$this->tableName = 'user';
21+
$this->columns = [
22+
new Column('id', Type::getType(Type::INTEGER)),
23+
new Column('username', Type::getType(Type::TEXT)),
24+
];
25+
$this->primaryKey = ['id'];
26+
$this->entityClass = UserEntity::class;
27+
}
28+
}

0 commit comments

Comments
 (0)