forked from symfony/panther
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PanthereTestCase.php
125 lines (103 loc) · 3 KB
/
PanthereTestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/*
* This file is part of the Panthère project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Panthere;
use Goutte\Client as GoutteClient;
use Panthere\Client as PanthereClient;
use Panthere\ProcessManager\WebServerManager;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
if (\class_exists(WebTestCase::class)) {
/**
* @internal
*/
abstract class InternalTestCase extends WebTestCase
{
}
} else {
/**
* @internal
*/
abstract class InternalTestCase extends TestCase
{
}
}
/**
* @author Kévin Dunglas <dunglas@gmail.com>
*/
abstract class PanthereTestCase extends InternalTestCase
{
/**
* @var string|null
*/
protected static $webServerDir;
/**
* @var WebServerManager|null
*/
protected static $webServerManager;
/**
* @var string|null
*/
protected static $baseUri;
/**
* @var GoutteClient|null
*/
protected static $goutteClient;
/**
* @var PanthereClient|null
*/
protected static $panthereClient;
public static function tearDownAfterClass()
{
if (null !== self::$webServerManager) {
self::$webServerManager->quit();
self::$webServerManager = null;
}
if (null !== self::$panthereClient) {
self::$panthereClient->quit();
self::$panthereClient = null;
}
if (null !== self::$goutteClient) {
self::$goutteClient = null;
}
}
protected static function startWebServer(?string $webServerDir = null): void
{
if (null !== static::$webServerManager) {
return;
}
if (null === $webServerDir) {
// Try the local $webServerDir property, or the PANTHERE_WEB_SERVER_DIR env var or default to the Flex directory structure
$webServerDir = static::$webServerDir ?? $_ENV['PANTHERE_WEB_SERVER_DIR'] ?? __DIR__.'/../../../../public';
}
self::$webServerManager = new WebServerManager($webServerDir, '127.0.0.1', 9000);
self::$webServerManager->start();
self::$baseUri = 'http://127.0.0.1:9000';
}
protected static function createPanthereClient(): PanthereClient
{
self::startWebServer();
if (null === self::$panthereClient) {
self::$panthereClient = Client::createChromeClient();
}
return self::$panthereClient;
}
protected static function createGoutteClient(): GoutteClient
{
if (!\class_exists(GoutteClient::class)) {
throw new \RuntimeException('Goutte is not installed. Run "composer req fabpot/goutte".');
}
self::startWebServer();
if (null === self::$goutteClient) {
self::$goutteClient = new GoutteClient();
}
return self::$goutteClient;
}
}