Passed
Pull Request — 1.x (#23)
by Wouter
02:16
created

HasPantherBrowser::pantherBrowser()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 16
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 26
rs 9.7333
1
<?php
2
3
namespace Zenstruck\Browser\Test;
4
5
use Symfony\Component\Panther\Client as PantherClient;
6
use Symfony\Component\Panther\PantherTestCase;
7
use Zenstruck\Browser\PantherBrowser;
8
9
trait HasPantherBrowser
10
{
11
    private static ?PantherClient $primaryPantherClient = null;
12
13
    /**
14
     * @internal
15
     * @after
16
     */
17
    final public static function _resetPantherBrowserClient(): void
18
    {
19
        self::$primaryPantherClient = null;
20
    }
21
22
    protected function pantherBrowser(array $options = [], array $kernelOptions = [], array $managerOptions = []): PantherBrowser
23
    {
24
        $class = $_SERVER['PANTHER_BROWSER_CLASS'] ?? PantherBrowser::class;
25
26
        if (!\is_a($class, PantherBrowser::class, true)) {
27
            throw new \RuntimeException(\sprintf('"PANTHER_BROWSER_CLASS" env variable must reference a class that extends %s.', PantherBrowser::class));
28
        }
29
30
        if (self::$primaryPantherClient) {
31
            $browser = new $class(static::createAdditionalPantherClient());
32
        } else {
33
            self::$primaryPantherClient = static::createPantherClient(
34
                \array_merge(['browser' => $_SERVER['PANTHER_BROWSER'] ?? static::CHROME], $options),
0 ignored issues
show
Bug introduced by
The constant Zenstruck\Browser\Test\HasPantherBrowser::CHROME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
35
                $kernelOptions,
36
                $managerOptions
37
            );
38
39
            $browser = new $class(self::$primaryPantherClient);
40
        }
41
42
        BrowserExtension::registerBrowser($browser);
43
44
        return $browser
45
            ->setSourceDir($_SERVER['BROWSER_SOURCE_DIR'] ?? './var/browser/source')
46
            ->setScreenshotDir($_SERVER['BROWSER_SCREENSHOT_DIR'] ?? './var/browser/screenshots')
47
            ->setConsoleLogDir($_SERVER['BROWSER_CONSOLE_LOG_DIR'] ?? './var/browser/console-logs')
48
        ;
49
    }
50
}
51