Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 58 additions & 46 deletions src/PantherTestCaseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,34 @@ trait PantherTestCaseTrait
/**
* @var bool
*/
public static $stopServerOnTeardown = true;
public static $stopServersOnTeardown = true;

/**
* @var string|null
*/
protected static $webServerDir;
protected static $webServerDir = null;

/**
* @var WebServerManager|null
* @var WebServerManager[]
*/
protected static $webServerManager;
protected static $webServerManagers = [];

/**
* The last started web server base uri.
*
* @var string|null
*/
protected static $baseUri;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This property is a problem. If people relies on it, we would maybe need a method to get the "baseUri" depending of options. Another way would be to name the client / web server when you create them.


/**
* @var GoutteClient|null
* @var GoutteClient[]
*/
protected static $goutteClient;
protected static $goutteClients = [];

/**
* @var PantherClient|null
* @var PantherClient[]
*/
protected static $pantherClient;
protected static $pantherClients = [];

/**
* @var array
Expand All @@ -69,61 +71,69 @@ trait PantherTestCaseTrait

public static function tearDownAfterClass()
{
if (self::$stopServerOnTeardown) {
static::stopWebServer();
if (self::$stopServersOnTeardown) {
static::stopWebServers();
}
}

public static function stopWebServer()
public static function stopWebServers(): void
{
if (null !== self::$webServerManager) {
self::$webServerManager->quit();
self::$webServerManager = null;
foreach (self::$webServerManagers as $webServerManager) {
$webServerManager->quit();
}

if (null !== self::$pantherClient) {
self::$pantherClient->quit();
self::$pantherClient = null;
}
self::$webServerManagers = [];

if (null !== self::$goutteClient) {
self::$goutteClient = null;
foreach (self::$pantherClients as $pantherClient) {
$pantherClient->quit();
}

self::$pantherClients = [];

self::$goutteClients = [];

self::$baseUri = null;
}

/**
* @param array $options see {@see $defaultOptions}
*/
public static function startWebServer(array $options = []): void
public static function startWebServer(array $options = []): string
{
if (null !== static::$webServerManager) {
return;
}

if ($externalBaseUri = $options['external_base_uri'] ?? $_SERVER['PANTHER_EXTERNAL_BASE_URI'] ?? self::$defaultOptions['external_base_uri']) {
self::$baseUri = $externalBaseUri;

return;
return $externalBaseUri;
}

$options = [
'webServerDir' => $options['webServerDir'] ?? static::$webServerDir ?? $_SERVER['PANTHER_WEB_SERVER_DIR'] ?? self::$defaultOptions['webServerDir'],
'hostname' => $options['hostname'] ?? self::$defaultOptions['hostname'],
'port' => (int) ($options['port'] ?? $_SERVER['PANTHER_WEB_SERVER_PORT'] ?? self::$defaultOptions['port']),
'router' => $options['router'] ?? $_SERVER['PANTHER_WEB_SERVER_ROUTER'] ?? self::$defaultOptions['router'],
];
$hostname = $options['hostname'] ?? self::$defaultOptions['hostname'];
$port = (int) ($options['port'] ?? $_SERVER['PANTHER_WEB_SERVER_PORT'] ?? self::$defaultOptions['port']);

$baseUri = sprintf('http://%s:%s', $hostname, $port);
if (!isset(self::$webServerManagers[$baseUri])) {
$webServerDir = $options['webServerDir'] ?? static::$webServerDir ?? $_SERVER['PANTHER_WEB_SERVER_DIR'] ?? self::$defaultOptions['webServerDir'];
$router = $options['router'] ?? $_SERVER['PANTHER_WEB_SERVER_ROUTER'] ?? self::$defaultOptions['router'];

$webServerManager = new WebServerManager($webServerDir, $hostname, $port, $router);
$webServerManager->start();

self::$webServerManager = new WebServerManager(...array_values($options));
self::$webServerManager->start();
self::$baseUri = $baseUri;

self::$baseUri = sprintf('http://%s:%s', $options['hostname'], $options['port']);
self::$webServerManagers[$baseUri] = $webServerManager;
}

return $baseUri;
}

public static function isWebServerStarted()
public static function isWebServerStarted(): bool
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might also be a problem. Maybe it would need an argument to know if a particular web server was started (so a need to name them ?)

{
return self::$webServerManager && self::$webServerManager->isStarted();
foreach (self::$webServerManagers as $webServerManager) {
if ($webServerManager->isStarted()) {
return true;
}
}

return false;
}

/**
Expand All @@ -132,16 +142,17 @@ public static function isWebServerStarted()
*/
protected static function createPantherClient(array $options = [], array $kernelOptions = []): PantherClient
{
self::startWebServer($options);
if (null === self::$pantherClient) {
self::$pantherClient = Client::createChromeClient(null, null, [], self::$baseUri);
$baseUri = self::startWebServer($options);

if (!isset(self::$pantherClients[$baseUri])) {
self::$pantherClients[$baseUri] = Client::createChromeClient(null, null, [], $baseUri);
}

if (\is_a(self::class, KernelTestCase::class, true)) {
static::bootKernel($kernelOptions);
}

return self::$pantherClient;
return self::$pantherClients[$baseUri];
}

/**
Expand All @@ -154,18 +165,19 @@ protected static function createGoutteClient(array $options = [], array $kernelO
throw new \RuntimeException('Goutte is not installed. Run "composer req fabpot/goutte".');
}

self::startWebServer($options);
if (null === self::$goutteClient) {
$baseUri = self::startWebServer($options);

if (!isset(self::$goutteClients[$baseUri])) {
$goutteClient = new GoutteClient();
$goutteClient->setClient(new GuzzleClient(['base_uri' => self::$baseUri]));
$goutteClient->setClient(new GuzzleClient(['base_uri' => $baseUri]));

self::$goutteClient = $goutteClient;
self::$goutteClients[$baseUri] = $goutteClient;
}

if (\is_a(self::class, KernelTestCase::class, true)) {
static::bootKernel($kernelOptions);
}

return self::$goutteClient;
return self::$goutteClients[$baseUri];
}
}