-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace oxidprojects\DI\Tests\Makaira\Connect; | ||
|
||
use Makaira\Connect\Core\Autosuggester; | ||
use Makaira\Connect\Event\AutoSuggesterResponseEvent; | ||
use Makaira\Connect\Event\ModifierQueryRequestEvent; | ||
use Makaira\Connect\Event\SearchResponseEvent; | ||
use Makaira\Connect\IntegrationTest; | ||
use Makaira\Connect\Utils\OperationalIntelligence; | ||
use Makaira\Query; | ||
use Makaira\Result; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
|
||
class ApiEventTest extends IntegrationTest | ||
{ | ||
public function testQueryAutosuggesterEvent() | ||
{ | ||
//Arrange | ||
$container = $this->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']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters