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

HasPantherBrowser::pantherBrowser()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 18
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 30
rs 9.6666
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 (!$this instanceof PantherTestCase) {
27
            throw new \RuntimeException(\sprintf('The "%s" method can only be used on TestCases that extend "%s".', __METHOD__, PantherTestCase::class));
28
        }
29
30
        if (!\is_a($class, PantherBrowser::class, true)) {
31
            throw new \RuntimeException(\sprintf('"PANTHER_BROWSER_CLASS" env variable must reference a class that extends %s.', PantherBrowser::class));
32
        }
33
34
        if (self::$primaryPantherClient) {
35
            $browser = new $class(static::createAdditionalPantherClient());
36
        } else {
37
            self::$primaryPantherClient = static::createPantherClient(
38
                \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...
39
                $kernelOptions,
40
                $managerOptions
41
            );
42
43
            $browser = new $class(self::$primaryPantherClient);
44
        }
45
46
        BrowserExtension::registerBrowser($browser);
47
48
        return $browser
49
            ->setSourceDir($_SERVER['BROWSER_SOURCE_DIR'] ?? './var/browser/source')
50
            ->setScreenshotDir($_SERVER['BROWSER_SCREENSHOT_DIR'] ?? './var/browser/screenshots')
51
            ->setConsoleLogDir($_SERVER['BROWSER_CONSOLE_LOG_DIR'] ?? './var/browser/console-logs')
52
        ;
53
    }
54
}
55