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

HasPantherBrowser::_resetPantherBrowserClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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 browser(): PantherBrowser
23
    {
24
        $browser = PantherBrowser::create(function() {
25
            if (!$this instanceof PantherTestCase) {
26
                throw new \RuntimeException(\sprintf('The "%s" method can only be used on TestCases that extend "%s".', __METHOD__, PantherTestCase::class));
27
            }
28
29
            $class = $_SERVER['PANTHER_BROWSER_CLASS'] ?? PantherBrowser::class;
30
31
            if (!\is_a($class, PantherBrowser::class, true)) {
32
                throw new \RuntimeException(\sprintf('"PANTHER_BROWSER_CLASS" env variable must reference a class that extends %s.', PantherBrowser::class));
33
            }
34
35
            if (self::$primaryPantherClient) {
36
                return new $class(static::createAdditionalPantherClient());
37
            }
38
39
            self::$primaryPantherClient = static::createPantherClient(
40
                ['browser' => $_SERVER['PANTHER_BROWSER'] ?? static::CHROME]
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...
41
            );
42
43
            return 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