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] 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 0000000..fc69f3e --- /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 ab95562..5a9b01b 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;