phpunit - Guzzle php how to set (mock) session variable -
in guzzle can use cookiejar persist session. how create session variable? phpunit guzzle code
use guzzle\http\client; use guzzle\plugin\cookie\cookieplugin; use guzzle\plugin\cookie\cookiejar\arraycookiejar; $cookieplugin = new cookieplugin(new arraycookiejar()); $client = new client('http://somewhere.com/'); $client->addsubscriber($cookieplugin); //i want set session variable here // $_session['foo'] = 'bar'; $client->get('http://somewhere.com/test.php')->send(); $request = $client->get('http://somewhere.com/'); $request->send();
and test.php file on server
session_start(); error_log(print_r($_session, true));
it essence of session variables not accessible outside world , cannot influenced client (in case: guzzle). way influence session send session cookie or not.
so if require tests set session variable, , production code on server not allow client set value directly, you'd have provide test method this. note security implications of doing so, in case code ever escapes production.
you can take shortcuts. if test code running on same machine server code, you'd able pre-define session id client, save data it, session_write_close()
, use session id cookie value request. it's supposed write session data file , read there. if have access different session storage directly, use this. these methods won't affect security.
if else fails, create file allows 2 parameters: session key , value. if posted, script enter them $_session.
Comments
Post a Comment