From 27e33711084a899aa094fda28a6738233c0a7d47 Mon Sep 17 00:00:00 2001 From: Kris Sum Date: Thu, 4 Jun 2020 11:17:15 +0100 Subject: [PATCH] Added support for activity updating --- src/FeedManagerInterface.php | 6 +++ src/ModelListener.php | 11 +++++ tests/stubs/ActivityWithExtraData.php | 70 +++++++++++++++++++++++++++ tests/unit/ModelListenerTest.php | 54 +++++++++++++++++++++ 4 files changed, 141 insertions(+) create mode 100644 tests/stubs/ActivityWithExtraData.php diff --git a/src/FeedManagerInterface.php b/src/FeedManagerInterface.php index 0f6a3fb..136be69 100644 --- a/src/FeedManagerInterface.php +++ b/src/FeedManagerInterface.php @@ -3,9 +3,15 @@ namespace GetStream\Doctrine; use GetStream\Stream\Feed; +use GetStream\Stream\Client; interface FeedManagerInterface { + /** + * @return Client + */ + public function getClient(); + /** * @param string $feed * @param string $id diff --git a/src/ModelListener.php b/src/ModelListener.php index 2855aaf..a6e426e 100644 --- a/src/ModelListener.php +++ b/src/ModelListener.php @@ -46,6 +46,17 @@ public function activityDeleting(ActivityInterface $instance) ->removeActivity($instance->activityForeignId(), true); } + /** + * @ORM\PostUpdate + * + * @param ActivityInterface $instance + */ + public function activityUpdated(ActivityInterface $instance) + { + $activity = $this->createActivity($instance); + $this->feedManager->getClient()->updateActivities([$activity]); + } + /** * @param ActivityInterface $instance * diff --git a/tests/stubs/ActivityWithExtraData.php b/tests/stubs/ActivityWithExtraData.php new file mode 100644 index 0000000..8e1854b --- /dev/null +++ b/tests/stubs/ActivityWithExtraData.php @@ -0,0 +1,70 @@ + ['tag1', 'tag2'], + 'pinned' => false, + ]; + + /** + * @return string + */ + protected function activityId() + { + return '1'; + } + + /** + * @return string + */ + public function activityActorId() + { + return '2'; + } + + /** + * @return string + */ + public function activityActor() + { + return '\User:2'; + } + + /** + * @return string + */ + public function activityVerb() + { + return 'like'; + } + + /** + * @return \DateTimeImmutable + */ + public function activityTime() + { + return Chronos::now(); + } + + public function activityExtraData() + { + return $this->extraData; + } + + /** + * @param array $extraData + */ + public function setExtraData(array $extraData = []) + { + $this->extraData = $extraData; + } +} diff --git a/tests/unit/ModelListenerTest.php b/tests/unit/ModelListenerTest.php index 5e2f7ba..460bd28 100644 --- a/tests/unit/ModelListenerTest.php +++ b/tests/unit/ModelListenerTest.php @@ -6,8 +6,10 @@ use GetStream\Doctrine\FeedManagerInterface; use GetStream\Doctrine\ModelListener; use GetStream\Doctrine\Stubs\Activity; +use GetStream\Doctrine\Stubs\ActivityWithExtraData; use GetStream\Stream\Feed; use PHPUnit\Framework\TestCase; +use GetStream\Stream\Client; class ModelListenerTest extends TestCase { @@ -49,6 +51,58 @@ public function activityCreated() $this->assertNull($result); } + /** @test */ + public function activityUpdated() + { + // Arrange + $manager = $this->createMock(FeedManagerInterface::class); + $feed = $this->createMock(Feed::class); + Chronos::setTestNow(new Chronos('2017-01-01T00:00:00+00:00')); + $listener = new ModelListener($manager); + + $manager->method('getUserFeed')->with('2')->willReturn($feed); + $feed->method('addActivity')->with([ + 'actor' => '\User:2', + 'verb' => 'like', + 'object' => 'GetStream\Doctrine\Stubs\ActivityWithExtraData:1', + 'foreign_id' => 'GetStream\Doctrine\Stubs\ActivityWithExtraData:1', + 'time' => '2017-01-01T00:00:00+0000', + 'to' => [], + 'tags' => ['tag1', 'tag2'], + 'pinned' => false + ]); + + $act = new ActivityWithExtraData(); + $result = $listener->activityCreated($act); + + $client = $this->createMock(Client::class); + + $manager->method('getClient')->willReturn($client); + $client->method('updateActivities')->with([ + [ + 'actor' => '\User:2', + 'verb' => 'like', + 'object' => 'GetStream\Doctrine\Stubs\ActivityWithExtraData:1', + 'foreign_id' => 'GetStream\Doctrine\Stubs\ActivityWithExtraData:1', + 'time' => '2017-01-01T00:00:00+0000', + 'to' => [], + 'tags' => ['test3', 'test4'], + 'pinned' => true + ] + ]); + + $act->setExtraData([ + 'tags' => ['test3', 'test4'], + 'pinned' => true + ]); + + // Act + $result = $listener->activityUpdated($act); + + // Assert + $this->assertNull($result); + } + /** @test */ public function activityDeleting() {