Skip to content

Commit d87c999

Browse files
oliverkleeSam Tuke
authored and
Sam Tuke
committed
[FEATURE] Repository.remove (#278)
This allows the removal of entities from controllers without needing a refererence to the entity manager. Also remove two redundant touchDatabaseTable calls from the integration tests.
1 parent 78502d2 commit d87c999

File tree

7 files changed

+112
-2
lines changed

7 files changed

+112
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
77
## x.y.z
88

99
### Added
10+
- Repository.remove (#278)
1011
- Interface for domain models (#274)
1112
- Trait for database tests and abstract web test (#264)
1213
- Repository methods for querying subscriptions (#253)

src/Domain/Repository/AbstractRepository.php

+16
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,20 @@ public function save(DomainModel $model)
2828
$this->getEntityManager()->persist($model);
2929
$this->getEntityManager()->flush();
3030
}
31+
32+
/**
33+
* Removes $model and flushes the entity manager change list.
34+
*
35+
* This method allows controllers to not depend on the entity manager, but only on the repositories instead,
36+
* following the Law of Demeter.
37+
*
38+
* @param DomainModel $model
39+
*
40+
* @return void
41+
*/
42+
public function remove(DomainModel $model)
43+
{
44+
$this->getEntityManager()->remove($model);
45+
$this->getEntityManager()->flush();
46+
}
3147
}

tests/Integration/Domain/Repository/Identity/AdministratorRepositoryTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,23 @@ public function savePersistsAndFlushesModel()
207207

208208
static::assertSame($model, $this->subject->find($model->getId()));
209209
}
210+
211+
/**
212+
* @test
213+
*/
214+
public function removeRemovesModel()
215+
{
216+
$this->getDataSet()->addTable(static::TABLE_NAME, __DIR__ . '/../Fixtures/Administrator.csv');
217+
$this->applyDatabaseChanges();
218+
219+
/** @var Administrator[] $allModels */
220+
$allModels = $this->subject->findAll();
221+
$numberOfModelsBeforeRemove = count($allModels);
222+
$firstModel = $allModels[0];
223+
224+
$this->subject->remove($firstModel);
225+
226+
$numberOfModelsAfterRemove = count($this->subject->findAll());
227+
static::assertSame(1, $numberOfModelsBeforeRemove - $numberOfModelsAfterRemove);
228+
}
210229
}

tests/Integration/Domain/Repository/Identity/AdministratorTokenRepositoryTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -258,4 +258,23 @@ public function savePersistsAndFlushesModel()
258258

259259
static::assertSame($model, $this->subject->find($model->getId()));
260260
}
261+
262+
/**
263+
* @test
264+
*/
265+
public function removeRemovesModel()
266+
{
267+
$this->getDataSet()->addTable(static::TABLE_NAME, __DIR__ . '/../Fixtures/DetachedAdministratorTokens.csv');
268+
$this->applyDatabaseChanges();
269+
270+
/** @var AdministratorToken[] $allModels */
271+
$allModels = $this->subject->findAll();
272+
$numberOfModelsBeforeRemove = count($allModels);
273+
$firstModel = $allModels[0];
274+
275+
$this->subject->remove($firstModel);
276+
277+
$numberOfModelsAfterRemove = count($this->subject->findAll());
278+
static::assertSame(1, $numberOfModelsBeforeRemove - $numberOfModelsAfterRemove);
279+
}
261280
}

tests/Integration/Domain/Repository/Messaging/SubscriberListRepositoryTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -290,4 +290,23 @@ public function removeAlsoRemovesAssociatedSubscriptions()
290290
$numberOfRemovedSubscriptions = $initialNumberOfSubscriptions - $newNumberOfSubscriptions;
291291
static::assertSame($numberOfAssociatedSubscriptions, $numberOfRemovedSubscriptions);
292292
}
293+
294+
/**
295+
* @test
296+
*/
297+
public function removeRemovesModel()
298+
{
299+
$this->getDataSet()->addTable(static::TABLE_NAME, __DIR__ . '/../Fixtures/SubscriberList.csv');
300+
$this->applyDatabaseChanges();
301+
302+
/** @var SubscriberList[] $allModels */
303+
$allModels = $this->subject->findAll();
304+
$numberOfModelsBeforeRemove = count($allModels);
305+
$firstModel = $allModels[0];
306+
307+
$this->subject->remove($firstModel);
308+
309+
$numberOfModelsAfterRemove = count($this->subject->findAll());
310+
static::assertSame(1, $numberOfModelsBeforeRemove - $numberOfModelsAfterRemove);
311+
}
293312
}

