From 6ba2f891956e28c6db31c06f78fb197af8004f68 Mon Sep 17 00:00:00 2001 From: Jonathan Flueren Date: Wed, 25 May 2022 10:59:55 +0200 Subject: [PATCH] Add integration tests for subs and empty since --- .../EpisodeActionControllerTest.php | 42 ++++ .../SubscriptionChangeControllerTest.php | 180 ++++++++++++++++++ 2 files changed, 222 insertions(+) create mode 100644 tests/Integration/Controller/SubscriptionChangeControllerTest.php diff --git a/tests/Integration/Controller/EpisodeActionControllerTest.php b/tests/Integration/Controller/EpisodeActionControllerTest.php index a45373b..73788a4 100644 --- a/tests/Integration/Controller/EpisodeActionControllerTest.php +++ b/tests/Integration/Controller/EpisodeActionControllerTest.php @@ -87,6 +87,48 @@ class EpisodeActionControllerTest extends TestCase self::assertSame("PLAY", $episodeActionInResponse['action']); } + public function testEpisodeActionListWithoutSinceAction() + { + $userId = uniqid("test_user"); + $episodeActionController = new EpisodeActionController( + "gpoddersync", + $this->createMock(IRequest::class), + $userId, + $this->container->get(EpisodeActionRepository::class), + $this->container->get(EpisodeActionSaver::class) + ); + + /** @var EpisodeActionWriter $episodeActionWriter */ + $episodeActionWriter = $this->container->get(EpisodeActionWriter::class); + + $episodeActionEntity = new EpisodeActionEntity(); + $expectedPodcast = uniqid("test"); + $episodeActionEntity->setPodcast($expectedPodcast); + $expectedEpisode = uniqid("test"); + $episodeActionEntity->setEpisode($expectedEpisode); + $episodeActionEntity->setAction("PLAY"); + $episodeActionEntity->setPosition(5); + $episodeActionEntity->setStarted(0); + $episodeActionEntity->setTotal(123); + $episodeActionEntity->setTimestampEpoch(1633520363); + $episodeActionEntity->setUserId($userId); + $episodeActionEntity->setGuid(self::TEST_GUID); + $episodeActionWriter->save($episodeActionEntity); + + $response = $episodeActionController->list(); + self::assertCount(1, $response->getData()['actions']); + + $episodeActionInResponse = $response->getData()['actions'][0]; + self::assertSame("2021-10-06T11:39:23", $episodeActionInResponse['timestamp']); + self::assertSame($expectedEpisode, $episodeActionInResponse['episode']); + self::assertSame($expectedPodcast, $episodeActionInResponse['podcast']); + self::assertSame(self::TEST_GUID, $episodeActionInResponse['guid']); + self::assertSame(5, $episodeActionInResponse['position']); + self::assertSame(0, $episodeActionInResponse['started']); + self::assertSame(123, $episodeActionInResponse['total']); + self::assertSame("PLAY", $episodeActionInResponse['action']); + } + public function testEpisodeActionCreateAction(): void { $time = time(); $userId = uniqid('test_user', true); diff --git a/tests/Integration/Controller/SubscriptionChangeControllerTest.php b/tests/Integration/Controller/SubscriptionChangeControllerTest.php new file mode 100644 index 0000000..afc9d99 --- /dev/null +++ b/tests/Integration/Controller/SubscriptionChangeControllerTest.php @@ -0,0 +1,180 @@ +container = $app->getContainer(); + $this->db = \OC::$server->getDatabaseConnection(); + } + + /** + * @before + */ + public function before() + { + $this->startTransaction(); + } + + public function testSubscriptionChangeListAction() + { + $userId = uniqid("test_user"); + $subscriptionChangeController = new SubscriptionChangeController( + "gpoddersync", + $this->createMock(IRequest::class), + $userId, + $this->container->get(SubscriptionChangeSaver::class), + $this->container->get(SubscriptionChangeRepository::class) + ); + + /** @var SubscriptionChangeWriter $subscriptionChangeWriter*/ + $subscriptionChangeWriter = $this->container->get(SubscriptionChangeWriter::class); + + $mark = 1633520363; + $subscriptionChangeEntity = new SubscriptionChangeEntity(); + $expectedUrl1 = uniqid("test"); + $subscriptionChangeEntity->setUrl($expectedUrl1); + $subscriptionChangeEntity->setSubscribed(true); + $subscriptionChangeEntity->setUpdated(date("Y-m-d\TH:i:s", $mark+600)); + $subscriptionChangeEntity->setUserId($userId); + + $subscriptionChangeWriter->create($subscriptionChangeEntity); + + $subscriptionChangeEntity = new SubscriptionChangeEntity(); + $expectedUrl2 = uniqid("test"); + $subscriptionChangeEntity->setUrl($expectedUrl2); + $subscriptionChangeEntity->setSubscribed(false); + $subscriptionChangeEntity->setUpdated(date("Y-m-d\TH:i:s", $mark+1200)); + $subscriptionChangeEntity->setUserId($userId); + + $subscriptionChangeWriter->create($subscriptionChangeEntity); + + $response = $subscriptionChangeController->list($mark); + self::assertCount(1, $response->getData()['add']); + self::assertCount(1, $response->getData()['remove']); + + $subscriptionChangeInResponse1 = $response->getData()['add'][0]; + $subscriptionChangeInResponse2 = $response->getData()['remove'][0]; + self::assertSame($expectedUrl1, $subscriptionChangeInResponse1); + self::assertSame($expectedUrl2, $subscriptionChangeInResponse2); + } + + public function testSubscriptionChangeListWithoutSinceAction() + { + $userId = uniqid("test_user"); + $subscriptionChangeController = new SubscriptionChangeController( + "gpoddersync", + $this->createMock(IRequest::class), + $userId, + $this->container->get(SubscriptionChangeSaver::class), + $this->container->get(SubscriptionChangeRepository::class) + ); + + /** @var SubscriptionChangeWriter $subscriptionChangeWriter*/ + $subscriptionChangeWriter = $this->container->get(SubscriptionChangeWriter::class); + + $subscriptionChangeEntity = new SubscriptionChangeEntity(); + $expectedUrl1 = uniqid("test"); + $subscriptionChangeEntity->setUrl($expectedUrl1); + $subscriptionChangeEntity->setSubscribed(true); + $subscriptionChangeEntity->setUpdated("2021-10-06T11:39:23"); + $subscriptionChangeEntity->setUserId($userId); + + $subscriptionChangeWriter->create($subscriptionChangeEntity); + + $subscriptionChangeEntity = new SubscriptionChangeEntity(); + $expectedUrl2 = uniqid("test"); + $subscriptionChangeEntity->setUrl($expectedUrl2); + $subscriptionChangeEntity->setSubscribed(false); + $subscriptionChangeEntity->setUpdated("2021-10-06T11:49:23"); + $subscriptionChangeEntity->setUserId($userId); + + $subscriptionChangeWriter->create($subscriptionChangeEntity); + + $response = $subscriptionChangeController->list(); + self::assertCount(1, $response->getData()['add']); + self::assertCount(1, $response->getData()['remove']); + + $subscriptionChangeInResponse1 = $response->getData()['add'][0]; + $subscriptionChangeInResponse2 = $response->getData()['remove'][0]; + self::assertSame($expectedUrl1, $subscriptionChangeInResponse1); + self::assertSame($expectedUrl2, $subscriptionChangeInResponse2); + } + + public function testSubscriptionChangeCreateAction(): void { + $time = time(); + $userId = uniqid('test_user'); + + $subscriptionChangeController = new SubscriptionChangeController( + "gpoddersync", + $this->createMock(IRequest::class), + $userId, + $this->container->get(SubscriptionChangeSaver::class), + $this->container->get(SubscriptionChangeRepository::class) + ); + + $expectedAdd1 = "https://example.com/feed.rss"; + $expectedAdd2 = "https://example.org/feed.xml"; + $expectedRemove1 = "https://www.example.com/feed.rss"; + $expectedRemove2 = "https://www.example.com/feed.xml"; + + $response = $subscriptionChangeController->create( + [$expectedAdd1, $expectedAdd2], + [$expectedRemove1,$expectedRemove2] + ); + + $this->assertArrayHasKey('timestamp', $response->getData()); + $this->assertGreaterThanOrEqual($time, $response->getData()['timestamp']); + + /** @var SubscriptionChangeMapper $mapper */ + $mapper = $this->container->query(SubscriptionChangeMapper::class); + $subscriptionChangeAddEntities = $mapper->findAllSubscriptionState(true, (new DateTime)->setTimestamp(0), $userId); + $subscriptionChangeRemoveEntities = $mapper->findAllSubscriptionState(false, (new DateTime)->setTimestamp(0), $userId); + + $firstAdd = $subscriptionChangeAddEntities[0]->getUrl(); + $secondAdd = $subscriptionChangeAddEntities[1]->getUrl(); + $firstRemove = $subscriptionChangeRemoveEntities[0]->getUrl(); + $secondRemove = $subscriptionChangeRemoveEntities[1]->getUrl(); + + $this->assertSame($expectedAdd1, $firstAdd); + $this->assertSame($expectedAdd2, $secondAdd); + $this->assertSame($expectedRemove1, $firstRemove); + $this->assertSame($expectedRemove2, $secondRemove); + + } + + /** + * @after + */ + public function after(): void { + $this->rollbackTransaction(); + } +}