From b2cda0bf1af3ba8e47e3d917d7c3fcb8b0c29bcf Mon Sep 17 00:00:00 2001 From: msantisteban Date: Mon, 16 Jun 2025 09:59:08 +0200 Subject: [PATCH] fix: symfony uuid compatibility add symfony/uid to composer suggest use match_phrase for found item with uuid update functional tests #27 --- composer.json | 6 +++- .../CurrentDataTrackerProvider.php | 8 ++++- tests/Fixtures/app/Model/DummyBlogPost.php | 13 +++++++ tests/Fixtures/app/Model/DummyComment.php | 35 +++++++++++++++++++ tests/FunctionalTests/ActivityLogTest.php | 23 ++++++++++-- 5 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 tests/Fixtures/app/Model/DummyComment.php diff --git a/composer.json b/composer.json index 2af0628..d208fac 100644 --- a/composer.json +++ b/composer.json @@ -33,7 +33,11 @@ "symfony/css-selector": "7.3.*", "symfony/console": "7.3.*", "symfony/phpunit-bridge": "^7.3", - "symfony/test-pack": "1.1.0" + "symfony/test-pack": "1.1.0", + "symfony/uid": "7.3.*" + }, + "suggest": { + "symfony/uid": "Provides an object-oriented API to generate and represent UIDs" }, "prefer-stable": true, "autoload": { diff --git a/src/DataProvider/CurrentDataTrackerProvider.php b/src/DataProvider/CurrentDataTrackerProvider.php index df5c5a6..3a41103 100644 --- a/src/DataProvider/CurrentDataTrackerProvider.php +++ b/src/DataProvider/CurrentDataTrackerProvider.php @@ -6,6 +6,7 @@ use Locastic\Loggastic\Bridge\Elasticsearch\ElasticsearchService; use Locastic\Loggastic\Model\Output\CurrentDataTracker; use Locastic\Loggastic\Model\Output\CurrentDataTrackerInterface; +use Symfony\Component\Uid\Uuid; final class CurrentDataTrackerProvider implements CurrentDataTrackerProviderInterface { @@ -17,8 +18,13 @@ public function getCurrentDataTrackerByClassAndId(string $className, $objectId): { $elasticContext = $this->elasticsearchContextFactory->create($className); + $queryTerm = 'term'; + if (class_exists(Uuid::class) && $objectId instanceof Uuid) { + $queryTerm = 'match_phrase'; + } + $body = [ - 'query' => ['term' => ['objectId' => $objectId]], + 'query' => [$queryTerm => ['objectId' => $objectId]], ]; //todo move class to config diff --git a/tests/Fixtures/app/Model/DummyBlogPost.php b/tests/Fixtures/app/Model/DummyBlogPost.php index 0a43943..62cc7b3 100644 --- a/tests/Fixtures/app/Model/DummyBlogPost.php +++ b/tests/Fixtures/app/Model/DummyBlogPost.php @@ -30,6 +30,9 @@ class DummyBlogPost #[Groups(['dummy_blog_post_log'])] private Collection $photos; + /** @Groups({"dummy_blog_post_log"}) */ + private Collection $comments; + public function __construct() { $this->photos = new ArrayCollection(); @@ -104,4 +107,14 @@ public function setPhotos(Collection $photos): void { $this->photos = $photos; } + + public function getComments(): Collection + { + return $this->comments; + } + + public function setComments(Collection $comments): void + { + $this->comments = $comments; + } } diff --git a/tests/Fixtures/app/Model/DummyComment.php b/tests/Fixtures/app/Model/DummyComment.php new file mode 100644 index 0000000..8e25df6 --- /dev/null +++ b/tests/Fixtures/app/Model/DummyComment.php @@ -0,0 +1,35 @@ +id; + } + + public function setId(Uuid $id): void + { + $this->id = $id; + } + + public function getContent(): ?string + { + return $this->content; + } + + public function setContent(?string $content): void + { + $this->content = $content; + } +} diff --git a/tests/FunctionalTests/ActivityLogTest.php b/tests/FunctionalTests/ActivityLogTest.php index 8f1e6f1..5f1d2e8 100644 --- a/tests/FunctionalTests/ActivityLogTest.php +++ b/tests/FunctionalTests/ActivityLogTest.php @@ -9,8 +9,10 @@ use Locastic\Loggastic\Logger\ActivityLoggerInterface; use Locastic\Loggastic\Model\Output\ActivityLogInterface; use Locastic\Loggastic\Tests\Fixtures\App\Model\DummyBlogPost; +use Locastic\Loggastic\Tests\Fixtures\App\Model\DummyComment; use Locastic\Loggastic\Tests\Fixtures\App\Model\DummyPhoto; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; +use Symfony\Component\Uid\Uuid; class ActivityLogTest extends KernelTestCase { @@ -112,6 +114,7 @@ public function testLogEdit(): void $this->blogPost->setTags(['#php', '#locastic', '#elasticSearch']); $this->blogPost->setEnabled(true); $this->blogPost->getPhotos()->first()->setPath('https://locastic.com/blog'); + $this->blogPost->getComments()->first()->setContent('comment 1 edited'); $this->activityLogger->logUpdatedItem($this->blogPost); @@ -132,7 +135,10 @@ public function testLogEdit(): void 'enabled' => false, 'photos' => [ 1950 => ['path' => 'https://locastic.com'], - ] + ], + 'comments' => [ + '019777b9-346a-7eb9-b289-1a7327b54dc1' => ['content' => 'comment 1'], + ], ], 'currentValues' => [ 'title' => 'Activity Logs using Elasticsearch', @@ -140,7 +146,10 @@ public function testLogEdit(): void 'enabled' => true, 'photos' => [ 1950 => ['path' => 'https://locastic.com/blog'], - ] + ], + 'comments' => [ + '019777b9-346a-7eb9-b289-1a7327b54dc1' => ['content' => 'comment 1 edited'], + ], ], ], $editedLog->getDataChanges()); } @@ -232,6 +241,16 @@ private function createDummyBlogPost(): DummyBlogPost $blogPost->setPhotos(new ArrayCollection([$photo1, $photo2])); + $comment1 = new DummyComment(); + $comment1->setId(Uuid::fromString('019777b9-346a-7eb9-b289-1a7327b54dc1')); + $comment1->setContent('comment 1'); + + $comment2 = new DummyComment(); + $comment2->setId(Uuid::fromString('019777b9-346a-7eb9-b289-1a7327fad6fb')); + $comment2->setContent('comment 2'); + + $blogPost->setComments(new ArrayCollection([$comment1, $comment2])); + return $blogPost; } }