Skip to content

Commit

Permalink
refactored assertCookie() to not require passing headers_list() exp…
Browse files Browse the repository at this point in the history
…licitly and moved to `\lithium\test\Unit`. Updated tests accordingly.
  • Loading branch information
daetal-us authored and nateabele committed Apr 12, 2010
1 parent 0f741be commit 2c6d422
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 96 deletions.
63 changes: 63 additions & 0 deletions libraries/lithium/test/Unit.php
Expand Up @@ -343,9 +343,12 @@ public function assertPattern($expected, $result, $message = '{:message}') {
*
* Checks for an input tag with a name attribute (contains any non-empty value) and an id
* attribute that contains 'my-input':
* {{{
* array('input' => array('name', 'id' => 'my-input'))
* }}}
*
* Checks for two p elements with some text in them:
* {{{
* array(
* array('p' => true),
* 'textA',
Expand All @@ -354,13 +357,16 @@ public function assertPattern($expected, $result, $message = '{:message}') {
* 'textB',
* '/p'
* )
* }}}
*
* You can also specify a pattern expression as part of the attribute values, or the tag
* being defined, if you prepend the value with preg: and enclose it with slashes, like so:
* {{{
* array(
* array('input' => array('name', 'id' => 'preg:/FieldName\d+/')),
* 'preg:/My\s+field/'
* )
* }}}
*
* Important: This function is very forgiving about whitespace and also accepts any
* permutation of attribute order. It will also allow whitespaces between specified tags.
Expand Down Expand Up @@ -491,6 +497,63 @@ function assertTags($string, $expected, $fullDebug = false) {
return $this->assert(true);
}

/**
* Assert Cookie data is properly set in headers.
*
* The value passed to `exepected` is an array of the cookie data, with at least the key and
* value expected, but can support any of the following keys:
* - `key`: the expected key
* - `value`: the expected value
* - `path`: optionally specifiy a path
* - `name`: optionally specify the cookie name
* - `expires`: optionally assert a specific expire time
*
* @param array $expected
* @param array $headers When empty, value of `headers_list()` is used.
*/
public function assertCookie($expected, $headers = null) {
$defaults = array('path' => '/', 'name' => 'app');
$expected += $defaults;

$result = $headers;
if (empty($result)) {
$result = headers_list();
}

$value = preg_quote(urlencode($expected['value']), '/');

$key = explode('.', $expected['key']);
$key = (count($key) == 1) ? '[' . current($key) . ']' : ('[' . join('][', $key) . ']');
$key = preg_quote($key, '/');

if (isset($expected['expires'])) {
$date = gmdate('D, d-M-Y H:i:s \G\M\T', strtotime($expected['expires']));
$expires = preg_quote($date, '/');
} else {
$expires = '(?:.+?)';
}
$path = preg_quote($expected['path'], '/');
$pattern = "/^Set\-Cookie:\s{$expected['name']}$key=$value;";
$pattern .= "\sexpires=$expires;\spath=$path/";
$match = false;

foreach ($result as $header) {
if (preg_match($pattern, $header)) {
$match = true;
continue;
}
}

if (!$match) {
$this->assert(false,
sprintf('{:message} - Cookie %s not found in headers.', $pattern),
compact('expected', 'result')
);
return false;
}
return $this->assert(true, '%s');
}

/**
* Used before a call to `assert*()` if you expect the test assertion to generate an exception
* or PHP error. If no error or exception is thrown, a test failure will be reported. Can
Expand Down
Expand Up @@ -23,59 +23,6 @@ public function skip() {
$this->skipIf($sapi === 'cli', $message);
}

public function tearDown() {
$this->_destroySession();
}

protected function _destroySession($name = null) {
if (!$name) {
$name = session_name();
}
$settings = session_get_cookie_params();
setcookie(
$name, '', time() - 1000, $settings['path'], $settings['domain'],
$settings['secure'], $settings['httponly']
);
if (session_id()) {
session_destroy();
}
$_COOKIE = array();
}

public function assertCookie($expected, $headers) {
$defaults = array('path' => '/', 'name' => 'li3');
$expected += $defaults;
$value = preg_quote(urlencode($expected['value']), '/');

$key = explode('.', $expected['key']);
$key = (count($key) == 1) ? '[' . current($key) . ']' : ('[' . join('][', $key) . ']');
$key = preg_quote($key, '/');

if (isset($expected['expires'])) {
$date = gmdate('D, d-M-Y H:i:s \G\M\T', strtotime($expected['expires']));
$expires = preg_quote($date, '/');
} else {
$expires = '(?:.+?)';
}
$path = preg_quote($expected['path'], '/');
$pattern = "/^Set\-Cookie:\s{$expected['name']}$key=$value;";
$pattern .= "\sexpires=$expires;\spath=$path/";
$match = false;

foreach ($headers as $header) {
if (preg_match($pattern, $header)) {
$match = true;
continue;
}
}

if (!$match) {
$this->assert(false, sprintf('{:message} - Cookie %s not found in headers.', $pattern));
return false;
}
return $this->assert(true, '%s');
}

public function setUp() {
$this->Cookie = new Cookie();
}
Expand Down Expand Up @@ -104,7 +51,7 @@ public function testWriteDefaultParameters() {
$params = compact('key', 'value');
$result = $closure($this->Cookie, $params, null);

$this->assertCookie(compact('key', 'value', 'expires', 'path'), headers_list());
$this->assertCookie(compact('key', 'value', 'expires', 'path'));
}

public function testWriteArrayData() {
Expand Down Expand Up @@ -161,7 +108,7 @@ public function testWriteCustomParameters() {
$params = compact('key', 'value', 'options');
$result = $closure($this->Cookie, $params, null);

$this->assertCookie(compact('key', 'value', 'expires', 'path'), headers_list());
$this->assertCookie(compact('key', 'value', 'expires', 'path'));
}

public function testRead() {
Expand Down Expand Up @@ -221,7 +168,7 @@ public function testDeleteNonExistentValue() {
$result = $closure($this->Cookie, $params, null);
$this->assertNull($result);

$this->assertCookie(compact('key', 'value', 'path'), headers_list());
$this->assertCookie(compact('key', 'value', 'path'));
}
}

Expand Down
46 changes: 6 additions & 40 deletions libraries/lithium/tests/integration/storage/SessionTest.php
Expand Up @@ -30,40 +30,6 @@ public function tearDown() {
}
}

public function assertCookie($expected, $headers) {
$defaults = array('path' => '/', 'name' => 'li3');
$expected += $defaults;
$value = preg_quote(urlencode($expected['value']), '/');

$key = explode('.', $expected['key']);
$key = (count($key) == 1) ? '[' . current($key) . ']' : ('[' . join('][', $key) . ']');
$key = preg_quote($key, '/');

if (isset($expected['expires'])) {
$date = gmdate('D, d-M-Y H:i:s \G\M\T', strtotime($expected['expires']));
$expires = preg_quote($date, '/');
} else {
$expires = '(?:.+?)';
}
$path = preg_quote($expected['path'], '/');
$pattern = "/^Set\-Cookie:\s{$expected['name']}$key=$value;";
$pattern .= "\sexpires=$expires;\spath=$path/";
$match = false;

foreach ($headers as $header) {
if (preg_match($pattern, $header)) {
$match = true;
continue;
}
}

if (!$match) {
$this->assert(false, sprintf('{:message} - Cookie %s not found in headers.', $pattern));
return false;
}
return $this->assert(true, '%s');
}

public function testWriteReadDelete() {
Session::config(array(
'test' => array('adapter' => 'Php')
Expand Down Expand Up @@ -96,13 +62,13 @@ public function testCookieWriteReadDelete() {
Session::write('testkey3', 'value3', array('name' => 'li3'));

$this->assertCookie(
array('key' => 'testkey1', 'value' => 'value1'), headers_list()
array('key' => 'testkey1', 'value' => 'value1')
);
$this->assertCookie(
array('key' => 'testkey2', 'value' => 'value2'), headers_list()
array('key' => 'testkey2', 'value' => 'value2')
);
$this->assertCookie(
array('key' => 'testkey3', 'value' => 'value3'), headers_list()
array('key' => 'testkey3', 'value' => 'value3')
);

Session::delete('testkey1', array('name' => 'li3'));
Expand All @@ -112,13 +78,13 @@ public function testCookieWriteReadDelete() {
$params = array('exires' => '-1 second', 'path' => '/');

$this->assertCookie(
array('key' => 'testkey1', 'value' => 'deleted'), headers_list()
array('key' => 'testkey1', 'value' => 'deleted')
);
$this->assertCookie(
array('key' => 'testkey2', 'value' => 'deleted'), headers_list()
array('key' => 'testkey2', 'value' => 'deleted')
);
$this->assertCookie(
array('key' => 'testkey3', 'value' => 'deleted'), headers_list()
array('key' => 'testkey3', 'value' => 'deleted')
);
}

Expand Down

0 comments on commit 2c6d422

Please sign in to comment.