-
-
Notifications
You must be signed in to change notification settings - Fork 236
feat: handle several clients and web servers in trait #161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -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; | ||
|
|
||
| /** | ||
| * @var GoutteClient|null | ||
| * @var GoutteClient[] | ||
| */ | ||
| protected static $goutteClient; | ||
| protected static $goutteClients = []; | ||
|
|
||
| /** | ||
| * @var PantherClient|null | ||
| * @var PantherClient[] | ||
| */ | ||
| protected static $pantherClient; | ||
| protected static $pantherClients = []; | ||
|
|
||
| /** | ||
| * @var array | ||
|
|
@@ -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 | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -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]; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -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]; | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.