Skip to content

Commit ff32f91

Browse files
committed
[minor] remove Browser::create()
It was a confusing system just to avoid an "else"...
1 parent 27e6f4c commit ff32f91

File tree

2 files changed

+45
-76
lines changed

2 files changed

+45
-76
lines changed

src/Browser.php

-16
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,6 @@ public function __construct(DriverInterface $driver)
3535
$this->mink = new Mink([self::SESSION => new Session($driver)]);
3636
}
3737

38-
/**
39-
* @internal
40-
*
41-
* @return static
42-
*/
43-
final public static function create(callable $factory): self
44-
{
45-
$browser = $factory();
46-
47-
if (!$browser instanceof self) {
48-
throw new \RuntimeException(\sprintf('The factory callable must return an instance of "%s".', self::class));
49-
}
50-
51-
return $browser;
52-
}
53-
5438
/**
5539
* @return static
5640
*/

src/Browser/Test/HasBrowser.php

+45-60
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,23 @@ final public static function _resetBrowserClients(): void
3232

3333
protected function pantherBrowser(array $options = [], array $kernelOptions = [], array $managerOptions = []): PantherBrowser
3434
{
35-
$browser = PantherBrowser::create(function() use ($options, $kernelOptions, $managerOptions) {
36-
if (!$this instanceof PantherTestCase) {
37-
throw new \RuntimeException(\sprintf('The "%s" method can only be used on TestCases that extend "%s".', __METHOD__, PantherTestCase::class));
38-
}
39-
40-
$class = $_SERVER['PANTHER_BROWSER_CLASS'] ?? PantherBrowser::class;
41-
42-
if (!\is_a($class, PantherBrowser::class, true)) {
43-
throw new \RuntimeException(\sprintf('"PANTHER_BROWSER_CLASS" env variable must reference a class that extends %s.', PantherBrowser::class));
44-
}
35+
$class = $_SERVER['PANTHER_BROWSER_CLASS'] ?? PantherBrowser::class;
4536

46-
if (self::$primaryPantherClient) {
47-
return new $class(static::createAdditionalPantherClient());
48-
}
37+
if (!\is_a($class, PantherBrowser::class, true)) {
38+
throw new \RuntimeException(\sprintf('"PANTHER_BROWSER_CLASS" env variable must reference a class that extends %s.', PantherBrowser::class));
39+
}
4940

41+
if (self::$primaryPantherClient) {
42+
$browser = new $class(static::createAdditionalPantherClient());
43+
} else {
5044
self::$primaryPantherClient = static::createPantherClient(
5145
\array_merge(['browser' => $_SERVER['PANTHER_BROWSER'] ?? static::CHROME], $options),
5246
$kernelOptions,
5347
$managerOptions
5448
);
5549

56-
return new $class(self::$primaryPantherClient);
57-
});
50+
$browser = new $class(self::$primaryPantherClient);
51+
}
5852

5953
BrowserExtension::registerBrowser($browser);
6054

@@ -67,57 +61,50 @@ protected function pantherBrowser(array $options = [], array $kernelOptions = []
6761

6862
protected function httpBrowser(array $kernelOptions = [], array $pantherOptions = []): HttpBrowser
6963
{
70-
$browser = HttpBrowser::create(function() use ($kernelOptions, $pantherOptions) {
71-
$class = $_SERVER['HTTP_BROWSER_CLASS'] ?? HttpBrowser::class;
64+
$class = $_SERVER['HTTP_BROWSER_CLASS'] ?? HttpBrowser::class;
7265

73-
if (!\is_a($class, HttpBrowser::class, true)) {
74-
throw new \RuntimeException(\sprintf('"HTTP_BROWSER_CLASS" env variable must reference a class that extends %s.', HttpBrowser::class));
75-
}
76-
77-
$baseUri = $_SERVER['HTTP_BROWSER_URI'] ?? null;
66+
if (!\is_a($class, HttpBrowser::class, true)) {
67+
throw new \RuntimeException(\sprintf('"HTTP_BROWSER_CLASS" env variable must reference a class that extends %s.', HttpBrowser::class));
68+
}
7869

79-
if (!$baseUri && !$this instanceof PantherTestCase) {
80-
throw new \RuntimeException(\sprintf('If not using "HTTP_BROWSER_URI", your TestCase must extend "%s".', PantherTestCase::class));
81-
}
70+
$baseUri = $_SERVER['HTTP_BROWSER_URI'] ?? null;
8271

83-
if (!$baseUri) {
84-
self::startWebServer($pantherOptions);
72+
if (!$baseUri && !$this instanceof PantherTestCase) {
73+
throw new \RuntimeException(\sprintf('If not using "HTTP_BROWSER_URI", your TestCase must extend "%s".', PantherTestCase::class));
74+
}
8575

86-
$baseUri = self::$baseUri;
87-
}
76+
if (!$baseUri) {
77+
self::startWebServer($pantherOptions);
8878

89-
// copied from PantherTestCaseTrait::createHttpBrowserClient()
90-
$client = new HttpBrowserClient();
91-
$urlComponents = \parse_url($baseUri);
92-
$host = $urlComponents['host'];
79+
$baseUri = self::$baseUri;
80+
}
9381

94-
if (isset($urlComponents['port'])) {
95-
$host .= ":{$urlComponents['port']}";
96-
}
82+
// copied from PantherTestCaseTrait::createHttpBrowserClient()
83+
$client = new HttpBrowserClient();
84+
$urlComponents = \parse_url($baseUri);
85+
$host = $urlComponents['host'];
9786

98-
$client->setServerParameter('HTTP_HOST', $host);
87+
if (isset($urlComponents['port'])) {
88+
$host .= ":{$urlComponents['port']}";
89+
}
9990

100-
if ('https' === ($urlComponents['scheme'] ?? 'http')) {
101-
$client->setServerParameter('HTTPS', 'true');
102-
}
91+
$client->setServerParameter('HTTP_HOST', $host);
10392

104-
/** @var HttpBrowser $browser */
105-
$browser = new $class(self::$httpBrowserClients[] = $client);
93+
if ('https' === ($urlComponents['scheme'] ?? 'http')) {
94+
$client->setServerParameter('HTTPS', 'true');
95+
}
10696

107-
if (!$this instanceof KernelTestCase) {
108-
return $browser;
109-
}
97+
$browser = new $class(self::$httpBrowserClients[] = $client);
11098

99+
if ($this instanceof KernelTestCase) {
111100
if (!static::$booted) {
112101
static::bootKernel($kernelOptions);
113102
}
114103

115104
if (static::$container->has('profiler')) {
116105
$browser->setProfiler(static::$container->get('profiler'));
117106
}
118-
119-
return $browser;
120-
});
107+
}
121108

122109
BrowserExtension::registerBrowser($browser);
123110

@@ -132,28 +119,26 @@ protected function kernelBrowser(array $options = []): KernelBrowser
132119
throw new \RuntimeException(\sprintf('The "%s" method can only be used on TestCases that extend "%s".', __METHOD__, KernelTestCase::class));
133120
}
134121

135-
$browser = KernelBrowser::create(function() use ($options) {
136-
$class = $_SERVER['KERNEL_BROWSER_CLASS'] ?? KernelBrowser::class;
137-
138-
if (!\is_a($class, KernelBrowser::class, true)) {
139-
throw new \RuntimeException(\sprintf('"KERNEL_BROWSER_CLASS" env variable must reference a class that extends %s.', KernelBrowser::class));
140-
}
122+
$class = $_SERVER['KERNEL_BROWSER_CLASS'] ?? KernelBrowser::class;
141123

142-
if ($this instanceof WebTestCase) {
143-
static::ensureKernelShutdown();
124+
if (!\is_a($class, KernelBrowser::class, true)) {
125+
throw new \RuntimeException(\sprintf('"KERNEL_BROWSER_CLASS" env variable must reference a class that extends %s.', KernelBrowser::class));
126+
}
144127

145-
return new $class(static::createClient($options));
146-
}
128+
if ($this instanceof WebTestCase) {
129+
static::ensureKernelShutdown();
147130

131+
$browser = new $class(static::createClient($options));
132+
} else {
148133
// reboot kernel before starting browser
149134
static::bootKernel($options);
150135

151136
if (!static::$container->has('test.client')) {
152137
throw new \RuntimeException('The Symfony test client is not enabled.');
153138
}
154139

155-
return new $class(static::$container->get('test.client'));
156-
});
140+
$browser = new $class(static::$container->get('test.client'));
141+
}
157142

158143
BrowserExtension::registerBrowser($browser);
159144

0 commit comments

Comments
 (0)