Update tests to new upload format

This commit is contained in:
Jonathan Flueren 2021-10-06 19:11:50 +02:00 committed by thrillfall
parent 792704d07a
commit 77571feb40
7 changed files with 26 additions and 41 deletions

View File

@ -33,25 +33,28 @@ class EpisodeActionSaver
/**
* @param array $episodeActionsArray
*
* @return void
* @return EpisodeActionEntity[]
*/
public function saveEpisodeActions($episodeActionsArray, string $userId): void
public function saveEpisodeActions($episodeActionsArray, string $userId): array
{
$episodeActions = $this->episodeActionReader->fromArray($episodeActionsArray);
$episodeActionEntities = [];
foreach ($episodeActions as $episodeAction) {
$episodeActionEntity = $this->hydrateEpisodeActionEntity($episodeAction, $userId);
try {
$this->episodeActionWriter->save($episodeActionEntity);
$episodeActionEntities[] = $this->episodeActionWriter->save($episodeActionEntity);
} catch (UniqueConstraintViolationException $uniqueConstraintViolationException) {
$this->updateEpisodeAction($episodeActionEntity, $userId);
$episodeActionEntities[] = $this->updateEpisodeAction($episodeActionEntity, $userId);
} catch (Exception $exception) {
if ($exception->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
$this->updateEpisodeAction($episodeActionEntity, $userId);
$episodeActionEntities[] = $this->updateEpisodeAction($episodeActionEntity, $userId);
}
}
}
return $episodeActionEntities;
}
private function convertTimestampToUnixEpoch(string $timestamp): string

View File

@ -31,7 +31,7 @@ class EpisodeActionRepositoryTest extends \Test\TestCase
$guid = uniqid("test_gid://art19-episode-locator/V0/Ktd");
$savedEpisodeActionEntity = $episodeActionSaver->saveEpisodeActions(
"[EpisodeAction{podcast='https://rss.art19.com/dr-death-s3-miracle-man', episode='{$episodeUrl}', guid='{$guid}', action=PLAY, timestamp=Mon Aug 23 01:58:56 GMT+02:00 2021, started=47, position=54, total=2252}]",
[["podcast" => 'https://rss.art19.com/dr-death-s3-miracle-man', "episode" => $episodeUrl, "guid" => $guid, "action" => "PLAY", "timestamp" => "2021-08-22T23:58:56", "started" => 47, "position" => 54, "total" => 2252]],
self::USER_ID_0
)[0];

View File

@ -32,12 +32,12 @@ class EpisodeActionSaverGuidBackwardCompatbilityTest extends TestCase
$guid = uniqid("test_gid://art19-episode-locator/V0/Ktd");
$savedEpisodeActionEntity = $episodeActionSaver->saveEpisodeActions(
"[EpisodeAction{podcast='https://rss.art19.com/dr-death-s3-miracle-man', episode='{$episodeUrl}', guid='{$guid}', action=PLAY, timestamp=Mon Aug 23 01:58:56 GMT+02:00 2021, started=47, position=54, total=2252}]",
[["podcast" => 'https://rss.art19.com/dr-death-s3-miracle-man', "episode" => $episodeUrl, "guid" => $guid, "action" => "PLAY", "timestamp" => "2021-08-22T23:58:56", "started" => 47, "position" => 54, "total" => 2252]],
self::USER_ID_0
)[0];
$savedEpisodeActionEntityWithoutGuidFromOldDevice = $episodeActionSaver->saveEpisodeActions(
"[EpisodeAction{podcast='https://rss.art19.com/dr-death-s3-miracle-man', episode='{$episodeUrl}', action=PLAY, timestamp=Mon Aug 23 01:58:56 GMT+02:00 2021, started=47, position=54, total=2252}]",
[["podcast" => 'https://rss.art19.com/dr-death-s3-miracle-man', "episode" => $episodeUrl, "action" => "PLAY", "timestamp" => "2021-08-22T23:58:56", "started" => 47, "position" => 54, "total" => 2252]],
self::USER_ID_0
)[0];

