From 2cf11723ef3e81ec9be9d11d7b4376a30926ed5d Mon Sep 17 00:00:00 2001 From: Demian Katz <demian.katz@villanova.edu> Date: Mon, 17 Aug 2015 13:20:49 -0400 Subject: [PATCH] Eliminated need for output buffering during unit tests. --- .../src/VuFind/Cookie/CookieManager.php | 61 ++++++++++++++----- module/VuFind/tests/bootstrap.php | 5 +- 2 files changed, 46 insertions(+), 20 deletions(-) diff --git a/module/VuFind/src/VuFind/Cookie/CookieManager.php b/module/VuFind/src/VuFind/Cookie/CookieManager.php index 20a5af45e9b..67357cbc867 100644 --- a/module/VuFind/src/VuFind/Cookie/CookieManager.php +++ b/module/VuFind/src/VuFind/Cookie/CookieManager.php @@ -117,7 +117,20 @@ class CookieManager } /** - * Set a cookie. + * Support method for setGlobalCookie -- proxy PHP's setcookie() function + * for compatibility with unit testing. + * + * @return bool + */ + public function proxySetCookie() + { + // Special case: in test suite -- don't actually write headers! + return defined('VUFIND_PHPUNIT_RUNNING') + ? true : call_user_func_array('setcookie', func_get_args()); + } + + /** + * Support method for set() -- set the actual cookie in PHP. * * @param string $key Name of cookie to set * @param mixed $value Value to set @@ -125,25 +138,41 @@ class CookieManager * * @return bool */ - public function set($key, $value, $expire = 0) + public function setGlobalCookie($key, $value, $expire) { - if (is_array($value)) { - $success = true; - foreach ($value as $i => $curr) { - $lastSuccess = setcookie( - $key . '[' . $i . ']', $curr, $expire, - $this->path, $this->domain, $this->secure - ); - if (!$lastSuccess) { - $success = false; - } - } - } else { - $success = setcookie( + // Simple case: flat value. + if (!is_array($value)) { + return $this->proxySetCookie( $key, $value, $expire, $this->path, $this->domain, $this->secure ); } - if ($success) { + + // Complex case: array of values. + $success = true; + foreach ($value as $i => $curr) { + $lastSuccess = $this->proxySetCookie( + $key . '[' . $i . ']', $curr, $expire, + $this->path, $this->domain, $this->secure + ); + if (!$lastSuccess) { + $success = false; + } + } + return $success; + } + + /** + * Set a cookie. + * + * @param string $key Name of cookie to set + * @param mixed $value Value to set + * @param int $expire Cookie expiration time + * + * @return bool + */ + public function set($key, $value, $expire = 0) + { + if ($success = $this->setGlobalCookie($key, $value, $expire)) { $this->cookies[$key] = $value; } return $success; diff --git a/module/VuFind/tests/bootstrap.php b/module/VuFind/tests/bootstrap.php index e951bd4a78a..7b32da1a850 100644 --- a/module/VuFind/tests/bootstrap.php +++ b/module/VuFind/tests/bootstrap.php @@ -45,7 +45,4 @@ if (file_exists('vendor/autoload.php')) { $loader->register(); } -define('PHPUNIT_SEARCH_FIXTURES', realpath(__DIR__ . '/../../VuFindSearch/tests/unit-tests/fixtures')); - -// Use output buffering -- some tests involve HTTP headers and will fail if there is output. -ob_start(); \ No newline at end of file +define('PHPUNIT_SEARCH_FIXTURES', realpath(__DIR__ . '/../../VuFindSearch/tests/unit-tests/fixtures')); \ No newline at end of file -- GitLab