Skip to content

Instantly share code, notes, and snippets.

@fprochazka
Last active August 31, 2017 20:23
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fprochazka/5299296 to your computer and use it in GitHub Desktop.
Save fprochazka/5299296 to your computer and use it in GitHub Desktop.
faking nette session in tests
<?php
/**
* Třída existuje, aby se vůbec neukládala session, tam kde není potřeba.
* Například v API, nebo v Cronu se různě sahá na session, i když se reálně mezi requesty nepřenáší.
*
* @internal
*/
class ArraySessionStorage extends Nette\Object implements Nette\Http\ISessionStorage
{
/**
* @var array
*/
private $session;
public function open($savePath, $sessionName)
{
$this->session = array();
}
public function close()
{
$this->session = array();
}
public function read($id)
{
return isset($this->session[$id]) ? $this->session[$id] : NULL;
}
public function write($id, $data)
{
$this->session[$id] = $data;
}
public function remove($id)
{
unset($this->session[$id]);
}
public function clean($maxlifetime)
{
}
}
<?php
/**
* This file is part of the Kdyby (http://www.kdyby.org)
*
* Copyright (c) 2008, 2012 Filip Procházka (filip@prochazka.su)
*
* For the full copyright and license information, please view the file license.md that was distributed with this source code.
*/
namespace Kdyby\Tests\Http;
use Kdyby;
use Nette;
/**
* @author Filip Procházka <filip@prochazka.su>
*/
class FakeSession extends Nette\Http\Session
{
/** @var \Kdyby\Tests\Http\FakeSessionSection[] */
private $sections = array();
/** @var bool */
private $started = FALSE;
/** @var array */
private $options = array();
/** @var string */
private $id;
/** @var string */
private $name = 'session_id';
/**
* @param \Nette\Http\IRequest $request
* @param \Nette\Http\IResponse $response
*/
public function __construct(Nette\Http\IRequest $request, Nette\Http\IResponse $response)
{
$this->regenerateId();
}
/**
*/
public function start()
{
$this->started = TRUE;
}
/**
* @return bool
*/
public function isStarted()
{
return $this->started;
}
/**
*/
public function close()
{
$this->started = NULL;
}
/**
*/
public function destroy()
{
$this->sections = array();
$this->close();
}
/**
* @return bool
*/
public function exists()
{
return TRUE;
}
/**
*
*/
public function regenerateId()
{
$this->id = md5((string)microtime(TRUE));
}
/**
* @return string
*/
public function getId()
{
return $this->id;
}
/**
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string $section
* @param string $class
*
* @return \Kdyby\Tests\Http\FakeSessionSection
*/
public function getSection($section, $class = 'Kdyby\Tests\Http\FakeSessionSection')
{
return $this->sections[$section] = new $class($this, $section);
}
/**
* @deprecated
* @param $section
* @throws \Kdyby\NotSupportedException
*/
public function getNamespace($section)
{
throw new Kdyby\NotSupportedException;
}
/**
* @param string $section
*
* @return bool
*/
public function hasSection($section)
{
return isset($this->sections[$section]);
}
/**
* @return \ArrayIterator
*/
public function getIterator()
{
return new \ArrayIterator($this->sections);
}
/**
*/
public function clean()
{
}
/**
* @param array $options
*/
public function setOptions(array $options)
{
$this->options = $options + $this->options;
}
/**
* @return array
*/
public function getOptions()
{
return $this->options;
}
/**
* @param int|string $time
*/
public function setExpiration($time)
{
}
/**
* @return array
*/
public function getCookieParameters()
{
$keys = array('cookie_path', 'cookie_domain', 'cookie_secure');
$empty = array_fill_keys($keys, NULL);
return array_intersect_key($this->options, $empty) + $empty;
}
/**
* @param \Nette\Http\ISessionStorage $storage
*/
public function setStorage(Nette\Http\ISessionStorage $storage)
{
}
}
<?php
/**
* This file is part of the Kdyby (http://www.kdyby.org)
*
* Copyright (c) 2008, 2012 Filip Procházka (filip@prochazka.su)
*
* For the full copyright and license information, please view the file license.md that was distributed with this source code.
*/
namespace Kdyby\Tests\Http;
use Kdyby;
use Nette;
/**
* @author Filip Procházka <filip@prochazka.su>
*/
class FakeSessionSection extends Nette\Object implements \IteratorAggregate, \ArrayAccess
{
/** @var \Nette\Http\Session */
private $session;
/** @var string */
private $name;
/** @var array */
private $data = array();
/** @var array */
private $meta = array();
/** @var bool */
public $warnOnUndefined = FALSE;
/**
* @param \Nette\Http\Session $session
* @param string $name
*/
public function __construct(Nette\Http\Session $session, $name)
{
if (!is_string($name)) {
throw new Nette\InvalidArgumentException("Session namespace must be a string, " . gettype($name) . " given.");
}
$this->session = $session;
$this->name = $name;
}
/**
* @return \ArrayIterator
*/
public function getIterator()
{
return new \ArrayIterator($this->data);
}
/**
* @param string $name
* @param mixed $value
*/
public function __set($name, $value)
{
$this->data[$name] = $value;
}
/**
* @param string $name
*
* @return mixed
*/
public function &__get($name)
{
if ($this->warnOnUndefined && !array_key_exists($name, $this->data)) {
trigger_error("The variable '$name' does not exist in session section", E_USER_NOTICE);
}
return $this->data[$name];
}
/**
* @param string $name
*
* @return bool
*/
public function __isset($name)
{
return isset($this->data[$name]);
}
/**
* @param string $name
*
* @return void
*/
public function __unset($name)
{
unset($this->data[$name], $this->meta[$name]);
}
/**
* @param string $name
* @param mixed $value
*/
public function offsetSet($name, $value)
{
$this->__set($name, $value);
}
/**
* @param string $name
*
* @return mixed
*/
public function offsetGet($name)
{
return $this->__get($name);
}
/**
* @param string $name
*
* @return bool
*/
public function offsetExists($name)
{
return $this->__isset($name);
}
/**
* @param string $name
*/
public function offsetUnset($name)
{
$this->__unset($name);
}
/**
* @param int|string $time
* @param array $variables
*
* @return \Kdyby\Tests\Http\FakeSessionSection
*/
public function setExpiration($time, $variables = NULL)
{
return $this;
}
/**
* @param array $variables
*/
public function removeExpiration($variables = NULL)
{
}
/**
* Cancels the current session section.
* @return void
*/
public function remove()
{
$this->data = NULL;
$this->meta = NULL;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment