Repository navigation
Expand file tree
/
Copy pathPhpBrowser.php
More file actions
148 lines (130 loc) · 4.1 KB
/
Copy pathPhpBrowser.php
File metadata and controls
148 lines (130 loc) · 4.1 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
namespace Codeception\Module;
use Codeception\Lib\Connector\Goutte;
use Codeception\Lib\InnerBrowser;
use Codeception\Lib\Interfaces\MultiSession;
use Codeception\Lib\Interfaces\Remote;
use Guzzle\Http\Client;
use Codeception\Exception\TestRuntime;
use Codeception\TestCase;
use Symfony\Component\BrowserKit\Request;
/**
* Uses [Goutte](https://github.com/fabpot/Goutte) and [Guzzle](http://guzzlephp.org/) to interact with your application over CURL.
* Module works over CURL and requires **PHP CURL extension** to be enabled.
*
* Use to perform web acceptance tests with non-javascript browser.
*
* If test fails stores last shown page in 'output' dir.
*
* ## Status
*
* * Maintainer: **davert**
* * Stability: **stable**
* * Contact: davert.codecept@mailican.com
* * relies on [Goutte](https://github.com/fabpot/Goutte) and [Guzzle](http://guzzlephp.org/)
*
* *Please review the code of non-stable modules and provide patches if you have issues.*
*
* ## Configuration
*
* * url *required* - start url of your app
* * curl - curl options
*
* ### Example (`acceptance.suite.yml`)
*
* modules:
* enabled: [PhpBrowser]
* config:
* PhpBrowser:
* url: 'http://localhost'
* curl:
* CURLOPT_RETURNTRANSFER: true
*
* ## Public Properties
*
* * session - contains Mink Session
* * guzzle - contains [Guzzle](http://guzzlephp.org/) client instance: `\Guzzle\Http\Client`
*
* All SSL certification checks are disabled by default.
* To configure CURL options use `curl` config parameter.
*
*/
class PhpBrowser extends InnerBrowser implements Remote, MultiSession
{
protected $requiredFields = array('url');
protected $config = array('curl' => array());
protected $curl_defaults = array(
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_CERTINFO => false,
);
/**
* @var \Codeception\Lib\Connector\Goutte
*/
public $client;
/**
* @var \Guzzle\Http\Client
*/
public $guzzle;
public function _initialize()
{
// build up a Guzzle friendly list of configuration options
// passed in both from our defaults and the respective
// yaml configuration file (if applicable)
$curl_config['curl.options'] = $this->curl_defaults;
foreach ($this->config['curl'] as $key => $val) {
if (defined($key)) $curl_config['curl.options'][constant($key)] = $val;
}
// Guzzle client requires that we set the ssl.certificate_authority config
// directive if we wish to disable SSL verification
if ($curl_config['curl.options'][CURLOPT_SSL_VERIFYPEER] !== true) {
$curl_config['ssl.certificate_authority'] = false;
}
$this->guzzle = new Client('', $curl_config);
}
public function _before(\Codeception\TestCase $test) {
$this->_initializeSession();
}
public function _getUrl()
{
return $this->config['url'];
}
public function setHeader($header, $value)
{
$this->client->setHeader($header, $value);
}
public function amOnSubdomain($subdomain)
{
$url = $this->config['url'];
$url = preg_replace('~(https?:\/\/)(.*\.)(.*\.)~', "$1$3", $url); // removing current subdomain
$url = preg_replace('~(https?:\/\/)(.*)~', "$1$subdomain.$2", $url); // inserting new
$this->_reconfigure(array('url' => $url));
}
public function _getResponseCode()
{
return $this->getResponseStatusCode();
}
public function _initializeSession()
{
$this->client = new Goutte();
$this->client->setClient($this->guzzle);
$this->client->setBaseUri($this->config['url']);
}
public function _backupSessionData()
{
return [
'client' => $this->client,
'guzzle' => $this->guzzle,
'crawler' => $this->crawler
];
}
public function _loadSessionData($data)
{
foreach ($data as $key => $val) {
$this->$key = $val;
}
}
public function _closeSession($data)
{
unset($data);
}
}