Skip to content

Commit

Permalink
PROGRAMMES-5737 Adding new service methods (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
votemike authored Oct 5, 2017
1 parent 1a59561 commit a6139b0
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 1 deletion.
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ parameters:
- '#Method BBC\\ProgrammesPagesService\\Domain\\ApplicationTime::getTime\(\) should return DateTimeImmutable but returns DateTimeImmutable\|null.#'
- '#Method BBC\\ProgrammesPagesService\\Mapper\\ProgrammesDbToDomain\\CategoryMapper::getGenreParentModel\(\) should return BBC\\ProgrammesPagesService\\Domain\\Entity\\Genre\|null but returns BBC\\ProgrammesPagesService\\Domain\\Entity\\Category.#'
- '#Method BBC\\ProgrammesPagesService\\Mapper\\ProgrammesDbToDomain\\MapperFactory::get[A-z]+Mapper\(\) should return BBC\\ProgrammesPagesService\\Mapper\\ProgrammesDbToDomain\\[A-z]+Mapper but returns BBC\\ProgrammesPagesService\\Mapper\\ProgrammesDbToDomain\\AbstractMapper.#'
- '#Method BBC\\ProgrammesPagesService\\Service\\ProgrammesAggregationService::findDescendantClips\(\) should return BBC\\ProgrammesPagesService\\Domain\\Entity\\Clip\[\] but returns BBC\\ProgrammesPagesService\\Domain\\Entity\\ProgrammeItem\[\].#'
- '#Method BBC\\ProgrammesPagesService\\Service\\ProgrammesAggregationService::findDescendantGalleries\(\) should return BBC\\ProgrammesPagesService\\Domain\\Entity\\Gallery\[\] but returns BBC\\ProgrammesPagesService\\Domain\\Entity\\CoreEntity\[\].#'
- '#Method BBC\\ProgrammesPagesService\\Service\\ProgrammesAggregationService::find[A-z]+\(\) should return BBC\\ProgrammesPagesService\\Domain\\Entity\\[A-z]+\[\] but returns BBC\\ProgrammesPagesService\\Domain\\Entity\\ProgrammeItem\[\].#'
- '#Parameter \#[0-9]+ \$maxResults of method Doctrine\\ORM\\Query(Builder)?::setMaxResults\(\) expects int, int\|null given.#'
- '#Parameter \#3 \$startAt of class BBC\\ProgrammesPagesService\\Domain\\Entity\\CollapsedBroadcast constructor expects DateTimeImmutable, DateTimeImmutable\|null given.#'
- '#Parameter \#4 \$endAt of class BBC\\ProgrammesPagesService\\Domain\\Entity\\CollapsedBroadcast constructor expects DateTimeImmutable, DateTimeImmutable\|null given.#'
Expand Down
32 changes: 32 additions & 0 deletions src/Data/ProgrammesDb/EntityRepository/CoreEntityRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use BBC\ProgrammesPagesService\Data\ProgrammesDb\Util\StripPunctuationTrait;
use BBC\ProgrammesPagesService\Domain\ValueObject\PartialDate;
use DateTimeImmutable;
use DateTimeInterface;
use Doctrine\ORM\Query;
use Gedmo\Tree\Entity\Repository\MaterializedPathRepository;
Expand Down Expand Up @@ -250,6 +251,37 @@ public function findStreamableDescendantsByType(array $ancestryDbIds, string $en
return $this->resolveParents($result);
}

/**
* @param int[] $ancestryDbIds
* @param string $entityType
* @param int|null $limit
* @param int $offset
* @return array
*/
public function findUpcomingStreamableDescendantsByType(array $ancestryDbIds, string $entityType, ?int $limit, int $offset, DateTimeImmutable $fromDateTime) : array
{
$this->assertEntityType($entityType, ['Clip', 'Episode']);

$qb = $this->getEntityManager()->createQueryBuilder()
->addSelect(['entity', 'masterBrand', 'image', 'mbImage', 'network'])
->from('ProgrammesPagesService:' . $entityType, 'entity')
->leftJoin('entity.masterBrand', 'masterBrand')
->leftJoin('masterBrand.network', 'network')
->leftJoin('entity.image', 'image')
->leftJoin('masterBrand.image', 'mbImage')
->andWhere('entity.ancestry LIKE :ancestry')
->andWhere('entity.streamable = 0')
->andWhere('entity.streamableFrom > :from')
->orderBy('entity.streamableFrom', 'ASC')
->setFirstResult($offset)
->setMaxResults($limit)
->setParameter('ancestry', $this->ancestryIdsToString($ancestryDbIds) . '%')
->setParameter('from', $fromDateTime);

$result = $qb->getQuery()->getResult(Query::HYDRATE_ARRAY);
return $this->resolveParents($result);
}

public function findChildrenSeriesByParent(int $id, ?int $limit, int $offset): array
{
$qb = $this->getEntityManager()->createQueryBuilder()
Expand Down
52 changes: 52 additions & 0 deletions src/Service/ProgrammesAggregationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
use BBC\ProgrammesPagesService\Cache\CacheInterface;
use BBC\ProgrammesPagesService\Data\ProgrammesDb\EntityRepository\ClipRepository;
use BBC\ProgrammesPagesService\Data\ProgrammesDb\EntityRepository\CoreEntityRepository;
use BBC\ProgrammesPagesService\Domain\ApplicationTime;
use BBC\ProgrammesPagesService\Domain\Entity\Clip;
use BBC\ProgrammesPagesService\Domain\Entity\CoreEntity;
use BBC\ProgrammesPagesService\Domain\Entity\Episode;
use BBC\ProgrammesPagesService\Domain\Entity\Gallery;
use BBC\ProgrammesPagesService\Domain\Entity\Programme;
use BBC\ProgrammesPagesService\Domain\Entity\ProgrammeItem;
Expand Down Expand Up @@ -39,6 +41,28 @@ public function findDescendantClips(
return $this->findStreamableDescendantsByType($programme, 'Clip', $limit, $page);
}

/**
* @return Episode[]
*/
public function findStreamableDescendantEpisodes(
Programme $programme,
?int $limit = self::DEFAULT_LIMIT,
int $page = self::DEFAULT_PAGE
): array {
return $this->findStreamableDescendantsByType($programme, 'Episode', $limit, $page);
}

/**
* @return Episode[]
*/
public function findUpcomingStreamableDescendantEpisodes(
Programme $programme,
?int $limit = self::DEFAULT_LIMIT,
int $page = self::DEFAULT_PAGE
): array {
return $this->findUpcomingStreamableDescendantsByType($programme, 'Episode', $limit, $page);
}

/**
* @return Gallery[]
*/
Expand Down Expand Up @@ -77,6 +101,34 @@ function () use ($programme, $type, $limit, $page) {
);
}

/**
* @return ProgrammeItem[]
*/
private function findUpcomingStreamableDescendantsByType(
Programme $programme,
string $type,
?int $limit,
int $page,
$ttl = CacheInterface::NORMAL
): array {
$key = $this->cache->keyHelper(__CLASS__, __FUNCTION__, $programme->getPid(), $type, $limit, $page, $ttl);
return $this->cache->getOrSet(
$key,
$ttl,
function () use ($programme, $type, $limit, $page) {
$children = $this->repository->findUpcomingStreamableDescendantsByType(
$programme->getDbAncestryIds(),
$type,
$limit,
$this->getOffset($limit, $page),
ApplicationTime::getTime()
);

return $this->mapManyEntities($children);
}
);
}

/**
* @return CoreEntity[]
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use BBC\ProgrammesPagesService\Data\ProgrammesDb\EntityRepository\CoreEntityRepository;
use BBC\ProgrammesPagesService\Domain\Entity\Clip;
use BBC\ProgrammesPagesService\Domain\Entity\Episode;
use BBC\ProgrammesPagesService\Domain\Entity\Gallery;
use BBC\ProgrammesPagesService\Domain\ValueObject\Pid;
use BBC\ProgrammesPagesService\Mapper\ProgrammesDbToDomain\CoreEntityMapper;
Expand All @@ -22,6 +23,10 @@ public function setUp()
$class = Clip::class;
}

if ($dbEntity['type'] === 'episode') {
$class = Episode::class;
}

if ($dbEntity['type'] === 'gallery') {
$class = Gallery::class;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Tests\BBC\ProgrammesPagesService\Service\ProgrammesAggregationService;

use BBC\ProgrammesPagesService\Domain\Entity\Episode;
use BBC\ProgrammesPagesService\Domain\Entity\Programme;

class FindStreamableDescendantEpisodesTest extends AbstractProgrammesAggregationTest
{
/**
* @dataProvider paginationProvider
*/
public function testProtocolWithRepositoryCollaborator(int $expectedLimit, int $expectedOffset, array $paramsPagination)
{
$stubProgramme = $this->createConfiguredMock(Programme::class, ['getDbAncestryIds' => [11, 12]]);

$this->mockRepository->expects($this->once())
->method('findStreamableDescendantsByType')
->with($stubProgramme->getDbAncestryIds(), 'Episode', $expectedLimit, $expectedOffset);

$this->service()->findStreamableDescendantEpisodes($stubProgramme, ...$paramsPagination);
}

public function paginationProvider(): array
{
return [
// [expectedLimit, expectedOffset, [limit, page]]
'default pagination' => [300, 0, []],
'custom pagination' => [3, 12, [3, 5]],
];
}

/**
* @dataProvider dbEpisodesProvider
*/
public function testResults(array $expectedEpisodesPids, array $dbEpisodesProvided)
{
$this->mockRepository->method('findStreamableDescendantsByType')->willReturn($dbEpisodesProvided);

$episodes = $this->service()->findStreamableDescendantEpisodes($this->createMock(Programme::class));

$this->assertContainsOnlyInstancesOf(Episode::class, $episodes);
$this->assertCount(count($dbEpisodesProvided), $episodes);
foreach ($expectedEpisodesPids as $i => $expectedPid) {
$this->assertEquals($expectedPid, $episodes[$i]->getPid());
}
}

public function dbEpisodesProvider(): array
{
return [
'CASE: episodes results found' => [
['p002b7q9', 'p002kzxk'],
[['type' => 'episode', 'pid' => 'p002b7q9'], ['type' => 'episode', 'pid' => 'p002kzxk']],
],
'CASE: episodes results NOT found' => [[], []],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Tests\BBC\ProgrammesPagesService\Service\ProgrammesAggregationService;

use BBC\ProgrammesPagesService\Domain\Entity\Episode;
use BBC\ProgrammesPagesService\Domain\Entity\Programme;

class FindUpcomingStreamableDescendantEpisodesTest extends AbstractProgrammesAggregationTest
{
/**
* @dataProvider paginationProvider
*/
public function testProtocolWithRepositoryCollaborator(int $expectedLimit, int $expectedOffset, array $paramsPagination)
{
$stubProgramme = $this->createConfiguredMock(Programme::class, ['getDbAncestryIds' => [11, 12]]);

$this->mockRepository->expects($this->once())
->method('findUpcomingStreamableDescendantsByType')
->with($stubProgramme->getDbAncestryIds(), 'Episode', $expectedLimit, $expectedOffset);

$this->service()->findUpcomingStreamableDescendantEpisodes($stubProgramme, ...$paramsPagination);
}

public function paginationProvider(): array
{
return [
// [expectedLimit, expectedOffset, [limit, page]]
'default pagination' => [300, 0, []],
'custom pagination' => [3, 12, [3, 5]],
];
}

/**
* @dataProvider dbEpisodesProvider
*/
public function testResults(array $expectedEpisodesPids, array $dbEpisodesProvided)
{
$this->mockRepository->method('findUpcomingStreamableDescendantsByType')->willReturn($dbEpisodesProvided);

$episodes = $this->service()->findUpcomingStreamableDescendantEpisodes($this->createMock(Programme::class));

$this->assertContainsOnlyInstancesOf(Episode::class, $episodes);
$this->assertCount(count($dbEpisodesProvided), $episodes);
foreach ($expectedEpisodesPids as $i => $expectedPid) {
$this->assertEquals($expectedPid, $episodes[$i]->getPid());
}
}

public function dbEpisodesProvider(): array
{
return [
'CASE: episodes results found' => [
['p002b7q9', 'p002kzxk'],
[['type' => 'episode', 'pid' => 'p002b7q9'], ['type' => 'episode', 'pid' => 'p002kzxk']],
],
'CASE: episodes results NOT found' => [[], []],
];
}
}

0 comments on commit a6139b0

Please sign in to comment.