tests/Integration/Domain/Repository/Subscription/SubscriberRepositoryTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -295,4 +295,23 @@ public function removeAlsoRemovesAssociatedSubscriptions()
295295
$numberOfRemovedSubscriptions = $initialNumberOfSubscriptions - $newNumberOfSubscriptions;
296296
static::assertSame($numberOfAssociatedSubscriptions, $numberOfRemovedSubscriptions);
297297
}
298+
299+
/**
300+
* @test
301+
*/
302+
public function removeRemovesModel()
303+
{
304+
$this->getDataSet()->addTable(static::TABLE_NAME, __DIR__ . '/../Fixtures/Subscriber.csv');
305+
$this->applyDatabaseChanges();
306+
307+
/** @var Subscriber[] $allModels */
308+
$allModels = $this->subject->findAll();
309+
$numberOfModelsBeforeRemove = count($allModels);
310+
$firstModel = $allModels[0];
311+
312+
$this->subject->remove($firstModel);
313+
314+
$numberOfModelsAfterRemove = count($this->subject->findAll());
315+
static::assertSame(1, $numberOfModelsBeforeRemove - $numberOfModelsAfterRemove);
316+
}
298317
}

tests/Integration/Domain/Repository/Subscription/SubscriptionRepositoryTest.php

+19-2
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ public function findBySubscriberFindsSubscriptionOnlyWithTheGivenSubscriber()
184184
{
185185
$this->getDataSet()->addTable(static::SUBSCRIBER_TABLE_NAME, __DIR__ . '/../Fixtures/Subscriber.csv');
186186
$this->getDataSet()->addTable(static::TABLE_NAME, __DIR__ . '/../Fixtures/Subscription.csv');
187-
$this->touchDatabaseTable(static::TABLE_NAME);
188187
$this->applyDatabaseChanges();
189188

190189
/** @var Subscriber $subscriber */
@@ -205,7 +204,6 @@ public function findBySubscriberListFindsSubscriptionOnlyWithTheGivenSubscriberL
205204
{
206205
$this->getDataSet()->addTable(static::SUBSCRIBER_TABLE_NAME, __DIR__ . '/../Fixtures/Subscriber.csv');
207206
$this->getDataSet()->addTable(static::TABLE_NAME, __DIR__ . '/../Fixtures/Subscription.csv');
208-
$this->touchDatabaseTable(static::TABLE_NAME);
209207
$this->applyDatabaseChanges();
210208

211209
/** @var SubscriberList $subscriberList */
@@ -218,4 +216,23 @@ public function findBySubscriberListFindsSubscriptionOnlyWithTheGivenSubscriberL
218216
static::assertSame($subscriberList, $subscription->getSubscriberList());
219217
}
220218
}
219+
220+
/**
221+
* @test
222+
*/
223+
public function removeRemovesModel()
224+
{
225+
$this->getDataSet()->addTable(static::TABLE_NAME, __DIR__ . '/../Fixtures/Subscription.csv');
226+
$this->applyDatabaseChanges();
227+
228+
/** @var Subscription[] $allModels */
229+
$allModels = $this->subject->findAll();
230+
$numberOfModelsBeforeRemove = count($allModels);
231+
$firstModel = $allModels[0];
232+
233+
$this->subject->remove($firstModel);
234+
235+
$numberOfModelsAfterRemove = count($this->subject->findAll());
236+
static::assertSame(1, $numberOfModelsBeforeRemove - $numberOfModelsAfterRemove);
237+
}
221238
}

0 commit comments

Comments
 (0)