Skip to content

Commit

Permalink
Correct top songs retrieval and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
usox committed Nov 20, 2022
1 parent c09b7d9 commit b2fbf98
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/Orm/Repository/SongRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,17 @@ public function getTopSongsByArtist(
WITH pbh.song_id = song.id
WHERE song.artist = :artist
GROUP BY song HAVING(pbh.id) > 0
ORDER BY COUNT(pbh.id)
ORDER BY COUNT(pbh.id) DESC
SQL;

return $this->getEntityManager()
->createQuery(sprintf(
$query,
Song::class,
PlaybackHistory::class,
))
->createQuery(
sprintf(
$query,
Song::class,
PlaybackHistory::class,
)
)
->setParameters(['artist' => $artist])
->setMaxResults(10)
->toIterable();
Expand Down
53 changes: 53 additions & 0 deletions tests/Orm/Repository/SongRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
use Mockery;
use Mockery\Adapter\Phpunit\MockeryTestCase;
use Mockery\MockInterface;
use Uxmp\Core\Orm\Model\ArtistInterface;
use Uxmp\Core\Orm\Model\CatalogInterface;
use Uxmp\Core\Orm\Model\Favorite;
use Uxmp\Core\Orm\Model\FavoriteItemTypeEnum;
use Uxmp\Core\Orm\Model\PlaybackHistory;
use Uxmp\Core\Orm\Model\Song;
use Uxmp\Core\Orm\Model\SongInterface;
use Uxmp\Core\Orm\Model\UserInterface;
Expand Down Expand Up @@ -218,4 +220,55 @@ public function testFindFavoritesReturnsData(): void
$this->subject->findFavorites($user, $catalog)
);
}

public function testGetTopSongsByArtistReturnsSongs(): void
{
$query = $this
->getMockBuilder(AbstractQuery::class)
->disableOriginalConstructor()
->onlyMethods(['getResult', 'setParameters', 'toIterable'])
->addMethods(['setMaxResults'])
->getMockForAbstractClass();
$artist = Mockery::mock(ArtistInterface::class);

$result = [Mockery::mock(SongInterface::class)];

$sql = <<<SQL
SELECT song
FROM %s song
LEFT JOIN %s pbh
WITH pbh.song_id = song.id
WHERE song.artist = :artist
GROUP BY song HAVING(pbh.id) > 0
ORDER BY COUNT(pbh.id) DESC
SQL;

$this->entityManager->shouldReceive('createQuery')
->with(
sprintf(
$sql,
Song::class,
PlaybackHistory::class,
)
)
->once()
->andReturn($query);

$query->expects($this->once())
->method('setParameters')
->with(['artist' => $artist])
->willReturnSelf();
$query->expects($this->once())
->method('setMaxResults')
->with(10)
->willReturnSelf();
$query->expects($this->once())
->method('toIterable')
->willReturn($result);

$this->assertSame(
$result,
$this->subject->getTopSongsByArtist($artist)
);
}
}

0 comments on commit b2fbf98

Please sign in to comment.