Skip to content

Commit

Permalink
Additional test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
thomascorthals committed Jan 5, 2024
1 parent 207fa84 commit 058006a
Showing 1 changed file with 86 additions and 3 deletions.
89 changes: 86 additions & 3 deletions tests/Plugin/NoWaitForResponseRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,29 @@
namespace Solarium\Tests\Plugin;

use PHPUnit\Framework\TestCase;
use Solarium\Client;
use Solarium\Core\Client\Adapter\AdapterInterface;
use Solarium\Core\Client\Adapter\Curl;
use Solarium\Core\Client\Adapter\Http;
use Solarium\Core\Client\Adapter\TimeoutAwareInterface;
use Solarium\Core\Client\Client;
use Solarium\Core\Client\Endpoint;
use Solarium\Core\Client\Request;
use Solarium\Core\Event\Events;
use Solarium\Core\Event\PreExecuteRequest as PreExecuteRequestEvent;
use Solarium\Exception\HttpException;
use Solarium\Plugin\NoWaitForResponseRequest;
use Solarium\QueryType\Select\Query\Query;
use Solarium\Tests\Integration\TestClientFactory;

class NoWaitForResponseRequestTest extends TestCase
{
/**
* @var \Solarium\Plugin\NoWaitForResponseRequest
* @var NoWaitForResponseRequest
*/
protected $plugin;

/**
* @var \Solarium\Core\Client\Adapter\Curl
* @var Client
*/
protected $client;

Expand Down Expand Up @@ -97,6 +101,85 @@ public function testExecuteRequest()
$this->assertTrue($this->client->getAdapter()->getOption('return_transfer'));
}

public function testSetFastTimeout()
{
$observer = new class() extends Http {
public $newTimeout;

public function setTimeout(int $timeoutInSeconds): self
{
if (!isset($this->newTimeout)) {
$this->newTimeout = $timeoutInSeconds;
}

return parent::setTimeout($timeoutInSeconds);
}
};
$this->client->setAdapter($observer);

$requestOutput = $this->client->createRequest($this->query);
$endpoint = $this->client->getEndpoint();
$endpoint->setCore('my_core');
$event = new PreExecuteRequestEvent($requestOutput, $endpoint);
$this->plugin->preExecuteRequest($event);

$this->assertSame(TimeoutAwareInterface::FAST_TIMEOUT, $observer->newTimeout);
}

public function testSetFastTimeoutWithConnectionTimeout()
{
$observer = new class() extends Curl {
public $newTimeout;

public function setTimeout(int $timeoutInSeconds): self
{
if (!isset($this->newTimeout)) {
$this->newTimeout = $timeoutInSeconds;
}

return parent::setTimeout($timeoutInSeconds);
}
};
$observer->setConnectionTimeout(1);
$this->client->setAdapter($observer);

$requestOutput = $this->client->createRequest($this->query);
$endpoint = $this->client->getEndpoint();
$endpoint->setCore('my_core');
$event = new PreExecuteRequestEvent($requestOutput, $endpoint);
$this->plugin->preExecuteRequest($event);

$this->assertSame(1 + TimeoutAwareInterface::FAST_TIMEOUT, $observer->newTimeout);
}

public function testUnrelatedHttpExceptionIsRethrown()
{
$requestOutput = $this->client->createRequest($this->query);
$endpoint = $this->client->getEndpoint();
$event = new PreExecuteRequestEvent($requestOutput, $endpoint);

// thrown by AdapterHelper::buildUri() because we didn't set a core or collection on the endpoint
$this->expectException(HttpException::class);
$this->expectExceptionCode(404);
$this->plugin->preExecuteRequest($event);
}

public function testUnrelatedExceptionIsRethrown()
{
$requestOutput = $this->client->createRequest($this->query);
$endpoint = $this->client->getEndpoint();
$event = new class($requestOutput, $endpoint) extends PreExecuteRequestEvent {
public function getEndpoint(): Endpoint
{
throw new \RuntimeException('', 42);
}
};

$this->expectException(\RuntimeException::class);
$this->expectExceptionCode(42);
$this->plugin->preExecuteRequest($event);
}

public function testPluginIntegration()
{
$client = TestClientFactory::createWithCurlAdapter();
Expand Down

0 comments on commit 058006a

Please sign in to comment.