Changeset 10830
- Timestamp:
- 08/13/08 09:18:50 (11 months ago)
- Files:
-
- branches/1.2/UPGRADE_TO_1_2 (modified) (1 diff)
- branches/1.2/lib/test/sfTestBrowser.class.php (modified) (3 diffs)
- branches/1.2/lib/util/sfBrowser.class.php (modified) (3 diffs)
- branches/1.2/test/functional/sfTestBrowserTest.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/1.2/UPGRADE_TO_1_2
r10827 r10830 62 62 // symfony 1.2 only 63 63 $v = new sfValidatorSchemaCompare('left', '==', 'right'); 64 65 Tests 66 ----- 67 68 Cookies are now extensively supported in the `sfBrowser` and `sfTestBrowser` classes. 69 70 You can manage cookies between requests by using the `setCookie()`, `removeCookie()`, and `clearCookies()` methods of the `sfBrowser` class: 71 72 [php] 73 $b-> 74 setCookie('foo', 'bar')-> 75 removeCookie('bar')-> 76 get('/')-> 77 // ... 78 79 clearCookies()-> 80 get('/')-> 81 // ... 82 83 You can also test cookies with the `hasCookie()` and `isCookie()` methods of the `sfTestBrowser` class: 84 85 [php] 86 $b-> 87 get('/')-> 88 hasCookie('foo')-> 89 hasCookie('foobar', false)-> 90 isCookie('foo', 'bar')-> 91 isCookie('foo', '/a/')-> 92 isCookie('foo', '/!z/')-> 93 94 The `hasCookie()` method takes a Boolean as its second argument to be able to test the fact that a cookie is not set. 95 96 The second argument of the `isCookie()` method behaves as the second argument of the `checkElementResponse` method. It can be a string, a regular expression, or a negative regular expression. 97 98 The browser class also automatically expires cookies as per the `expire` value of each cookie. branches/1.2/lib/test/sfTestBrowser.class.php
r9253 r10830 381 381 else if (null !== $message) 382 382 { 383 $this->test()->is($e->getMessage(), $message, sprintf('response exception message matches regex"%s"', $message));383 $this->test()->is($e->getMessage(), $message, sprintf('response exception message is "%s"', $message)); 384 384 } 385 385 } … … 391 391 392 392 /** 393 * Trigger a test failure if an uncaught exception is present.393 * Triggers a test failure if an uncaught exception is present. 394 394 * 395 395 * @return bool … … 401 401 $this->test()->fail(sprintf('last request threw an uncaught exception "%s: %s"', get_class($this->getCurrentException()), $this->getCurrentException()->getMessage())); 402 402 } 403 403 404 404 return $empty; 405 } 406 407 /** 408 * Checks if a cookie exists. 409 * 410 * @param string $name The cookie name 411 * @param Boolean $exists Whether the cookie must exist or not 412 * 413 * @return sfTestBrowser The current sfTestBrowser instance 414 */ 415 public function hasCookie($name, $exists = true) 416 { 417 if (!array_key_exists($name, $_COOKIE)) 418 { 419 if ($exists) 420 { 421 $this->test()->fail(sprintf('cookie "%s" exist.', $name)); 422 } 423 else 424 { 425 $this->test()->pass(sprintf('cookie "%s" does not exist.', $name)); 426 } 427 428 return $this; 429 } 430 431 if ($exists) 432 { 433 $this->test()->pass(sprintf('cookie "%s" exists.', $name)); 434 } 435 else 436 { 437 $this->test()->fail(sprintf('cookie "%s" does not exist.', $name)); 438 } 439 440 return $this; 441 } 442 443 /** 444 * Checks the value of a cookie. 445 * 446 * @param string $name The cookie name 447 * @param mixed $value The expected value 448 * 449 * @return sfTestBrowser The current sfTestBrowser instance 450 */ 451 public function isCookie($name, $value) 452 { 453 if (!array_key_exists($name, $_COOKIE)) 454 { 455 $this->test()->fail(sprintf('cookie "%s" does not exist.', $name)); 456 457 return $this; 458 } 459 460 if (preg_match('/^(!)?([^a-zA-Z0-9\\\\]).+?\\2[ims]?$/', $value, $match)) 461 { 462 if ($match[1] == '!') 463 { 464 $this->test()->unlike($_COOKIE[$name], substr($value, 1), sprintf('cookie "%s" content does not match regex "%s"', $name, $value)); 465 } 466 else 467 { 468 $this->test()->like($_COOKIE[$name], $value, sprintf('cookie "%s" content matches regex "%s"', $name, $value)); 469 } 470 } 471 else if (null !== $message) 472 { 473 $this->test()->is($_COOKIE[$name], $value, sprintf('cookie "%s" content is ok', $name)); 474 } 475 476 return $this; 405 477 } 406 478 branches/1.2/lib/util/sfBrowser.class.php
r10739 r10830 107 107 108 108 /** 109 * Sets a cookie. 110 * 111 * @param string $name The cookie name 112 * @param string $name HTTP header name 113 * @param string $value Value for the cookie 114 * @param string $expire Cookie expiration period 115 * @param string $path Path 116 * @param string $domain Domain name 117 * @param bool $secure If secure 118 * @param bool $httpOnly If uses only HTTP 119 * 120 * @return sfBrowser This sfBrowser instance 121 */ 122 public function setCookie($name, $value, $expire = null, $path = '/', $domain = '', $secure = false, $httpOnly = false) 123 { 124 $this->cookieJar[$name] = array( 125 'name' => $name, 126 'value' => $value, 127 'expire' => $expire, 128 'path' => $path, 129 'domain' => $domain, 130 'secure' => (Boolean) $secure, 131 'httpOnly' => $httpOnly, 132 ); 133 134 return $this; 135 } 136 137 /** 138 * Removes a cookie by name. 139 * 140 * @param string $name The cookie name 141 * 142 * @return sfBrowser This sfBrowser instance 143 */ 144 public function removeCookie($name) 145 { 146 unset($this->cookieJar[$name]); 147 148 return $this; 149 } 150 151 /** 152 * Clears all cookies. 153 * 154 * @return sfBrowser This sfBrowser instance 155 */ 156 public function clearCookies() 157 { 158 $this->cookieJar = array(); 159 160 return $this; 161 } 162 163 /** 109 164 * Sets username and password for simulating http authentication. 110 165 * … … 234 289 } 235 290 291 // expire cookies 292 $cookies = $this->cookieJar; 293 foreach ($cookies as $name => $cookie) 294 { 295 if ($cookie['expire'] && $cookie['expire'] < time()) 296 { 297 unset($this->cookieJar[$name]); 298 } 299 } 300 236 301 // restore cookies 237 302 $_COOKIE = array(); … … 269 334 270 335 // save cookies 271 $this->cookieJar = array();272 336 foreach ($response->getCookies() as $name => $cookie) 273 337 { 274 // FIXME: deal with expire,path, secure, ...338 // FIXME: deal with path, secure, ... 275 339 $this->cookieJar[$name] = $cookie; 276 340 } branches/1.2/test/functional/sfTestBrowserTest.php
r9192 r10830 89 89 isResponseHeader('foo', 'foobar') 90 90 ; 91 92 // cookies 93 $b-> 94 setCookie('foo', 'bar')-> 95 setCookie('bar', 'foo')-> 96 setCookie('foofoo', 'foo', time() - 10)-> 97 98 get('/cookie')-> 99 hasCookie('foofoo', false)-> 100 hasCookie('foo')-> 101 isCookie('foo', 'bar')-> 102 isCookie('foo', '/a/')-> 103 isCookie('foo', '!/z/')-> 104 checkResponseElement('p', 'bar.foo-')-> 105 get('/cookie')-> 106 hasCookie('foo')-> 107 isCookie('foo', 'bar')-> 108 isCookie('foo', '/a/')-> 109 isCookie('foo', '!/z/')-> 110 checkResponseElement('p', 'bar.foo-')-> 111 removeCookie('foo')-> 112 get('/cookie')-> 113 hasCookie('foo', false)-> 114 hasCookie('bar')-> 115 checkResponseElement('p', '.foo-')-> 116 clearCookies()-> 117 get('/cookie')-> 118 hasCookie('foo', false)-> 119 hasCookie('bar', false)-> 120 checkResponseElement('p', '.-') 121 ; 122 123 $b-> 124 setCookie('foo', 'bar')-> 125 setCookie('bar', 'foo')-> 126 127 get('/cookie/setCookie')-> 128 129 get('/cookie')-> 130 hasCookie('foo')-> 131 isCookie('foo', 'bar')-> 132 isCookie('foo', '/a/')-> 133 isCookie('foo', '!/z/')-> 134 checkResponseElement('p', 'bar.foo-barfoo')-> 135 get('/cookie')-> 136 hasCookie('foo')-> 137 isCookie('foo', 'bar')-> 138 isCookie('foo', '/a/')-> 139 isCookie('foo', '!/z/')-> 140 checkResponseElement('p', 'bar.foo-barfoo')-> 141 removeCookie('foo')-> 142 get('/cookie')-> 143 hasCookie('foo', false)-> 144 hasCookie('bar')-> 145 checkResponseElement('p', '.foo-barfoo')-> 146 147 get('/cookie/removeCookie')-> 148 149 get('/cookie')-> 150 hasCookie('foo', false)-> 151 hasCookie('bar')-> 152 checkResponseElement('p', '.foo-')-> 153 154 get('/cookie/setCookie')-> 155 156 clearCookies()-> 157 get('/cookie')-> 158 hasCookie('foo', false)-> 159 hasCookie('bar', false)-> 160 checkResponseElement('p', '.-') 161 ;