View File

@ -32,12 +32,12 @@ class EpisodeActionSaverGuidMigrationTest extends TestCase
$guid = uniqid("test_gid://art19-episode-locator/V0/Ktd");
$savedEpisodeActionEntityWithoutGuid = $episodeActionSaver->saveEpisodeActions(
"[EpisodeAction{podcast='https://rss.art19.com/dr-death-s3-miracle-man', episode='{$episodeUrl}', action=PLAY, timestamp=Mon Aug 23 01:58:56 GMT+02:00 2021, started=47, position=54, total=2252}]",
[["podcast" => 'https://rss.art19.com/dr-death-s3-miracle-man', "episode" => $episodeUrl, "action" => "PLAY", "timestamp" => "2021-08-22T23:58:56", "started" => 47, "position" => 54, "total" => 2252]],
self::USER_ID_0
)[0];
$savedEpisodeActionEntityWithGuid = $episodeActionSaver->saveEpisodeActions(
"[EpisodeAction{podcast='https://rss.art19.com/dr-death-s3-miracle-man', episode='{$episodeUrl}', guid='{$guid}', action=PLAY, timestamp=Mon Aug 23 01:58:56 GMT+02:00 2021, started=47, position=54, total=2252}]",
[["podcast" => 'https://rss.art19.com/dr-death-s3-miracle-man', "episode" => $episodeUrl, "guid" => $guid, "action" => "PLAY", "timestamp" => "2021-08-22T23:58:56", "started" => 47, "position" => 54, "total" => 2252]],
self::USER_ID_0
)[0];
@ -53,12 +53,12 @@ class EpisodeActionSaverGuidMigrationTest extends TestCase
$guid = uniqid("test_gid://art19-episode-locator/V0/Ktd");
$savedEpisodeActionEntity = $episodeActionSaver->saveEpisodeActions(
"[EpisodeAction{podcast='https://rss.art19.com/dr-death-s3-miracle-man', episode='{$episodeUrl}', guid='{$guid}', action=PLAY, timestamp=Mon Aug 23 01:58:56 GMT+02:00 2021, started=47, position=54, total=2252}]",
[["podcast" => 'https://rss.art19.com/dr-death-s3-miracle-man', "episode" => $episodeUrl, "guid" => $guid, "action" => "PLAY", "timestamp" => "2021-08-22T23:58:56", "started" => 47, "position" => 54, "total" => 2252]],
self::USER_ID_0
)[0];
$savedEpisodeActionEntityWithDifferentEpisodeUrl = $episodeActionSaver->saveEpisodeActions(
"[EpisodeAction{podcast='https://rss.art19.com/dr-death-s3-miracle-man', episode='{$episodeUrl}_different', guid='{$guid}', action=PLAY, timestamp=Mon Aug 23 01:58:56 GMT+02:00 2021, started=47, position=54, total=2252}]",
[["podcast" => 'https://rss.art19.com/dr-death-s3-miracle-man', "episode" => $episodeUrl . "_different", "guid" => $guid, "action" => "PLAY", "timestamp" => "2021-08-22T23:58:56", "started" => 47, "position" => 54, "total" => 2252]],
self::USER_ID_0
)[0];

View File

@ -64,7 +64,7 @@ class TimestampMigrationTest extends TestCase
$episodeActionEntity->setPosition(5);
$episodeActionEntity->setStarted(0);
$episodeActionEntity->setTotal(123);
$episodeActionEntity->setTimestamp("Sun Aug 22 23:58:56 GMT+00:00 2021");
$episodeActionEntity->setTimestamp("2021-08-22T23:58:56");
$episodeActionEntity->setUserId(self::ADMIN);
$guid = uniqid("self::TEST_GUID_1234");
$episodeActionEntity->setGuid($guid);

