Skip to content

Commit

Permalink
Move filter factory dependency one level up from synchronizer
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Fedurtsya <[email protected]>
  • Loading branch information
Sieg committed Jan 9, 2024
1 parent 3c1bd0c commit 864819a
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/Integration/Command/UpdateTypeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,6 +20,7 @@ class UpdateTypeCommand extends Command
{
public function __construct(
protected SynchronizerInterface $synchronizer,
protected FilterFactoryInterface $filterFactory,
) {
parent::__construct();
}
Expand All @@ -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;
Expand Down
7 changes: 3 additions & 4 deletions src/Integration/Service/Synchronizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion src/Integration/Service/SynchronizerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
25 changes: 22 additions & 3 deletions tests/Integration/Integration/Command/UpdateTypeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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),
);
}
}
6 changes: 1 addition & 5 deletions tests/Unit/Integration/Service/SynchronizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 864819a

Please sign in to comment.