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

HasHttpBrowser::_resetHttpBrowserClients()   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\Bundle\FrameworkBundle\Test\KernelTestCase;
6
use Symfony\Component\BrowserKit\HttpBrowser as HttpBrowserClient;
7
use Symfony\Component\Panther\PantherTestCase;
8
use Zenstruck\Browser\HttpBrowser;
9
10
trait HasHttpBrowser
11
{
12
    /** @var HttpBrowserClient[] */
13
    private static array $httpBrowserClients = [];
14
15
    /**
16
     * @internal
17
     * @after
18
     */
19
    final public function _resetHttpBrowserClients(): void
20
    {
21
        self::$httpBrowserClients = [];
22
    }
23
24
    protected function browser(): HttpBrowser
25
    {
26
        $browser = HttpBrowser::create(function() {
27
            $class = $_SERVER['HTTP_BROWSER_CLASS'] ?? HttpBrowser::class;
28
29
            if (!\is_a($class, HttpBrowser::class, true)) {
30
                throw new \RuntimeException(\sprintf('"HTTP_BROWSER_CLASS" env variable must reference a class that extends %s.', HttpBrowser::class));
31
            }
32
33
            $baseUri = $_SERVER['HTTP_BROWSER_URI'] ?? null;
34
35
            if (!$baseUri && !$this instanceof PantherTestCase) {
36
                throw new \RuntimeException(\sprintf('If not using "HTTP_BROWSER_URI", your TestCase must extend "%s".', PantherTestCase::class));
37
            }
38
39
            if (!$baseUri) {
40
                self::startWebServer();
41
42
                $baseUri = self::$baseUri;
43
            }
44
45
            // copied from PantherTestCaseTrait::createHttpBrowserClient()
46
            $client = new HttpBrowserClient();
47
            $urlComponents = \parse_url($baseUri);
48
            $host = $urlComponents['host'];
49
50
            if (isset($urlComponents['port'])) {
51
                $host .= ":{$urlComponents['port']}";
52
            }
53
54
            $client->setServerParameter('HTTP_HOST', $host);
55
56
            if ('https' === ($urlComponents['scheme'] ?? 'http')) {
57
                $client->setServerParameter('HTTPS', 'true');
58
            }
59
60
            /** @var HttpBrowser $browser */
61
            $browser = new $class(self::$httpBrowserClients[] = $client);
62
63
            if (!$this instanceof KernelTestCase) {
64
                return $browser;
65
            }
66
67
            if (!static::$booted) {
0 ignored issues
show
Bug introduced by
The property booted is declared protected in Symfony\Bundle\FrameworkBundle\Test\KernelTestCase and cannot be accessed from this context.
Loading history...
68
                static::bootKernel();
69
            }
70
71
            if (static::$container->has('profiler')) {
0 ignored issues
show
Bug introduced by
The property container is declared protected in Symfony\Bundle\FrameworkBundle\Test\KernelTestCase and cannot be accessed from this context.
Loading history...
72
                $browser->setProfiler(static::$container->get('profiler'));
0 ignored issues
show
Bug introduced by
It seems like static::container->get('profiler') can also be of type null; however, parameter $profiler of Zenstruck\Browser\HttpBrowser::setProfiler() does only seem to accept Symfony\Component\HttpKernel\Profiler\Profiler, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

72
                $browser->setProfiler(/** @scrutinizer ignore-type */ static::$container->get('profiler'));
Loading history...
73
            }
74
75
            return $browser;
76
        });
77
78
        BrowserExtension::registerBrowser($browser);
79
80
        return $browser
81
            ->setSourceDir($_SERVER['BROWSER_SOURCE_DIR'] ?? './var/browser/source')
82
        ;
83
    }
84
}
85