View File

@ -9,12 +9,12 @@ use Test\TestCase;
class EpisodeActionReaderTest extends TestCase {
public function testCreateFromArray(): void {
$reader = new EpisodeActionReader();
$episodeActions = $reader->fromArray([["podcast" => "https://example.org/feed.xml", "episode" => "https://example.org/episode1.mp3", "action" => "PLAY", "timestamp" => "Sun Oct 03 14:03:17 GMT+02:00 2021", "started" => 0, "position" => 50, "total"=> 3422]]);
$episodeActions = $reader->fromArray([["podcast" => "https://example.org/feed.xml", "episode" => "https://example.org/episode1.mp3", "action" => "PLAY", "timestamp" => "2021-10-03T12:03:17", "started" => 0, "position" => 50, "total"=> 3422]]);
$this->assertSame("https://example.org/feed.xml", $episodeActions[0]->getPodcast());
$this->assertSame("https://example.org/episode1.mp3", $episodeActions[0]->getEpisode());
$this->assertSame("PLAY", $episodeActions[0]->getAction());
$this->assertSame("Sun Oct 03 14:03:17 GMT+02:00 2021", $episodeActions[0]->getTimestamp());
$this->assertSame("2021-10-03T12:03:17", $episodeActions[0]->getTimestamp());
$this->assertSame(0, $episodeActions[0]->getStarted());
$this->assertSame(50, $episodeActions[0]->getPosition());
$this->assertSame(3422, $episodeActions[0]->getTotal());
@ -23,16 +23,16 @@ class EpisodeActionReaderTest extends TestCase {
public function testCreateFromMultipleEpisodesArray(): void {
$reader = new EpisodeActionReader();
$episodeActions = $reader->fromArray([
["podcast" => "https://example.org/feed.xml", "episode" => "https://example.org/episode1.mp3", "guid" => "episode1", "action" => "PLAY", "timestamp" => "Sun Oct 03 14:03:17 GMT+02:00 2021", "started" => 0, "position" => 50, "total"=> 3422],
["podcast" => "https://example.org/feed.xml", "episode" => "https://example.org/episode2.mp3", "guid" => "episode2", "action" => "DOWNLOAD", "timestamp" => "Sat Oct 02 11:06:28 GMT+02:00 2021", "started" => -1, "position" => -1, "total"=> -1],
["podcast" => "https://example.com/feed.xml", "episode" => "https://chrt.fm/track/47G541/injector.simplecastaudio.com/f16c3da7-cf46-4a42-99b7-8467255c6086/episodes/e8e24c01-6157-40e8-9b5a-45d539aeb7e6/audio/128/default.mp3?aid=rss_feed&awCollectionId=f16c3da7-cf46-4a42-99b7-8467255c6086&awEpisodeId=e8e24c01-6157-40e8-9b5a-45d539aeb7e6&feed=wEl4UUJZ", "guid" => "EPISODE-001-EXAMPLE-COM", "action" => "PLAY", "timestamp" => "Sun Oct 03 14:07:15 GMT+02:00 2021", "started" => 50, "position" => 221, "total"=> 450]
["podcast" => "https://example.org/feed.xml", "episode" => "https://example.org/episode1.mp3", "guid" => "episode1", "action" => "PLAY", "timestamp" => "2021-10-03T12:03:17", "started" => 0, "position" => 50, "total"=> 3422],
["podcast" => "https://example.org/feed.xml", "episode" => "https://example.org/episode2.mp3", "guid" => "episode2", "action" => "download", "timestamp" => "2021-10-03T12:03:17"],
["podcast" => "https://example.com/feed.xml", "episode" => "https://chrt.fm/track/47G541/injector.simplecastaudio.com/f16c3da7-cf46-4a42-99b7-8467255c6086/episodes/e8e24c01-6157-40e8-9b5a-45d539aeb7e6/audio/128/default.mp3?aid=rss_feed&awCollectionId=f16c3da7-cf46-4a42-99b7-8467255c6086&awEpisodeId=e8e24c01-6157-40e8-9b5a-45d539aeb7e6&feed=wEl4UUJZ", "guid" => "EPISODE-001-EXAMPLE-COM", "action" => "PLAY", "timestamp" => "2021-10-03T12:03:17", "started" => 50, "position" => 221, "total"=> 450]
]);
$this->assertSame("https://example.org/feed.xml", $episodeActions[0]->getPodcast());
$this->assertSame("https://example.org/episode1.mp3", $episodeActions[0]->getEpisode());
$this->assertSame("episode1", $episodeActions[0]->getGuid());
$this->assertSame("PLAY", $episodeActions[0]->getAction());
$this->assertSame("Sun Oct 03 14:03:17 GMT+02:00 2021", $episodeActions[0]->getTimestamp());
$this->assertSame("2021-10-03T12:03:17", $episodeActions[0]->getTimestamp());
$this->assertSame(0, $episodeActions[0]->getStarted());
$this->assertSame(50, $episodeActions[0]->getPosition());
$this->assertSame(3422, $episodeActions[0]->getTotal());
@ -41,7 +41,7 @@ class EpisodeActionReaderTest extends TestCase {
$this->assertSame("https://example.org/episode2.mp3", $episodeActions[1]->getEpisode());
$this->assertSame("episode2", $episodeActions[1]->getGuid());
$this->assertSame("DOWNLOAD", $episodeActions[1]->getAction());
$this->assertSame("Sat Oct 02 11:06:28 GMT+02:00 2021", $episodeActions[1]->getTimestamp());
$this->assertSame("2021-10-03T12:03:17", $episodeActions[1]->getTimestamp());
$this->assertSame(-1, $episodeActions[1]->getStarted());
$this->assertSame(-1, $episodeActions[1]->getPosition());
$this->assertSame(-1, $episodeActions[1]->getTotal());
@ -50,7 +50,7 @@ class EpisodeActionReaderTest extends TestCase {
$this->assertSame("https://chrt.fm/track/47G541/injector.simplecastaudio.com/f16c3da7-cf46-4a42-99b7-8467255c6086/episodes/e8e24c01-6157-40e8-9b5a-45d539aeb7e6/audio/128/default.mp3?aid=rss_feed&awCollectionId=f16c3da7-cf46-4a42-99b7-8467255c6086&awEpisodeId=e8e24c01-6157-40e8-9b5a-45d539aeb7e6&feed=wEl4UUJZ", $episodeActions[2]->getEpisode());
$this->assertSame("EPISODE-001-EXAMPLE-COM", $episodeActions[2]->getGuid());
$this->assertSame("PLAY", $episodeActions[2]->getAction());
$this->assertSame("Sun Oct 03 14:07:15 GMT+02:00 2021", $episodeActions[2]->getTimestamp());
$this->assertSame("2021-10-03T12:03:17", $episodeActions[2]->getTimestamp());
$this->assertSame(50, $episodeActions[2]->getStarted());
$this->assertSame(221, $episodeActions[2]->getPosition());
$this->assertSame(450, $episodeActions[2]->getTotal());

View File

@ -1,18 +0,0 @@
<?php
declare(strict_types=1);
namespace OCA\GPodderSync\Tests\Unit\Core\EpisodeAction;
use Test\TestCase;
class EpisodeActionRequestTimestampConversionTest extends TestCase
{
public function testDateTimeFormatIsEnsured(): void
{
$episodeActionTimestamp = "2021-05-18T23:45:11";
$datetime = \DateTime::createFromFormat('Y-m-d\TH:i:s', $episodeActionTimestamp)
->format('Y-m-d\TH:i:s');
$this->assertEquals($episodeActionTimestamp, $datetime);
}
}