Skip to content

Commit

Permalink
Tests for new Events
Browse files Browse the repository at this point in the history
  • Loading branch information
TumTum committed Apr 20, 2023
1 parent 0dede7c commit 90a94a4
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
117 changes: 117 additions & 0 deletions tests/Makaira/Connect/ApiEventTest.php
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']);
}
}
2 changes: 2 additions & 0 deletions tests/Makaira/Connect/OxidMocks/oxRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 90a94a4

Please sign in to comment.