Last active
January 29, 2019 22:50
-
-
Save Vinai/9734553 to your computer and use it in GitHub Desktop.
Simple Magento integration test PHPUnit bootstrap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Simple Magento integration test PHPUnit bootstrap | |
*/ | |
chdir(__DIR__ . '/../..'); | |
$mageFile = 'htdocs/app/Mage.php'; | |
umask(0); | |
if (! file_exists($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(); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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