Ben bir kombinasyonu yoluyla, seans dayanır web siteme bir parçası davranışını test etmek için PHPUnit kullanabilirsiniz bulundu
Curl ve çerezi oturum kimliği oturumunu geçen bir tanımlama bilgisi.
Aşağıdaki Curl
sınıfı, bir oturum parametresi geçirmek için CURLOPT_COOKIE
seçeneğini kullanır. $sessionid
statik değişkeni, oturumu farklı Curl çağrıları arasında kaydeder. Ayrıca, oturumlar changeSession
statik işlevi kullanılarak değiştirilebilir. Özellikle Curl::changeSession()
denir sürece
class Curl {
private $ch;
private static $sessionid;
public function __construct($url, $options) {
$this->ch = curl_init($url);
if (!self::$sessionid)
self::$sessionid = .. generateRandomString() ..;
$options = $options + array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_COOKIE => 'PHPSESSID=' . self::$sessionid);
foreach ($options as $key => $val) {
curl_setopt($this->ch, $key, $val);
}
}
public function getResponse() {
if ($this->response) {
return $this->response;
}
$response = curl_exec($this->ch);
$error = curl_error($this->ch);
$errno = curl_errno($this->ch);
$header_size = curl_getinfo($this->ch, CURLINFO_HEADER_SIZE);
$this->header = substr($response, 0, $header_size);
$response = substr($response, $header_size);
if (is_resource($this->ch)) {
curl_close($this->ch);
}
if (0 !== $errno) {
throw new \RuntimeException($error, $errno);
}
return $this->response = $response;
}
public function __toString() {
return $this->getResponse();
}
public static function changeSession() {
self::$SESSIONID = Practicalia::generateRandomString();
}
}
bir örnek çağrı
$data = array(
'action' => 'someaction',
'info' => 'someinfo'
);
$curl = new Curl(
'http://localhost/somephp.php',
array(
CURLOPT_POSTFIELDS => http_build_query($data)));
$response = $curl->getResponse();
Ve sonradan Bukle çağrıları otomatik olarak bir önceki ile aynı oturumu kullanacaktır.
Hacky üzerinde ayrıntı yapabilir misiniz? Testin 'setUp'ında sadece unset ($ _ COOKIE) var mı? –
Testlerimde bununla ilgilenmek istemiyorum. Gereksiz dağınıklığı hissediyorum ve otomatik olarak ele alınmalıdır. –