Skip to content

Instantly share code, notes, and snippets.

@amacgregor
Forked from Vinai/bootstrap.php
Created March 24, 2014 09:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amacgregor/9736774 to your computer and use it in GitHub Desktop.
Save amacgregor/9736774 to your computer and use it in GitHub Desktop.
<?php
/**
* Simple Magento integration test PHPUnit bootstrap
*/
chdir(__DIR__ . '/../..');
$mageFile = 'htdocs/app/Mage.php';
umask(0);
if (! $mageFile) {
echo "Unable to locate app/Mage.php\n";
exit(2);
}
// for mikey179/vfsStream
require_once 'vendor/autoload.php';
// bootstrap Magento
require_once $mageFile;
// mock response object that doesn't really send out anything
require_once __DIR__ . '/VinaiKopp/TestFramework/Controller/Response/Http.php';
/**
* Reset and initialize the Magento runtime environment
*
* @param string $configModelClass
*/
function resetMagento($configModelClass = null)
{
Mage::reset();
Mage::setIsDeveloperMode(true);
$options = array();
if ($configModelClass) $options['config_model'] = $configModelClass;
Mage::app('admin', 'store', $options)
->setResponse(new VinaiKopp_TestFramework_Controller_Response_Http);
// Fix error handler/Magento autoloader
$handler = set_error_handler(function() {});
set_error_handler(function($errno, $errstr, $errfile, $errline) use ($handler) {
if (E_WARNING === $errno
&& 0 === strpos($errstr, 'include(')
&& substr($errfile, -19) == 'Varien/Autoload.php'
){
return null;
}
call_user_func($handler, $errno, $errstr, $errfile, $errline);
});
}
// initialize Magento runtime
resetMagento();
<?php
class VinaiKopp_TestFramework_Controller_Response_Http extends Mage_Core_Controller_Response_Http
{
protected $_headersSentFlag = false;
public function canSendHeaders($throw = false)
{
if ($this->_headersSentFlag) {
// Will trigger headers sent exception in testing context
return parent::canSendHeaders($throw);
}
return true;
}
public function setHeadersSentFlag($flag)
{
$this->_headersSentFlag = (bool) $flag;
}
/**
* Contains all parent logic except that the actual header sending is disabled.
*
* @return Mage_Core_Controller_Response_Http
* @see Mage_Core_Controller_Response_Http::sendHeaders()
* @see Zend_Controller_Response_Abstract::sendHeaders()
*/
public function sendHeaders()
{
// From Mage_Core_Controller_Response_Http --------------------------
if (!$this->canSendHeaders()) {
Mage::log('HEADERS ALREADY SENT: ' . mageDebugBacktrace(true, true, true));
return $this;
}
if (substr(php_sapi_name(), 0, 3) == 'cgi') {
$statusSent = false;
foreach ($this->_headersRaw as $i => $header) {
if (stripos($header, 'status:') === 0) {
if ($statusSent) {
unset($this->_headersRaw[$i]);
} else {
$statusSent = true;
}
}
}
foreach ($this->_headers as $i => $header) {
if (strcasecmp($header['name'], 'status') === 0) {
if ($statusSent) {
unset($this->_headers[$i]);
} else {
$statusSent = true;
}
}
}
}
// End code from Mage_Core_Controller_Response_Http --------------------------
// From Zend_Controller_Response_Abstract --------------------------
// Only check if we can send headers if we have headers to send
if (count($this->_headersRaw) || count($this->_headers) || (200 != $this->_httpResponseCode)) {
$this->canSendHeaders(true);
} elseif (200 == $this->_httpResponseCode) {
// Haven't changed the response code, and we have no headers
return $this;
}
$httpCodeSent = false;
foreach ($this->_headersRaw as $header) {
if (!$httpCodeSent && $this->_httpResponseCode) {
//header($header, true, $this->_httpResponseCode);
$httpCodeSent = true;
} else {
//header($header);
}
}
foreach ($this->_headers as $header) {
if (!$httpCodeSent && $this->_httpResponseCode) {
//header($header['name'] . ': ' . $header['value'], $header['replace'], $this->_httpResponseCode);
$httpCodeSent = true;
} else {
//header($header['name'] . ': ' . $header['value'], $header['replace']);
}
}
if (!$httpCodeSent) {
//header('HTTP/1.1 ' . $this->_httpResponseCode);
$httpCodeSent = true;
}
// End code from Zend_Controller_Response_Abstract --------------------------
// Addition for test response object
$this->setHeadersSentFlag(true);
// End test response object addition
return $this;
}
/**
* Do everything except output any content.
*/
public function sendResponse()
{
ob_start();
$result = parent::sendResponse();
ob_end_clean();
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment