From 864819a8239d2287f6ec5a8bfd7d553498e29ec4 Mon Sep 17 00:00:00 2001 From: Anton Fedurtsya Date: Tue, 9 Jan 2024 21:12:13 +0200 Subject: [PATCH] Move filter factory dependency one level up from synchronizer Signed-off-by: Anton Fedurtsya --- README.md | 1 + src/Integration/Command/UpdateTypeCommand.php | 5 +++- src/Integration/Service/Synchronizer.php | 7 +++--- .../Service/SynchronizerInterface.php | 4 ++- .../Command/UpdateTypeCommandTest.php | 25 ++++++++++++++++--- .../Integration/Service/SynchronizerTest.php | 6 +---- 6 files changed, 34 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 1478389..9f5658c 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,7 @@ Urls are updated and added during the "fa:sitemap:update" command. The sitemap is generated by "fa:sitemap:generate" command. ```shell +./vendor/bin/oe-console fa:sitemap:update general ./vendor/bin/oe-console fa:sitemap:update content ./vendor/bin/oe-console fa:sitemap:update category ./vendor/bin/oe-console fa:sitemap:update product diff --git a/src/Integration/Command/UpdateTypeCommand.php b/src/Integration/Command/UpdateTypeCommand.php index cbd4bbb..1aa9939 100644 --- a/src/Integration/Command/UpdateTypeCommand.php +++ b/src/Integration/Command/UpdateTypeCommand.php @@ -9,6 +9,7 @@ namespace FreshAdvance\Sitemap\Integration\Command; +use FreshAdvance\Sitemap\Integration\Service\FilterFactoryInterface; use FreshAdvance\Sitemap\Integration\Service\SynchronizerInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; @@ -19,6 +20,7 @@ class UpdateTypeCommand extends Command { public function __construct( protected SynchronizerInterface $synchronizer, + protected FilterFactoryInterface $filterFactory, ) { parent::__construct(); } @@ -36,8 +38,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int { /** @var string $type */ $type = $input->getArgument('type'); + $filter = $this->filterFactory->getFilter($type); - $updateCount = $this->synchronizer->updateTypeUrls($type); + $updateCount = $this->synchronizer->updateUrlsByFilter($filter); $output->writeln("Updated items: " . $updateCount); return Command::SUCCESS; diff --git a/src/Integration/Service/Synchronizer.php b/src/Integration/Service/Synchronizer.php index 79a0214..bb52cb0 100644 --- a/src/Integration/Service/Synchronizer.php +++ b/src/Integration/Service/Synchronizer.php @@ -9,20 +9,19 @@ namespace FreshAdvance\Sitemap\Integration\Service; +use FreshAdvance\Sitemap\Integration\Contract\ChangeFilterInterface; use FreshAdvance\Sitemap\Url\Repository\UrlRepositoryInterface; class Synchronizer implements SynchronizerInterface { public function __construct( - private FilterFactoryInterface $filterService, private UrlRepositoryInterface $urlRepository ) { } - public function updateTypeUrls(string $type): int + public function updateUrlsByFilter(ChangeFilterInterface $changeFilter): int { - $filter = $this->filterService->getFilter($type); - $urls = $filter->getUpdatedUrls(100); + $urls = $changeFilter->getUpdatedUrls(100); $count = 0; foreach ($urls as $oneObjectUrl) { diff --git a/src/Integration/Service/SynchronizerInterface.php b/src/Integration/Service/SynchronizerInterface.php index 975364f..024b846 100644 --- a/src/Integration/Service/SynchronizerInterface.php +++ b/src/Integration/Service/SynchronizerInterface.php @@ -7,7 +7,9 @@ namespace FreshAdvance\Sitemap\Integration\Service; +use FreshAdvance\Sitemap\Integration\Contract\ChangeFilterInterface; + interface SynchronizerInterface { - public function updateTypeUrls(string $type): int; + public function updateUrlsByFilter(ChangeFilterInterface $changeFilter): int; } diff --git a/tests/Integration/Integration/Command/UpdateTypeCommandTest.php b/tests/Integration/Integration/Command/UpdateTypeCommandTest.php index 2a70f89..6d67d91 100644 --- a/tests/Integration/Integration/Command/UpdateTypeCommandTest.php +++ b/tests/Integration/Integration/Command/UpdateTypeCommandTest.php @@ -9,7 +9,11 @@ namespace FreshAdvance\Sitemap\Tests\Integration\Integration\Command; +use FreshAdvance\Sitemap\Integration\Command\UpdateTypeCommand; +use FreshAdvance\Sitemap\Integration\Contract\ChangeFilterInterface; +use FreshAdvance\Sitemap\Integration\Service\FilterFactoryInterface; use FreshAdvance\Sitemap\Integration\Service\Synchronizer; +use FreshAdvance\Sitemap\Integration\Service\SynchronizerInterface; use PHPUnit\Framework\TestCase; use Symfony\Component\Console\Tester\CommandTester; @@ -20,13 +24,28 @@ class UpdateTypeCommandTest extends TestCase { public function testUpdateTypeUrls(): void { - $command = new \FreshAdvance\Sitemap\Integration\Command\UpdateTypeCommand( + $command = $this->getSut( synchronizer: $dataSynchronizerMock = $this->createMock(Synchronizer::class), + filterFactory: $filterFactoryMock = $this->createMock(FilterFactoryInterface::class), ); - $dataSynchronizerMock->expects($this->once())->method('updateTypeUrls')->with('someType'); + $exampleType = 'someType'; + $filterStub = $this->createStub(ChangeFilterInterface::class); + $filterFactoryMock->method('getFilter')->with($exampleType)->willReturn($filterStub); + $dataSynchronizerMock->expects($this->once())->method('updateUrlsByFilter')->with($filterStub); $commandTester = new CommandTester($command); - $commandTester->execute(['type' => 'someType']); + $commandTester->execute(['type' => $exampleType]); + } + + public function getSut( + SynchronizerInterface $synchronizer = null, + FilterFactoryInterface $filterFactory = null, + ): UpdateTypeCommand + { + return new UpdateTypeCommand( + synchronizer: $synchronizer ?? $this->createMock(SynchronizerInterface::class), + filterFactory: $filterFactory ?? $this->createMock(FilterFactoryInterface::class), + ); } } diff --git a/tests/Unit/Integration/Service/SynchronizerTest.php b/tests/Unit/Integration/Service/SynchronizerTest.php index 57acb51..d6eb0f2 100644 --- a/tests/Unit/Integration/Service/SynchronizerTest.php +++ b/tests/Unit/Integration/Service/SynchronizerTest.php @@ -31,9 +31,6 @@ public function testUpdateIteratesOverUrlsAndUpdatesThemThroughRepository(): voi ]) ); - $filterServiceStub = $this->createMock(FilterFactoryInterface::class); - $filterServiceStub->method('getFilter')->with('someType')->willReturn($filterStub); - $urlRepository = $this->createMock(UrlRepositoryInterface::class); $matcher = $this->exactly(2); $urlRepository->expects($matcher) @@ -60,11 +57,10 @@ function ( ); $sut = new \FreshAdvance\Sitemap\Integration\Service\Synchronizer( - filterService: $filterServiceStub, urlRepository: $urlRepository, ); - $this->assertSame(2, $sut->updateTypeUrls('someType')); + $this->assertSame(2, $sut->updateUrlsByFilter($filterStub)); } protected function arrayAsGenerator(array $array): Generator