-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
153 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/Feed/Application/Listener/Article/InvalidateArticleCacheListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace App\Feed\Application\Listener\Article; | ||
|
||
use App\Common\Infrastructure\Messenger\EventBus\AsEventSubscriber; | ||
use App\Feed\Application\Event\Article\ArticleUpdatedEvent; | ||
use App\Feed\Infrastructure\Cache\FeedCacheKeys; | ||
use Symfony\Contracts\Cache\CacheInterface; | ||
|
||
final readonly class InvalidateArticleCacheListener | ||
{ | ||
public function __construct( | ||
private CacheInterface $cache, | ||
) { | ||
} | ||
|
||
#[AsEventSubscriber] | ||
public function onArticleUpdated(ArticleUpdatedEvent $event): void | ||
{ | ||
$this->cache->delete(sprintf(FeedCacheKeys::ARTICLE_WITH_ARTICLE_ID->value, $event->articleId)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
tests/Unit/Feed/Application/Listener/Article/InvalidateArticleCacheListenerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace Unit\Feed\Application\Listener\Article; | ||
|
||
use App\Feed\Application\Event\Article\ArticleUpdatedEvent; | ||
use App\Feed\Application\Listener\Article\InvalidateArticleCacheListener; | ||
use App\Feed\Infrastructure\Cache\FeedCacheKeys; | ||
use Dev\Feed\Factory\ArticleFactory; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Cache\Adapter\ArrayAdapter; | ||
use Symfony\Contracts\Cache\CacheInterface; | ||
use Symfony\Contracts\Cache\ItemInterface; | ||
|
||
final class InvalidateArticleCacheListenerTest extends TestCase | ||
{ | ||
private InvalidateArticleCacheListener $listener; | ||
private ArrayAdapter $cache; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->listener = new InvalidateArticleCacheListener( | ||
$this->cache = new ArrayAdapter(), | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_should_delete_the_cache_entry_when_the_article_is_updated(): void | ||
{ | ||
// Arrange | ||
$article = (new ArticleFactory())->create(); | ||
$cacheKey = sprintf(FeedCacheKeys::ARTICLE_WITH_ARTICLE_ID->value, $article->getId()); | ||
|
||
$this->cache->get( | ||
$cacheKey, | ||
fn (ItemInterface $item) => $article, | ||
); | ||
|
||
// Act | ||
$this->listener->onArticleUpdated(new ArticleUpdatedEvent($article->getId())); | ||
|
||
// Assert | ||
self::assertFalse($this->cache->hasItem($cacheKey)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters