mirror of
https://github.com/shlinkio/shlink.git
synced 2024-11-22 00:47:25 -06:00
Extracted method to find crawlable short codes to its own query object
This commit is contained in:
parent
73c8b53882
commit
60ef98b836
@ -50,6 +50,10 @@ return [
|
||||
EntityRepositoryFactory::class,
|
||||
ShortUrl\Entity\ShortUrl::class,
|
||||
],
|
||||
ShortUrl\Repository\CrawlableShortCodesQuery::class => [
|
||||
EntityRepositoryFactory::class,
|
||||
ShortUrl\Entity\ShortUrl::class,
|
||||
],
|
||||
|
||||
Tag\TagService::class => ConfigAbstractFactory::class,
|
||||
|
||||
|
@ -4,20 +4,16 @@ declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Crawling;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Repository\ShortUrlRepositoryInterface;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Repository\CrawlableShortCodesQueryInterface;
|
||||
|
||||
class CrawlingHelper implements CrawlingHelperInterface
|
||||
{
|
||||
public function __construct(private EntityManagerInterface $em)
|
||||
public function __construct(private readonly CrawlableShortCodesQueryInterface $query)
|
||||
{
|
||||
}
|
||||
|
||||
public function listCrawlableShortCodes(): iterable
|
||||
{
|
||||
/** @var ShortUrlRepositoryInterface $repo */
|
||||
$repo = $this->em->getRepository(ShortUrl::class);
|
||||
yield from $repo->findCrawlableShortCodes();
|
||||
yield from ($this->query)();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\ShortUrl\Repository;
|
||||
|
||||
use Happyr\DoctrineSpecification\Repository\EntitySpecificationRepository;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
||||
|
||||
class CrawlableShortCodesQuery extends EntitySpecificationRepository implements CrawlableShortCodesQueryInterface
|
||||
{
|
||||
/**
|
||||
* @return iterable<string>
|
||||
*/
|
||||
public function __invoke(): iterable
|
||||
{
|
||||
$blockSize = 1000;
|
||||
$qb = $this->getEntityManager()->createQueryBuilder();
|
||||
$qb->select('DISTINCT s.shortCode')
|
||||
->from(ShortUrl::class, 's')
|
||||
->where($qb->expr()->eq('s.crawlable', ':crawlable'))
|
||||
->setParameter('crawlable', true)
|
||||
->setMaxResults($blockSize);
|
||||
|
||||
$page = 0;
|
||||
do {
|
||||
$qbClone = (clone $qb)->setFirstResult($blockSize * $page);
|
||||
$iterator = $qbClone->getQuery()->toIterable();
|
||||
$resultsFound = false;
|
||||
$page++;
|
||||
|
||||
foreach ($iterator as ['shortCode' => $shortCode]) {
|
||||
$resultsFound = true;
|
||||
yield $shortCode;
|
||||
}
|
||||
} while ($resultsFound);
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\ShortUrl\Repository;
|
||||
|
||||
interface CrawlableShortCodesQueryInterface
|
||||
{
|
||||
/**
|
||||
* @return iterable<string>
|
||||
*/
|
||||
public function __invoke(): iterable;
|
||||
}
|
@ -190,28 +190,4 @@ class ShortUrlRepository extends EntitySpecificationRepository implements ShortU
|
||||
$qb->andWhere($qb->expr()->isNull('s.domain'));
|
||||
}
|
||||
}
|
||||
|
||||
public function findCrawlableShortCodes(): iterable
|
||||
{
|
||||
$blockSize = 1000;
|
||||
$qb = $this->getEntityManager()->createQueryBuilder();
|
||||
$qb->select('DISTINCT s.shortCode')
|
||||
->from(ShortUrl::class, 's')
|
||||
->where($qb->expr()->eq('s.crawlable', ':crawlable'))
|
||||
->setParameter('crawlable', true)
|
||||
->setMaxResults($blockSize);
|
||||
|
||||
$page = 0;
|
||||
do {
|
||||
$qbClone = (clone $qb)->setFirstResult($blockSize * $page);
|
||||
$iterator = $qbClone->getQuery()->toIterable();
|
||||
$resultsFound = false;
|
||||
$page++;
|
||||
|
||||
foreach ($iterator as ['shortCode' => $shortCode]) {
|
||||
$resultsFound = true;
|
||||
yield $shortCode;
|
||||
}
|
||||
} while ($resultsFound);
|
||||
}
|
||||
}
|
||||
|
@ -25,6 +25,4 @@ interface ShortUrlRepositoryInterface extends ObjectRepository, EntitySpecificat
|
||||
public function findOneMatching(ShortUrlCreation $meta): ?ShortUrl;
|
||||
|
||||
public function findOneByImportedUrl(ImportedShlinkUrl $url): ?ShortUrl;
|
||||
|
||||
public function findCrawlableShortCodes(): iterable;
|
||||
}
|
||||
|
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioDbTest\Shlink\Core\ShortUrl\Repository;
|
||||
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlCreation;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Repository\CrawlableShortCodesQuery;
|
||||
use Shlinkio\Shlink\TestUtils\DbTest\DatabaseTestCase;
|
||||
|
||||
class CrawlableShortCodesQueryTest extends DatabaseTestCase
|
||||
{
|
||||
private CrawlableShortCodesQuery $query;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$em = $this->getEntityManager();
|
||||
$this->query = new CrawlableShortCodesQuery($em, $em->getClassMetadata(ShortUrl::class));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function invokingQueryReturnsExpectedResult(): void
|
||||
{
|
||||
$createShortUrl = fn (bool $crawlable) => ShortUrl::create(
|
||||
ShortUrlCreation::fromRawData(['crawlable' => $crawlable, 'longUrl' => 'foo.com']),
|
||||
);
|
||||
|
||||
$shortUrl1 = $createShortUrl(true);
|
||||
$this->getEntityManager()->persist($shortUrl1);
|
||||
$shortUrl2 = $createShortUrl(false);
|
||||
$this->getEntityManager()->persist($shortUrl2);
|
||||
$shortUrl3 = $createShortUrl(true);
|
||||
$this->getEntityManager()->persist($shortUrl3);
|
||||
$shortUrl4 = $createShortUrl(true);
|
||||
$this->getEntityManager()->persist($shortUrl4);
|
||||
$shortUrl5 = $createShortUrl(false);
|
||||
$this->getEntityManager()->persist($shortUrl5);
|
||||
$this->getEntityManager()->flush();
|
||||
|
||||
$results = [...($this->query)()];
|
||||
|
||||
self::assertCount(3, $results);
|
||||
self::assertContains($shortUrl1->getShortCode(), $results);
|
||||
self::assertContains($shortUrl3->getShortCode(), $results);
|
||||
self::assertContains($shortUrl4->getShortCode(), $results);
|
||||
self::assertNotContains($shortUrl2->getShortCode(), $results);
|
||||
self::assertNotContains($shortUrl5->getShortCode(), $results);
|
||||
}
|
||||
}
|
@ -391,37 +391,4 @@ class ShortUrlRepositoryTest extends DatabaseTestCase
|
||||
self::assertNull($this->repo->findOneByImportedUrl($buildImported('my-cool-slug', 'doma.in')));
|
||||
self::assertNull($this->repo->findOneByImportedUrl($buildImported('another-slug')));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function findCrawlableShortCodesReturnsExpectedResult(): void
|
||||
{
|
||||
$createShortUrl = fn (bool $crawlable) => ShortUrl::create(
|
||||
ShortUrlCreation::fromRawData(['crawlable' => $crawlable, 'longUrl' => 'foo.com']),
|
||||
);
|
||||
|
||||
$shortUrl1 = $createShortUrl(true);
|
||||
$this->getEntityManager()->persist($shortUrl1);
|
||||
$shortUrl2 = $createShortUrl(false);
|
||||
$this->getEntityManager()->persist($shortUrl2);
|
||||
$shortUrl3 = $createShortUrl(true);
|
||||
$this->getEntityManager()->persist($shortUrl3);
|
||||
$shortUrl4 = $createShortUrl(true);
|
||||
$this->getEntityManager()->persist($shortUrl4);
|
||||
$shortUrl5 = $createShortUrl(false);
|
||||
$this->getEntityManager()->persist($shortUrl5);
|
||||
$this->getEntityManager()->flush();
|
||||
|
||||
$iterable = $this->repo->findCrawlableShortCodes();
|
||||
$results = [];
|
||||
foreach ($iterable as $shortCode) {
|
||||
$results[] = $shortCode;
|
||||
}
|
||||
|
||||
self::assertCount(3, $results);
|
||||
self::assertContains($shortUrl1->getShortCode(), $results);
|
||||
self::assertContains($shortUrl3->getShortCode(), $results);
|
||||
self::assertContains($shortUrl4->getShortCode(), $results);
|
||||
self::assertNotContains($shortUrl2->getShortCode(), $results);
|
||||
self::assertNotContains($shortUrl5->getShortCode(), $results);
|
||||
}
|
||||
}
|
||||
|
@ -4,34 +4,26 @@ declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioTest\Shlink\Core\Crawling;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Shlinkio\Shlink\Core\Crawling\CrawlingHelper;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Repository\ShortUrlRepositoryInterface;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Repository\CrawlableShortCodesQueryInterface;
|
||||
|
||||
class CrawlingHelperTest extends TestCase
|
||||
{
|
||||
private CrawlingHelper $helper;
|
||||
private MockObject & EntityManagerInterface $em;
|
||||
private MockObject & CrawlableShortCodesQueryInterface $query;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->em = $this->createMock(EntityManagerInterface::class);
|
||||
$this->helper = new CrawlingHelper($this->em);
|
||||
$this->query = $this->createMock(CrawlableShortCodesQueryInterface::class);
|
||||
$this->helper = new CrawlingHelper($this->query);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function listCrawlableShortCodesDelegatesIntoRepository(): void
|
||||
{
|
||||
$repo = $this->createMock(ShortUrlRepositoryInterface::class);
|
||||
$repo->expects($this->once())->method('findCrawlableShortCodes')->willReturn([]);
|
||||
$this->em->expects($this->once())->method('getRepository')->with(ShortUrl::class)->willReturn($repo);
|
||||
|
||||
$result = $this->helper->listCrawlableShortCodes();
|
||||
foreach ($result as $shortCode) {
|
||||
// $result is a generator and therefore, it needs to be iterated
|
||||
}
|
||||
$this->query->expects($this->once())->method('__invoke')->willReturn([]);
|
||||
[...$this->helper->listCrawlableShortCodes()];
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user