From 0dede7cb5da04cd7ee8b471b9ce83890b5fbdf60 Mon Sep 17 00:00:00 2001 From: Tobias Matthaiou Date: Thu, 20 Apr 2023 13:39:29 +0200 Subject: [PATCH 1/3] To modify the query and response to Makaira API, with DI. --- services.yaml | 1 + src/Makaira/Connect/Core/Autosuggester.php | 20 ++++++++++- .../Event/AutoSuggesterResponseEvent.php | 31 +++++++++++++++++ .../Event/ModifierQueryRequestEvent.php | 33 +++++++++++++++++++ .../Connect/Event/SearchResponseEvent.php | 33 +++++++++++++++++++ .../core/makaira_connect_request_handler.php | 20 ++++++++++- 6 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 src/Makaira/Connect/Event/AutoSuggesterResponseEvent.php create mode 100644 src/Makaira/Connect/Event/ModifierQueryRequestEvent.php create mode 100644 src/Makaira/Connect/Event/SearchResponseEvent.php diff --git a/services.yaml b/services.yaml index 1235d8a8..f89c34e1 100644 --- a/services.yaml +++ b/services.yaml @@ -112,6 +112,7 @@ services: $oxLang: '@makaira.connect.oxid.language' $operationalIntelligence: '@Makaira\Connect\Utils\OperationalIntelligence' $searchHandler: '@Makaira\Connect\SearchHandler' + $dispatcher: '@Symfony\Component\EventDispatcher\EventDispatcherInterface' # makaira.connect.searchhandler Makaira\Connect\SearchHandler: diff --git a/src/Makaira/Connect/Core/Autosuggester.php b/src/Makaira/Connect/Core/Autosuggester.php index 66491fc8..45f1560e 100644 --- a/src/Makaira/Connect/Core/Autosuggester.php +++ b/src/Makaira/Connect/Core/Autosuggester.php @@ -10,11 +10,14 @@ namespace Makaira\Connect\Core; +use Makaira\Connect\Event\ModifierQueryRequestEvent; +use Makaira\Connect\Event\AutoSuggesterResponseEvent; use oxLang as Language; use Makaira\Connect\SearchHandler; use Makaira\Connect\Utils\OperationalIntelligence; use Makaira\Constraints; use Makaira\Query; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** * Class makaira_connect_autosuggester @@ -36,14 +39,21 @@ class Autosuggester */ private $searchHandler; + /** + * @var EventDispatcherInterface + */ + private $dispatcher; + public function __construct( Language $oxLang, OperationalIntelligence $operationalIntelligence, - SearchHandler $searchHandler + SearchHandler $searchHandler, + EventDispatcherInterface $dispatcher ) { $this->oxLang = $oxLang; $this->operationalIntelligence = $operationalIntelligence; $this->searchHandler = $searchHandler; + $this->dispatcher = $dispatcher; } /** @@ -373,6 +383,10 @@ protected function prepareProductItem(&$doc, &$product) */ public function modifyRequest(Query &$query) { + $this->dispatcher->dispatch( + ModifierQueryRequestEvent::NAME_AUTOSUGGESTER, + new ModifierQueryRequestEvent($query) + ); } /** @@ -380,5 +394,9 @@ public function modifyRequest(Query &$query) */ public function afterSearchRequest(&$result) { + $this->dispatcher->dispatch( + AutoSuggesterResponseEvent::NAME, + new AutoSuggesterResponseEvent($result) + ); } } diff --git a/src/Makaira/Connect/Event/AutoSuggesterResponseEvent.php b/src/Makaira/Connect/Event/AutoSuggesterResponseEvent.php new file mode 100644 index 00000000..49e440f7 --- /dev/null +++ b/src/Makaira/Connect/Event/AutoSuggesterResponseEvent.php @@ -0,0 +1,31 @@ +result = $result; + } + + /** + * @return Result + */ + public function getResult() + { + return $this->result; + } +} diff --git a/src/Makaira/Connect/Event/ModifierQueryRequestEvent.php b/src/Makaira/Connect/Event/ModifierQueryRequestEvent.php new file mode 100644 index 00000000..4d9e8afc --- /dev/null +++ b/src/Makaira/Connect/Event/ModifierQueryRequestEvent.php @@ -0,0 +1,33 @@ +query = $query; + } + + /** + * @return Query + */ + public function getQuery() + { + return $this->query; + } +} diff --git a/src/Makaira/Connect/Event/SearchResponseEvent.php b/src/Makaira/Connect/Event/SearchResponseEvent.php new file mode 100644 index 00000000..15e8b9fa --- /dev/null +++ b/src/Makaira/Connect/Event/SearchResponseEvent.php @@ -0,0 +1,33 @@ +productIds = new \ArrayObject($productIds); + } + + /** + * @return array + */ + public function getProductIds() + { + return $this->productIds; + } +} diff --git a/src/oxid/core/makaira_connect_request_handler.php b/src/oxid/core/makaira_connect_request_handler.php index 636f43c4..34274963 100644 --- a/src/oxid/core/makaira_connect_request_handler.php +++ b/src/oxid/core/makaira_connect_request_handler.php @@ -16,6 +16,9 @@ use Makaira\Query; use Makaira\Result; use Makaira\Connect\Connect; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Makaira\Connect\Event\ModifierQueryRequestEvent; +use Makaira\Connect\Event\SearchResponseEvent; /** * Class makaira_connect_request_handler @@ -273,14 +276,29 @@ public function deletePageNumberCookie() protected function modifyRequest(Query $query) { + $dispatcher = Connect::getContainerFactory()->getContainer()->get(EventDispatcherInterface::class); + $dispatcher->dispatch( + ModifierQueryRequestEvent::NAME_SEARCH, + new ModifierQueryRequestEvent($query) + ); + return $query; } /** * @param array $productIds */ - public function afterSearchRequest(array $productIds = []) + public function afterSearchRequest(array &$productIds = []) { + $searchResponseEvent = new SearchResponseEvent($productIds); + + $dispatcher = Connect::getContainerFactory()->getContainer()->get(EventDispatcherInterface::class); + $dispatcher->dispatch( + SearchResponseEvent::NAME, + $searchResponseEvent + ); + + $productIds = (array)$searchResponseEvent->getProductIds(); } /** From 90a94a4b86dd3476ec6fcee1b5a1bb519e793d61 Mon Sep 17 00:00:00 2001 From: Tobias Matthaiou <225997+TumTum@users.noreply.github.com> Date: Thu, 20 Apr 2023 15:10:29 +0200 Subject: [PATCH 2/3] Tests for new Events --- tests/Makaira/Connect/ApiEventTest.php | 117 ++++++++++++++++++ .../Makaira/Connect/OxidMocks/oxRegistry.php | 2 + 2 files changed, 119 insertions(+) create mode 100644 tests/Makaira/Connect/ApiEventTest.php diff --git a/tests/Makaira/Connect/ApiEventTest.php b/tests/Makaira/Connect/ApiEventTest.php new file mode 100644 index 00000000..fc69f3ec --- /dev/null +++ b/tests/Makaira/Connect/ApiEventTest.php @@ -0,0 +1,117 @@ +getContainer(); + $event = $container->get(EventDispatcherInterface::class); + $autosuggester = $container->get(Autosuggester::class); + $query = new Query(['searchPhrase' => 'Tisch']); + + //Act + $event->addListener(ModifierQueryRequestEvent::NAME_AUTOSUGGESTER, [$this, 'listenerModifierQueryRequest']); + + $autosuggester->modifyRequest($query); + $accept = $query->searchPhrase; + + //Assert + $this->assertEquals('Bett', $accept); + } + + public function testQuerySearchEvent() + { + //Arrange + $container = $this->getContainer(); + $event = $container->get(EventDispatcherInterface::class); + $makaira_connect_request_handler = new \makaira_connect_request_handler(); + $reflectionClass = new \ReflectionClass(\makaira_connect_request_handler::class); + $methodModifyRequest = $reflectionClass->getMethod('modifyRequest'); + $methodModifyRequest->setAccessible(true); + + $query = new Query(['searchPhrase' => 'Tische']); + + //Act + $event->addListener(ModifierQueryRequestEvent::NAME_SEARCH, [$this, 'listenerModifierQueryRequest']); + + $methodModifyRequest->invoke($makaira_connect_request_handler, $query); + $accept = $query->searchPhrase; + + //Assert + $this->assertEquals('Bett', $accept); + } + + public function testResponseSearchEvent() + { + //Arrange + $container = $this->getContainer(); + $event = $container->get(EventDispatcherInterface::class); + $makaira_connect_request_handler = new \makaira_connect_request_handler(); + + $productIds = ['1234']; + $expect = ['1234', 'Extra ID']; + + //Act + $event->addListener(SearchResponseEvent::NAME, [$this, 'listenerSearchResponse']); + $makaira_connect_request_handler->afterSearchRequest($productIds); + + //Assert + $this->assertEquals($expect, $productIds); + } + + public function testResponseAutoSuggesterResponseEvent() + { + //Arrange + $container = $this->getContainer(); + $event = $container->get(EventDispatcherInterface::class); + $autosuggester = $container->get(Autosuggester::class); + + $result = new Result(['count' => 33]); + + //Act + $event->addListener(AutoSuggesterResponseEvent::NAME, [$this, 'listenerResponseAutoSuggester']); + $autosuggester->afterSearchRequest($result); + + //Assert + $this->assertEquals(66, $result->count); + } + + public function listenerResponseAutoSuggester(AutoSuggesterResponseEvent $event) { + $event->getResult()->count = 66; + } + + public function listenerModifierQueryRequest(ModifierQueryRequestEvent $event) { + $event->getQuery()->searchPhrase = 'Bett'; + } + + public function listenerSearchResponse(SearchResponseEvent $event) + { + $productIds = $event->getProductIds(); + $productIds[] = 'Extra ID'; + } + + protected function setUp(): void + { + $container = $this->getContainer(); + $event = $container->get(EventDispatcherInterface::class); + $event->removeListener(ModifierQueryRequestEvent::NAME_AUTOSUGGESTER, [$this, 'listenerModifierQueryRequest']); + $event->removeListener(ModifierQueryRequestEvent::NAME_SEARCH, [$this, 'listenerModifierQueryRequest']); + $event->removeListener(SearchResponseEvent::NAME, [$this, 'listenerSearchResponse']); + $event->removeListener(AutoSuggesterResponseEvent::NAME, [$this, 'listenerResponseAutoSuggester']); + } +} diff --git a/tests/Makaira/Connect/OxidMocks/oxRegistry.php b/tests/Makaira/Connect/OxidMocks/oxRegistry.php index ab955628..5a9b01bf 100644 --- a/tests/Makaira/Connect/OxidMocks/oxRegistry.php +++ b/tests/Makaira/Connect/OxidMocks/oxRegistry.php @@ -77,6 +77,8 @@ public static function get($key) case 'oxseoencodercategory': case 'oxseoencodermanufacturer': return new oxSeoEncoder(); + case 'makaira_cookie_utils': + return new makaira_cookie_utils(); } return null; From 4bb29a805d791565ee1f583fd6aa2e27d71f417c Mon Sep 17 00:00:00 2001 From: Tobias Matthaiou <225997+TumTum@users.noreply.github.com> Date: Thu, 20 Apr 2023 16:20:56 +0200 Subject: [PATCH 3/3] It is not a '\Makaira\Result' object --- src/Makaira/Connect/Core/Autosuggester.php | 4 +++- src/Makaira/Connect/Event/AutoSuggesterResponseEvent.php | 6 +++--- tests/Makaira/Connect/ApiEventTest.php | 8 +++++--- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/Makaira/Connect/Core/Autosuggester.php b/src/Makaira/Connect/Core/Autosuggester.php index 45f1560e..15fa8c9c 100644 --- a/src/Makaira/Connect/Core/Autosuggester.php +++ b/src/Makaira/Connect/Core/Autosuggester.php @@ -394,9 +394,11 @@ public function modifyRequest(Query &$query) */ public function afterSearchRequest(&$result) { + $event = new AutoSuggesterResponseEvent($result); $this->dispatcher->dispatch( AutoSuggesterResponseEvent::NAME, - new AutoSuggesterResponseEvent($result) + $event ); + $result = (array)$event->getResult(); } } diff --git a/src/Makaira/Connect/Event/AutoSuggesterResponseEvent.php b/src/Makaira/Connect/Event/AutoSuggesterResponseEvent.php index 49e440f7..7acd084f 100644 --- a/src/Makaira/Connect/Event/AutoSuggesterResponseEvent.php +++ b/src/Makaira/Connect/Event/AutoSuggesterResponseEvent.php @@ -16,13 +16,13 @@ class AutoSuggesterResponseEvent extends Event private $result; - public function __construct(Result &$result) + public function __construct($result) { - $this->result = $result; + $this->result = new \ArrayObject($result); } /** - * @return Result + * @return \ArrayObject */ public function getResult() { diff --git a/tests/Makaira/Connect/ApiEventTest.php b/tests/Makaira/Connect/ApiEventTest.php index fc69f3ec..227cc880 100644 --- a/tests/Makaira/Connect/ApiEventTest.php +++ b/tests/Makaira/Connect/ApiEventTest.php @@ -81,18 +81,20 @@ public function testResponseAutoSuggesterResponseEvent() $event = $container->get(EventDispatcherInterface::class); $autosuggester = $container->get(Autosuggester::class); - $result = new Result(['count' => 33]); + $result = ['count' => 33]; //Act $event->addListener(AutoSuggesterResponseEvent::NAME, [$this, 'listenerResponseAutoSuggester']); $autosuggester->afterSearchRequest($result); //Assert - $this->assertEquals(66, $result->count); + $this->assertEquals(66, $result['count']); } public function listenerResponseAutoSuggester(AutoSuggesterResponseEvent $event) { - $event->getResult()->count = 66; + $result = $event->getResult(); + $this->assertInstanceOf(\ArrayObject::class, $result); + $result['count'] = 66; } public function listenerModifierQueryRequest(ModifierQueryRequestEvent $event) {