Skip to content

Commit

Permalink
Optimize PersistenceCachePurger and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrerom committed Mar 14, 2019
1 parent 544c676 commit c238da5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
12 changes: 6 additions & 6 deletions bundle/Cache/PersistenceCachePurger.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ public function content($locationIds = null, array $contentIds = null)
$tags[] = 'location-' . $id;
$tags[] = 'urlAlias-location-' . $id;
$tags[] = 'urlAlias-location-path-' . $id;
}

if (empty($contentIds)) {
// if caller did not provide affected content id's, then try to load location to get it
try {
$tags[] = 'content-' . $this->locationHandler->load($id)->contentId;
} catch (APINotFoundException $e) {
}
// if caller did not provide affected content id's, then try to load location to get it
if (empty($contentIds)) {
$contentIds = [];
foreach ($this->locationHandler->loadList($locationIds) as $location) {
$contentIds[] = $location->contentId;
}
}

Expand Down
33 changes: 17 additions & 16 deletions bundle/Tests/Cache/PersistenceCachePurgerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ public function testNotFoundLocation()
$id = 'locationIdThatDoesNotExist';
$this->locationHandler
->expects($this->once())
->method('load')
->will($this->throwException(new NotFoundException('location', $id)));
->method('loadList')
->with([$id])
->willReturn([]);

$this->cachePurger->content($id);
}
Expand Down Expand Up @@ -149,18 +150,17 @@ public function testClearContent()
$locationId3 = 3;
$contentId3 = 30;

$locationIds = array($locationId1, $locationId2, $locationId3);

$this->locationHandler
->expects($this->exactly(3))
->method('load')
->will(
$this->returnValueMap(
array(
array($locationId1, $this->buildLocation($locationId1, $contentId1)),
array($locationId2, $this->buildLocation($locationId2, $contentId2)),
array($locationId3, $this->buildLocation($locationId3, $contentId3)),
)
)
);
->expects($this->once())
->method('loadList')
->with($locationIds)
->willReturn([
$locationId1 => $this->buildLocation($locationId1, $contentId1),
$locationId2 => $this->buildLocation($locationId2, $contentId2),
$locationId3 => $this->buildLocation($locationId3, $contentId3),
]);

$this->cacheService
->expects($this->any())
Expand All @@ -180,7 +180,6 @@ public function testClearContent()
)
);

$locationIds = array($locationId1, $locationId2, $locationId3);
$this->assertSame($locationIds, $this->cachePurger->content($locationIds));
}

Expand All @@ -194,8 +193,10 @@ public function testClearOneContent()

$this->locationHandler
->expects($this->once())
->method('load')
->will($this->returnValue($this->buildLocation($locationId, $contentId)));
->method('loadList')
->with([$locationId])
->willReturn([$locationId => $this->buildLocation($locationId, $contentId)]);

$this->cacheService
->expects($this->any())
->method('clear')
Expand Down

0 comments on commit c238da5

Please sign in to comment.