Commit a1c4ad24 authored by Ivan Chepurnyi's avatar Ivan Chepurnyi

+ Controller Test Case (Request, Response, Layout, Session Support)

! Some changes in configuration constraints
+ Added JSON constraint + assert method in base test case
! Changes in configuration
! Fix issues with mock object creation
! Changes in app/etc/local.phpunit for supporting controller test cases
! Refactored module, almost all the interfaces moved to lib. 
! NOTICE: Constraints are implmented without knowing about Magento system at all, so everyone can use them, just by creating own module that will implement required interface from lib.
parent 68f9fe81
...@@ -10,6 +10,10 @@ if (!Mage::isInstalled()) { ...@@ -10,6 +10,10 @@ if (!Mage::isInstalled()) {
exit('Magento Unit Tests can be runned only on installed version'); exit('Magento Unit Tests can be runned only on installed version');
} }
/* Replace server variables for proper file naming */
$_SERVER['SCRIPT_NAME'] = dirname(__FILE__) . DS . 'index.php';
$_SERVER['SCRIPT_FILENAME'] = dirname(__FILE__) . DS . 'index.php';
Mage::app('admin'); Mage::app('admin');
Mage::getConfig()->init(); Mage::getConfig()->init();
......
<?php
/**
* PHP Unit test suite for Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2011 Ecommerce Developers (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
/**
* Front controller for test suite
*
*/
class EcomDev_PHPUnit_Controller_Front extends Mage_Core_Controller_Varien_Front
{
/**
* Overriden for getting rid of unusual behavior in the test case,
* because test should be isolated
*
* (non-PHPdoc)
* @see Mage_Core_Controller_Varien_Front::_checkBaseUrl()
*/
protected function _checkBaseUrl()
{
// Does nothing
}
/**
* Overriden for getting rid
* of initialization of routers for each test case
*
* (non-PHPdoc)
* @see Mage_Core_Controller_Varien_Front::init()
*/
public function init()
{
if (!$this->_routers) {
parent::init();
}
return $this;
}
}
\ No newline at end of file
<?php
/**
* PHP Unit test suite for Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2011 Ecommerce Developers (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
/**
* Request class for usage in the controller test cases
*
* By default set for test app instance,
* you can change to your class,
* but you should extend it from this one
*
*/
class EcomDev_PHPUnit_Controller_Request_Http
extends Mage_Core_Controller_Request_Http
implements EcomDev_PHPUnit_Isolation_Interface,
EcomDev_PHPUnit_Controller_Request_Interface
{
/**
* List of $_SERVER variable changes
* that were done by test case
*
* @var array
*/
protected $_originalServerValues = array();
/**
* List of headers that were set for test case
*
* @return array
*/
protected $_headers = array();
/**
* Initializes forward data
*
* (non-PHPdoc)
* @see Mage_Core_Controller_Request_Http::initForward()
*/
public function initForward()
{
if (empty($this->_beforeForwardInfo)) {
parent::initForward();
$this->_beforeForwardInfo['route_name'] = $this->getRouteName();
return $this;
}
return parent::initForward();
}
/**
* Returns only request uri that was set before
* (non-PHPdoc)
* @see Zend_Controller_Request_Http::getRequestUri()
*/
public function getRequestUri()
{
return $this->_requestUri;
}
/**
* Sets cookie value for test,
*
* @param string|array $name
* @param string|null $value
* @return EcomDev_PHPUnit_Controller_Request_Http_Test
*/
public function setCookie($name, $value)
{
$_COOKIE[$name] = $value;
return $this;
}
/**
* Sets more than one cookie
*
* @param array $cookies
* @return EcomDev_PHPUnit_Controller_Request_Http_Test
*/
public function setCookies(array $cookies)
{
$_COOKIE += $cookies;
return $this;
}
/**
* Resets all cookies for the test request
*
* @return EcomDev_PHPUnit_Controller_Request_Http_Test
*/
public function resetCookies()
{
$_COOKIE = array();
return $this;
}
/**
* Resets query for the current request
*
* @return EcomDev_PHPUnit_Controller_Request_Http_Test
*/
public function resetQuery()
{
$_GET = array();
return $this;
}
/**
* Resets $_POST superglobal for test request
*
* @return EcomDev_PHPUnit_Controller_Request_Http_Test
*/
public function resetPost()
{
$_POST = array();
return $this;
}
/**
* Resets user defined request params for test request
*
* @return EcomDev_PHPUnit_Controller_Request_Http_Test
*/
public function resetParams()
{
$this->_params = array();
return $this;
}
/**
* Resets internal properties to its default values
*
* @return EcomDev_PHPUnit_Controller_Request_Http_Test
*/
public function resetInternalProperties()
{
// From abstract request
$this->_dispatched = false;
$this->_module = null;
$this->_moduleKey = 'module';
$this->_controller = null;
$this->_controllerKey = 'controller';
$this->_action = null;
$this->_actionKey = 'action';
// From Http request
$this->_paramSources = array('_GET', '_POST');
$this->_requestUri = null;
$this->_baseUrl = null;
$this->_basePath = null;
$this->_pathInfo = '';
$this->_rawBody = null;
$this->_aliases = array();
// From Magento Http request
$this->_originalPathInfo = '';
$this->_storeCode = null;
$this->_requestString = '';
$this->_rewritedPathInfo = null;
$this->_requestedRouteName = null;
$this->_routingInfo = array();
$this->_route = null;
$this->_directFrontNames = null;
$this->_controllerModule = null;
return $this;
}
/**
* Set custom http header
*
* @param string $name
* @param string $value
* @return EcomDev_PHPUnit_Controller_Request_Http_Test
*/
public function setHeader($name, $value)
{
$name = $this->headerName($name);
$this->_headers[$name] = $value;
// Additionally set $_SERVER http header value
$this->setServer('HTTP_' . $name, $value);
return $this;
}
/**
* Sets more than one header,
* headers list is an associative array
*
* @param array $headers
* @return EcomDev_PHPUnit_Controller_Request_Http_Test
*/
public function setHeaders(array $headers)
{
foreach ($headers as $name => $value) {
$this->setHeader($name, $value);
}
return $this;
}
/**
* Returns header from test request parameters
*
* (non-PHPdoc)
* @see Zend_Controller_Request_Http::getHeader()
*/
public function getHeader($header)
{
$name = $this->headerName($header);
if (isset($this->_headers[$name])) {
return $this->_headers[$name];
}
return false;
}
/**
* Resets headers in test request
*
* @return EcomDev_PHPUnit_Controller_Request_Http_Test
*/
public function resetHeaders()
{
$this->_headers = array();
return $this;
}
/**
* Returns unified header name for internal storage
*
* @param string $name
* @return string
*/
protected function headerName($name)
{
return strtr(strtoupper($name), '-', '_');
}
/**
* Sets value for a particular $_SERVER superglobal array key for test request
*
* Saves original value for returning it back
*
* @param string $name
* @param string $value
* @return EcomDev_PHPUnit_Controller_Request_Http_Test
*/
public function setServer($name, $value)
{
if (!isset($this->_originalServerValues[$name])) {
$originalValue = (isset($_SERVER[$name]) ? $_SERVER[$name] : null);
$this->_originalServerValues[$name] = $originalValue;
}
$_SERVER[$name] = $value;
return $this;
}
/**
* Sets multiple values for $_SERVER superglobal in test request
*
* @param array $values
* @return EcomDev_PHPUnit_Controller_Request_Http_Test
*/
public function setServers(array $values)
{
foreach ($values as $name => $value) {
$this->setServer($name, $value);
}
return $this;
}
/**
* Resets $_SERVER superglobal to previous state
*
* @return EcomDev_PHPUnit_Controller_Request_Http_Test
*/
public function resetServer()
{
foreach ($this->_originalServerValues as $name => $value) {
if ($value !== null) {
$_SERVER[$name] = $value;
} elseif (isset($_SERVER[$name])) {
// If original value was not set,
// then unsetting the changed value
unset($_SERVER[$name]);
}
}
$this->_originalServerValues = array();
return $this;
}
/**
* Sets request method for test request
*
* @param string $requestMethod
* @return EcomDev_PHPUnit_Controller_Request_Http_Test
*/
public function setMethod($requestMethod)
{
$this->setServer('REQUEST_METHOD', $requestMethod);
return $this;
}
/**
* Sets current request scheme for test request,
* accepts boolean flag for HTTPS
*
* @param boolean $flag
* @return EcomDev_PHPUnit_Controller_Request_Http_Test
*/
public function setIsSecure($flag = true)
{
$this->setServer('HTTPS', $flag ? 'on' : null);
return $this;
}
/**
* Returns HTTP host from base url that were set in the controller
*
* (non-PHPdoc)
* @see Mage_Core_Controller_Request_Http::getHttpHost()
*/
public function getHttpHost($trimPort = false)
{
$baseUrl = $this->getBaseUrl();
$parts = parse_url($baseUrl);
$httpHost = $parts['host'];
if (!$trimPort && isset($parts['port'])) {
$httpHost .= ':' . $parts['port'];
}
return $httpHost;
}
/**
* Returns only base url that was set before
*
* (non-PHPdoc)
* @see Mage_Core_Controller_Request_Http::getBaseUrl()
*/
public function getBaseUrl()
{
return $this->_baseUrl;
}
/**
* Resets all request data for test
*
* @return EcomDev_PHPUnit_Controller_Request_Http_Test
*/
public function reset()
{
$this->resetInternalProperties()
->resetHeaders()
->resetParams()
->resetPost()
->resetQuery()
->resetCookies()
->resetServer();
return $this;
}
}
<?php
/**
* PHP Unit test suite for Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2011 Ecommerce Developers (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
/**
* Response class for usage in the controller test cases
*
* By default set for test app instance,
* you can change to your class,
* but you should extend it from this one
*
*/
class EcomDev_PHPUnit_Controller_Response_Http
extends Mage_Core_Controller_Response_Http
implements EcomDev_PHPUnit_Isolation_Interface,
EcomDev_PHPUnit_Controller_Response_Interface
{
const LINE_ENDING = "\r\n";
/**
* Headers that was sent via sendHeaders() method
*
* @var array
*/
protected $_sentHeaders = null;
/**
* Response that was sent via sendRespose()
* or sendHeaders() or outputBody() methods
*
* @var string
*/
protected $_sentResponse = null;
/**
* Response body that was sent via outputBody()
* method
*
* @var string
*/
protected $_outputBody = null;
/**
* Resets response object
*
* @return EcomDev_PHPUnit_Controller_Response_Http_Test
*/
public function reset()
{
$this->_sentHeaders = null;
$this->_sentResponse = null;
$this->_outputBody = null;
$this->_body = array();
$this->_exceptions = array();
$this->_headers = array();
$this->_headersRaw = array();
$this->_httpResponseCode = 200;
$this->_isRedirect = false;
$this->_renderExceptions = false;
$this->headersSentThrowsException = Mage::$headersSentThrowsException;
$this->setHeader('Content-Type', 'text/html; charset=UTF-8');
return $this;
}
/**
* Implementation of sending the headers to output
*
* (non-PHPdoc)
* @see Mage_Core_Controller_Response_Http::sendHeaders()
*/
public function sendHeaders()
{
$this->canSendHeaders(true);
$this->_sentHeaders= array();
$this->_sentHeaders[null] = 'HTTP/1.1 ' . $this->_httpResponseCode;
foreach ($this->_headersRaw as $headerRaw) {
list($headerName, $headerValue) = explode(':', $headerRaw, 2);
$headerName = $this->_normalizeHeader($headerName);
if (isset($this->_sentHeaders[$headerName])) {
// Merge headers, if already any was sent with the same name
// Set-Cookie header is usually not sent via response
// so it should be ok.
$this->_sentHeaders[$headerName] .= '; ' . $headerValue;
} else {
$this->_sentHeaders[$headerName] = $headerValue;
}
}
foreach ($this->_headers as $header) {
if (isset($this->_sentHeaders[$header['name']])) {
$this->_sentHeaders[$header['name']] .= '; ' . $header['value'];
} else {
$this->_sentHeaders[$header['name']] = $header['value'];
}
}
$this->_sentResponse = '';
foreach ($this->_sentHeaders as $headerName => $headerValue) {
$headerString = '';
if ($headerName === null) {
$headerString .= $headerName . ': ';
}
$headerString .= $headerValue . self::LINE_ENDING;
$this->_sentResponse .= $headerString;
}
$this->_sentResponse .= self::LINE_ENDING;
}
/**
* Returns rendered headers array that was sent,
* if headers was not sent, then returns null
*
* @return array|null
*/
public function getSentHeaders()
{
return $this->_sentHeaders;
}
/**
* Returns a particular header that was sent
*
* @param string $headerName
* @return string|false
*/
public function getSentHeader($headerName)
{
$headerName = $this->_normalizeHeader($headerName);
if (isset($this->_sentHeaders[$headerName])) {
return $this->_sentHeaders[$headerName];
}
return false;
}
/**
* Implementation of sending response for test case
* (non-PHPdoc)
* @see Mage_Core_Controller_Response_Http::sendResponse()
*/
public function sendResponse()
{
//Mage::dispatchEvent('http_response_send_before', array('response'=>$this));
$this->sendHeaders();
if ($this->isException() && $this->renderExceptions()) {
$exceptions = '';
foreach ($this->getException() as $e) {
$exceptions .= (string)$e . "\n";
}
$this->_sentResponse .= $exceptions;
return;
}
$this->outputBody();
}
/**
* Returns rendered response, if response was not sent,
* then it returns null
*
* @return string|null
*/
public function getSentResponse()
{
return $this->_sentResponse;
}
/**
* Implementation of outputting the body of response
*
* (non-PHPdoc)
* @see Zend_Controller_Response_Abstract::outputBody()
*/
public function outputBody()
{
$this->_outputBody = implode('', $this->_body);
$this->_sentResponse .= $this->_outputBody;
}
/**
* Returns rendered body output
*
* @return string
*/
public function getOutputBody()
{
return $this->_outputBody;
}
/**
* Can send headers implementation for test case
*
* (non-PHPdoc)
* @see Zend_Controller_Response_Abstract::canSendHeaders()
*/
public function canSendHeaders($throw = false)
{
if ($this->_sentHeaders !== null && $throw && $this->headersSentThrowsException) {
#require_once 'Zend/Controller/Response/Exception.php';
throw new Zend_Controller_Response_Exception('Cannot send headers; headers already sent');
}
return $this->_sentHeaders === null;
}
}
\ No newline at end of file
<?php
/**
* PHP Unit test suite for Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2011 Ecommerce Developers (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
/**
* Implementation of app area for a test
*
*
*/
class EcomDev_PHPUnit_Model_App_Area
extends Mage_Core_Model_App_Area
implements EcomDev_PHPUnit_Isolation_Interface
{
const AREA_TEST = 'test';
/**
* Reset all the data,
* related to a particular area
*
* @return EcomDev_PHPUnit_Model_App_Area
*/
public function reset()
{
$this->resetEvents();
$this->resetDesign();
$this->resetTranslate();
return $this;
}
/**
* Resets events area data
*
* @return EcomDev_PHPUnit_Model_App_Area
*/
public function resetEvents()
{
$this->_application->removeEventArea($this->_code);
return $this;
}
/**
* Resets design related area data
*
*
* @return EcomDev_PHPUnit_Model_App_Area
*/
public function resetDesign()
{
Mage::getSingleton('core/design')->unsetData();
Mage::getSingleton('core/design_package')->reset();
return $this;
}
/**
* Resets translator data
*
* @return EcomDev_PHPUnit_Model_App_Area
*/
public function resetTranslate()
{
$translator = $this->_application->getTranslator();
EcomDev_Utils_Reflection::setRestrictedPropertyValue($translator, '_config', null);
EcomDev_Utils_Reflection::setRestrictedPropertyValue($translator, '_locale', null);
EcomDev_Utils_Reflection::setRestrictedPropertyValue($translator, '_cacheId', null);
EcomDev_Utils_Reflection::setRestrictedPropertyValue($translator, '_translateInline', null);
EcomDev_Utils_Reflection::setRestrictedPropertyValue($translator, '_dataScope', null);
EcomDev_Utils_Reflection::setRestrictedPropertyValue($translator, '_data', array());
return $this;
}
}
\ No newline at end of file
...@@ -23,6 +23,10 @@ ...@@ -23,6 +23,10 @@
*/ */
class EcomDev_PHPUnit_Model_Config extends Mage_Core_Model_Config class EcomDev_PHPUnit_Model_Config extends Mage_Core_Model_Config
{ {
const XML_PATH_SECURE_BASE_URL = 'default/web/secure/base_url';
const XML_PATH_UNSECURE_BASE_URL = 'default/web/unsecure/base_url';
const CHANGE_ME = '[change me]';
/** /**
* Scope snapshot without applied configurations, * Scope snapshot without applied configurations,
* It is used for proper store/website/default loading on per store basis * It is used for proper store/website/default loading on per store basis
...@@ -212,6 +216,7 @@ class EcomDev_PHPUnit_Model_Config extends Mage_Core_Model_Config ...@@ -212,6 +216,7 @@ class EcomDev_PHPUnit_Model_Config extends Mage_Core_Model_Config
$merge = clone $this->_prototype; $merge = clone $this->_prototype;
if ($merge->loadFile($this->_getLocalXmlForTest())) { if ($merge->loadFile($this->_getLocalXmlForTest())) {
$this->_checkDbCredentialForDuplicate($this, $merge); $this->_checkDbCredentialForDuplicate($this, $merge);
$this->_checkBaseUrl($this, $merge);
$this->extend($merge); $this->extend($merge);
} else { } else {
throw new Exception('Unable to load local.xml.phpunit'); throw new Exception('Unable to load local.xml.phpunit');
...@@ -254,6 +259,30 @@ class EcomDev_PHPUnit_Model_Config extends Mage_Core_Model_Config ...@@ -254,6 +259,30 @@ class EcomDev_PHPUnit_Model_Config extends Mage_Core_Model_Config
return $this; return $this;
} }
/**
* Check base url settings, if not set it rises an exception
*
* @param Mage_Core_Model_Config_Base $original
* @param Mage_Core_Model_Config_Base $test
* @return EcomDev_PHPUnit_Model_Config
* @throws RuntimeException
*/
protected function _checkBaseUrl($original, $test)
{
$baseUrlSecure = (string)$test->getNode(self::XML_PATH_SECURE_BASE_URL);
$baseUrlUnsecure = (string)$test->getNode(self::XML_PATH_UNSECURE_BASE_URL);
if (empty($baseUrlSecure) || empty($baseUrlUnsecure)
|| $baseUrlSecure == self::CHANGE_ME || $baseUrlUnsecure == self::CHANGE_ME) {
echo sprintf(
'Please change values in %s file for nodes %s and %s. '
. 'It will help in setting up proper controller test cases',
'app/etc/local.phpunit', self::XML_PATH_SECURE_BASE_URL, self::XML_PATH_UNSECURE_BASE_URL
);
exit();
}
}
/** /**
* Retrieves local.xml file path for tests, * Retrieves local.xml file path for tests,
* If it is not found, method will rise an exception * If it is not found, method will rise an exception
......
...@@ -2,7 +2,9 @@ ...@@ -2,7 +2,9 @@
class EcomDev_PHPUnit_Model_Design_Package class EcomDev_PHPUnit_Model_Design_Package
extends Mage_Core_Model_Design_Package extends Mage_Core_Model_Design_Package
implements EcomDev_PHPUnit_Constraint_Config_Layout_Design_Package_Interface implements EcomDev_PHPUnit_Design_Package_Interface,
EcomDev_PHPUnit_Isolation_Interface
{ {
/** /**
* Asserts layout file existance in design packages, * Asserts layout file existance in design packages,
......
This diff is collapsed.
...@@ -29,7 +29,7 @@ class EcomDev_PHPUnit_Test_Suite extends PHPUnit_Framework_TestSuite ...@@ -29,7 +29,7 @@ class EcomDev_PHPUnit_Test_Suite extends PHPUnit_Framework_TestSuite
// Configuration path constants // Configuration path constants
const XML_PATH_UNIT_TEST_GROUPS = 'phpunit/suite/groups'; const XML_PATH_UNIT_TEST_GROUPS = 'phpunit/suite/groups';
const XML_PATH_UNIT_TEST_MODULES = 'phpunit/suite/modules'; const XML_PATH_UNIT_TEST_MODULES = 'phpunit/suite/modules';
const XML_PATH_UNIT_TEST_APP = 'phpunit/suite/app'; const XML_PATH_UNIT_TEST_APP = 'phpunit/suite/app/class';
const XML_PATH_UNIT_TEST_SUITE = 'phpunit/suite/test_suite'; const XML_PATH_UNIT_TEST_SUITE = 'phpunit/suite/test_suite';
const CACHE_TAG = 'ECOMDEV_PHPUNIT'; const CACHE_TAG = 'ECOMDEV_PHPUNIT';
const CACHE_TYPE = 'ecomdev_phpunit'; const CACHE_TYPE = 'ecomdev_phpunit';
......
...@@ -51,12 +51,18 @@ ...@@ -51,12 +51,18 @@
<helpers>Helper</helpers> <helpers>Helper</helpers>
<blocks>Block</blocks> <blocks>Block</blocks>
<config>Config</config> <config>Config</config>
<controllers>Controller</controllers>
</groups> </groups>
<!-- Test suite that will be used for creation of each of the tests --> <!-- Test suite that will be used for creation of each of the tests -->
<test_suite>EcomDev_PHPUnit_Test_Suite_Group</test_suite> <test_suite>EcomDev_PHPUnit_Test_Suite_Group</test_suite>
<layout> <layout>
<model>ecomdev_phpunit/layout</model> <model>ecomdev_phpunit/layout</model>
</layout> </layout>
<design>
<package>
<model>ecomdev_phpunit/design_package</model>
</package>
</design>
<expectation> <expectation>
<!-- Default model for loading of expectations --> <!-- Default model for loading of expectations -->
<model>ecomdev_phpunit/expectation</model> <model>ecomdev_phpunit/expectation</model>
...@@ -73,8 +79,27 @@ ...@@ -73,8 +79,27 @@
<catalog_category>ecomdev_phpunit/fixture_eav_catalog_category</catalog_category> <catalog_category>ecomdev_phpunit/fixture_eav_catalog_category</catalog_category>
</eav> </eav>
</fixture> </fixture>
<!-- Application model class name for running tests --> <app>
<app>EcomDev_PHPUnit_Model_App</app> <!-- Application class name for running tests -->
<class>EcomDev_PHPUnit_Model_App</class>
<area>
<!-- Application area class name for tests -->
<class>EcomDev_PHPUnit_Model_App_Area</class>
</area>
</app>
<!-- Configuration of controller for test cases -->
<controller>
<front>
<class>EcomDev_PHPUnit_Controller_Front</class>
</front>
<request>
<class>EcomDev_PHPUnit_Controller_Request_Http</class>
</request>
<response>
<class>EcomDev_PHPUnit_Controller_Response_Http</class>
</response>
</controller>
<modules> <modules>
<!-- Place your module name in your module config.xml <!-- Place your module name in your module config.xml
For adding it to test suite --> For adding it to test suite -->
...@@ -84,4 +109,17 @@ ...@@ -84,4 +109,17 @@
</modules> </modules>
</suite> </suite>
</phpunit> </phpunit>
<test>
<!-- Definition of event observers that will be used only for test environment -->
<events>
<core_block_abstract_to_html_after>
<observers>
<ecomdev_phpunit>
<class>core/layout</class>
<method>recordBlockRender</method>
</ecomdev_phpunit>
</observers>
</core_block_abstract_to_html_after>
</events>
</test>
</config> </config>
...@@ -9,4 +9,17 @@ ...@@ -9,4 +9,17 @@
</default_setup> </default_setup>
</resources> </resources>
</global> </global>
<default>
<web>
<seo>
<use_rewrites>1</use_rewrites>
</seo>
<secure>
<base_url>[change me]</base_url>
</secure>
<unsecure>
<base_url>[change me]</base_url>
</unsecure>
</web>
</default>
</config> </config>
<?php
/**
* PHP Unit test suite for Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2011 Ecommerce Developers (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
/**
* Abstract constraint for EcomDev_PHPUnit constraints
* Contains flexible constaint types implementation
*
*/
abstract class EcomDev_PHPUnit_Constraint_Abstract
extends PHPUnit_Framework_Constraint
{
/**
* List of valiadation rules for expected value
* It is an associative array with key as type and value
* as an array of rules.
*
* First item of the rule array is mandatory indicator,
* second is function name for checking the type,
* third one is the type that will be displayed in invalid argument expception
* each of them can be ommited or if it between other ones just by specifying null value
*
* @var array
*/
protected $_expectedValueValidation = array();
/**
* List of types that will use diff for displaying fail result
*
* @var array
*/
protected $_typesWithDiff = array();
/**
* Comparisment type defined in the constructor
*
* @var string
*/
protected $_type = null;
/**
* Expected value defined in the constructor
*
* @var mixed
*/
protected $_expectedValue = null;
/**
* Custom actual value
*
* @var mixed
*/
protected $_actualValue = null;
/**
* Flag for using of actual value in failure description
*
* @var boolean
*/
protected $_useActualValue = false;
/**
* Abstract cnstraint constructor,
* provides unified interface for working with multiple types of evalation
*
* @param string $type
* @param mixed $expectedValue
*/
public function __construct($type, $expectedValue = null)
{
$reflection = EcomDev_Utils_Reflection::getRelflection(get_class($this));
$types = array();
foreach ($reflection->getConstants() as $name => $constant) {
if (strpos($name, 'TYPE_') === 0) {
$types[] = $constant;
}
}
if (empty($type) || !is_string($type) || !in_array($type, $types)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string', $type);
}
if (isset($this->_expectedValueValidation[$type])) {
$expectedValueType = (isset($this->_expectedValueValidation[$type][2]) ?
isset($this->_expectedValueValidation[$type][2]) :
'');
// Mandatory check
if (isset($this->_expectedValueValidation[$type][0])
&& $this->_expectedValueValidation[$type][0]
&& $expectedValue === null) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, $expectedValueType, $expectedValue);
}
// Type check
if (isset($this->_expectedValueValidation[$type][1])
&& !$this->_expectedValueValidation[$type][1]($expectedValue)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, $expectedValueType, $expectedValue);
}
}
$this->_type = $type;
$this->_expectedValue = $expectedValue;
}
/**
* Set actual value that will be used in the fail message
*
* @param mixed $actual
* @return EcomDev_PHPUnit_Constraint_Abstract
*/
protected function setActualValue($actual)
{
$this->_useActualValue = true;
$this->_actualValue = $actual;
return $this;
}
/**
* Calls internal protected method by defined constraint type
* Also can be passed a single argument
*
* @param string $prefix
* @return mixed
*/
protected function callProtectedByType($prefix, $argument = null)
{
$camelizedType = uc_words($this->_type, '');
$methodName = $prefix . $camelizedType;
return $this->$methodName($argument);
}
/**
* Evaluates value by type.
* (non-PHPdoc)
* @see PHPUnit_Framework_Constraint::evaluate()
*/
public function evaluate($other)
{
return $this->callProtectedByType('evaluate', $other);
}
/**
* Generates a failure exception based on exception type
*
* (non-PHPdoc)
* @see PHPUnit_Framework_Constraint::fail()
*/
public function fail($other, $description, $not = FALSE)
{
$failureDescription = $this->failureDescription($other, $description, $not);
if (in_array($this->_type, $this->_typesWithDiff)) {
throw new EcomDev_PHPUnit_Constraint_Exception(
$failureDescription,
PHPUnit_Util_Diff::diff($this->getExpectedValue(), $this->getActualValue($other)),
$description
);
} else {
throw new EcomDev_PHPUnit_Constraint_Exception(
$failureDescription, $this->getActualValue($other), $description
);
}
}
/**
* Returns a scalar representation of actual value,
* Returns $other if internal acutal value is not set
*
* @param Varien_Simplexml_Element $other
* @return scalar
*/
protected function getActualValue($other = null)
{
if ($this->_useActualValue) {
return $this->_actualValue;
}
return $other;
}
/**
* Returns a scalar representation of expected value
*
* @return string
*/
protected function getExpectedValue()
{
return $this->_expectedValue;
}
/**
* Text reperesentation of constraint
* (non-PHPdoc)
* @see PHPUnit_Framework_SelfDescribing::toString()
*/
public function toString()
{
return $this->callProtectedByType('text');
}
}
\ No newline at end of file
...@@ -44,17 +44,14 @@ class EcomDev_PHPUnit_Constraint_Config extends PHPUnit_Framework_Constraint ...@@ -44,17 +44,14 @@ class EcomDev_PHPUnit_Constraint_Config extends PHPUnit_Framework_Constraint
*/ */
public function __construct($constraint) public function __construct($constraint)
{ {
$this->config = $config;
if (!$constraint instanceof EcomDev_PHPUnit_Constraint_Config_Interface) { if (!$constraint instanceof EcomDev_PHPUnit_Constraint_Config_Interface) {
throw PHPUnit_Util_InvalidArgumentHelper::factory( throw PHPUnit_Util_InvalidArgumentHelper::factory(
1, 'EcomDev_PHPUnit_Constraint_Config_Interface' 1, 'EcomDev_PHPUnit_Constraint_Config_Interface'
); );
} }
$this->constraint = $constraint; $this->constraint = $constraint;
} }
/** /**
* *
* @param mixed $other * @param mixed $other
......
...@@ -21,31 +21,9 @@ ...@@ -21,31 +21,9 @@
* *
*/ */
abstract class EcomDev_PHPUnit_Constraint_Config_Abstract abstract class EcomDev_PHPUnit_Constraint_Config_Abstract
extends PHPUnit_Framework_Constraint extends EcomDev_PHPUnit_Constraint_Abstract
implements EcomDev_PHPUnit_Constraint_Config_Interface implements EcomDev_PHPUnit_Constraint_Config_Interface
{ {
/**
* List of valiadation rules for expected value
* It is an associative array with key as type and value
* as an array of rules.
*
* First item of the rule array is mandatory indicator,
* second is function name for checking the type,
* third one is the type that will be displayed in invalid argument expception
* each of them can be ommited or if it between other ones just by specifying null value
*
* @var array
*/
protected $_expectedValueValidation = array();
/**
* List of types that will use diff for displaying fail result
*
* @var array
*/
protected $_typesWithDiff = array();
/** /**
* Config node path defined in the constructor * Config node path defined in the constructor
* *
...@@ -53,34 +31,6 @@ abstract class EcomDev_PHPUnit_Constraint_Config_Abstract ...@@ -53,34 +31,6 @@ abstract class EcomDev_PHPUnit_Constraint_Config_Abstract
*/ */
protected $_nodePath = null; protected $_nodePath = null;
/**
* Comparisment type defined in the constructor
*
* @var string
*/
protected $_type = null;
/**
* Expected value defined in the constructor
*
* @var mixed
*/
protected $_expectedValue = null;
/**
* Custom actual value
*
* @var mixed
*/
protected $_actualValue = null;
/**
* Flag for using of actual valu in failure description
*
* @var boolean
*/
protected $_useActualValue = false;
/** /**
* Constraint constructor * Constraint constructor
* *
...@@ -94,56 +44,8 @@ abstract class EcomDev_PHPUnit_Constraint_Config_Abstract ...@@ -94,56 +44,8 @@ abstract class EcomDev_PHPUnit_Constraint_Config_Abstract
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string', $type); throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string', $type);
} }
$reflection = EcomDev_Utils_Reflection::getRelflection(get_class($this));
$types = array();
foreach ($reflection->getConstants() as $name => $constant) {
if (strpos($name, 'TYPE_') === 0) {
$types[] = $constant;
}
}
if (empty($type) || !is_string($type) || !in_array($type, $types)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string', $type);
}
if (isset($this->_expectedValueValidation[$type])) {
$expectedValueType = (isset($this->_expectedValueValidation[$type][2]) ?
isset($this->_expectedValueValidation[$type][2]) :
'');
// Mandatory check
if (isset($this->_expectedValueValidation[$type][0])
&& $this->_expectedValueValidation[$type][0]
&& $expectedValue === null) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(3, $expectedValueType, $expectedValue);
}
// Type check
if (isset($this->_expectedValueValidation[$type][1])
&& !$this->_expectedValueValidation[$type][1]($expectedValue)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(3, $expectedValueType, $expectedValue);
}
}
$this->_nodePath = $nodePath; $this->_nodePath = $nodePath;
$this->_type = $type; parent::__construct($type, $expectedValue);
$this->_expectedValue = $expectedValue;
}
/**
* Set actual value that will be used in the fail message
*
* @param mixed $actual
* @return EcomDev_PHPUnit_Constraint_Config_Abstract
*/
protected function setActualValue($actual)
{
$this->_useActualValue = true;
$this->_actualValue = $actual;
return $this;
} }
/** /**
...@@ -158,22 +60,10 @@ abstract class EcomDev_PHPUnit_Constraint_Config_Abstract ...@@ -158,22 +60,10 @@ abstract class EcomDev_PHPUnit_Constraint_Config_Abstract
} }
/** /**
* Calls internal protected method by defined constraint type * Automatically evaluate to false if the node was not found
* Also can be passed a single argument
* *
* @param string $prefix
*/
protected function callProtectedByType($prefix, $argument = null)
{
$camelizedType = uc_words($this->_type, '');
$methodName = $prefix . $camelizedType;
return $this->$methodName($argument);
}
/**
* Evaluates value by type.
* (non-PHPdoc) * (non-PHPdoc)
* @see PHPUnit_Framework_Constraint::evaluate() * @see EcomDev_PHPUnit_Constraint_Abstract::evaluate()
*/ */
public function evaluate($other) public function evaluate($other)
{ {
...@@ -182,59 +72,39 @@ abstract class EcomDev_PHPUnit_Constraint_Config_Abstract ...@@ -182,59 +72,39 @@ abstract class EcomDev_PHPUnit_Constraint_Config_Abstract
return false; return false;
} }
return $this->callProtectedByType('evaluate', $other); return parent::evaluate($other);
} }
/** /**
* Generates a failure exception based on exception type * Returns a scalar representation of actual value,
* Returns $other if internal acutal value is not set
* *
* (non-PHPdoc) * @param Varien_Simplexml_Element $other
* @see PHPUnit_Framework_Constraint::fail() * @return scalar
*/ */
public function fail($other, $description, $not = FALSE) protected function getActualValue($other = null)
{ {
$failureDescription = $this->failureDescription($other, $description, $not); if (!$this->_useActualValue && $other->hasChildren()) {
return $other->asNiceXml();
if (in_array($this->_type, $this->_typesWithDiff)) { } elseif (!$this->_useActualValue) {
if ($this->_useActualValue) { return (string) $other;
$other = $this->_actualValue;
} elseif ($other->hasChildren()) {
$other = $other->asNiceXml();
} else {
$other = (string) $other;
}
if ($this->_expectedValue instanceof Varien_Simplexml_Element) {
$expected = $this->_expectedValue->asNiceXml();
} else {
$expected = $this->_expectedValue;
}
throw new EcomDev_PHPUnit_Constraint_Exception(
$failureDescription,
PHPUnit_Util_Diff::diff($expected, $other),
$description
);
} else {
if (!$this->_useActualValue) {
$actualValue = $other->asNiceXml();
} else {
$actualValue = $this->_actualValue;
}
throw new EcomDev_PHPUnit_Constraint_Exception(
$failureDescription, $actualValue, $description
);
} }
return parent::getActualValue($other);
} }
/** /**
* Text reperesentation of constraint * Returns a scalar representation of expected value
* (non-PHPdoc) *
* @see PHPUnit_Framework_SelfDescribing::toString() * @return string
*/ */
public function toString() protected function getExpectedValue()
{ {
return $this->callProtectedByType('text'); if ($this->_expectedValue instanceof Varien_Simplexml_Element) {
return $this->_expectedValue->asNiceXml();
}
return parent::getExpectedValue();
} }
} }
\ No newline at end of file
...@@ -99,9 +99,9 @@ class EcomDev_PHPUnit_Constraint_Config_Layout ...@@ -99,9 +99,9 @@ class EcomDev_PHPUnit_Constraint_Config_Layout
/** /**
* Sets design package model for assertions * Sets design package model for assertions
* *
* @param EcomDev_PHPUnit_Constraint_Config_Design_Package_Interface $model * @param EcomDev_PHPUnit_Design_Package_Interface $model
*/ */
public static function setDesignPackageModel(EcomDev_PHPUnit_Constraint_Config_Design_Package_Interface $model) public static function setDesignPackageModel(EcomDev_PHPUnit_Design_Package_Interface $model)
{ {
self::$_designPackageModel = $model; self::$_designPackageModel = $model;
} }
...@@ -109,7 +109,7 @@ class EcomDev_PHPUnit_Constraint_Config_Layout ...@@ -109,7 +109,7 @@ class EcomDev_PHPUnit_Constraint_Config_Layout
/** /**
* Retrieves design package model that was set before * Retrieves design package model that was set before
* *
* @return EcomDev_PHPUnit_Constraint_Config_Design_Package_Interface * @return EcomDev_PHPUnit_Design_Package_Interface
*/ */
public static function getDesignPackageModel() public static function getDesignPackageModel()
{ {
...@@ -162,7 +162,7 @@ class EcomDev_PHPUnit_Constraint_Config_Layout ...@@ -162,7 +162,7 @@ class EcomDev_PHPUnit_Constraint_Config_Layout
->getLayoutFileAssertion($this->_expectedValue, $this->_area, $this->_designPackage, $this->_theme); ->getLayoutFileAssertion($this->_expectedValue, $this->_area, $this->_designPackage, $this->_theme);
$this->setActualValue($assertion['actual']); $this->setActualValue($assertion['actual']);
$this->_expectedValue = $assertin['expected']; $this->_expectedValue = $assertion['expected'];
return $this->_actualValue !== $this->_expectedValue; return $this->_actualValue !== $this->_expectedValue;
} }
......
<?php
/**
* PHP Unit test suite for Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2011 Ecommerce Developers (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
/**
* Constraint for controller request assetions
*
*/
class EcomDev_PHPUnit_Constraint_Controller_Request extends EcomDev_PHPUnit_Constraint_Abstract
{
const TYPE_ROUTE = 'route';
const TYPE_ROUTE_NAME = 'route_name';
const TYPE_CONTROLLER_NAME = 'controller_name';
const TYPE_CONTROLLER_MODULE = 'controller_module';
const TYPE_ACTION_NAME = 'action_name';
const TYPE_DISPATCHED = 'dispatched';
const TYPE_FORWARDED = 'forwarded';
const TYPE_BEFORE_FORWARD_ROUTE = 'before_forward_route';
/**
* Constraint for controller request assetions
*
*
* @param string $type
* @param string|null $expectedValue
*/
public function __construct($type, $expectedValue = null)
{
$this->_expectedValueValidation += array(
self::TYPE_ROUTE => array(true, 'is_string', 'string'),
self::TYPE_ROUTE_NAME => array(true, 'is_string', 'string'),
self::TYPE_CONTROLLER_NAME => array(true, 'is_string', 'string'),
self::TYPE_CONTROLLER_MODULE => array(true, 'is_string', 'string'),
self::TYPE_ACTION_NAME => array(true, 'is_string', 'string'),
self::TYPE_BEFORE_FORWARD_ROUTE => array(true, 'is_string', 'string')
);
$this->_typesWithDiff[] = self::TYPE_ROUTE;
$this->_typesWithDiff[] = self::TYPE_ROUTE_NAME;
$this->_typesWithDiff[] = self::TYPE_CONTROLLER_NAME;
$this->_typesWithDiff[] = self::TYPE_CONTROLLER_MODULE;
$this->_typesWithDiff[] = self::TYPE_ACTION_NAME;
$this->_typesWithDiff[] = self::TYPE_BEFORE_FORWARD_ROUTE;
parent::__construct($type, $expectedValue);
}
/**
* Parses route to params
*
* @param string $route
* @return array
*/
protected function parseRoute($route)
{
$routeParts = explode('/', $route, 3);
$routePartsCount = count($routeParts);
if ($routePartsCount < 3) {
array_pad($routeParts, 3-$routePartsCount, null);
}
$params = array();
if ($routeParts[0] !== '*') {
$params['route_name'] = !empty($routeParts[0]) ? $routeParts[0] : 'index';
}
if ($routeParts[1] !== '*') {
$params['controller_name'] = !empty($routeParts[1]) ? $routeParts[1] : 'index';
}
if ($routeParts[2] !== '*') {
$params['action_name'] = !empty($routeParts[2]) ? $routeParts[2] : 'index';
}
return $params;
}
/**
* Evaluates that current controller route is equal to expected
*
* @param EcomDev_PHPUnit_Controller_Request_Interface $other
* @return boolean
*/
protected function evaluateRoute($other)
{
$params = $this->parseRoute($this->_expectedValue);
$this->setActualValue(
$other->getRouteName() . '/' . $other->getControllerName()
. '/' . $other->getActionName()
);
foreach ($params as $propertyName => $expectedValue) {
$methodName = 'get'.str_replace(' ', '',
ucwords(strtr($propertyName, '_', ' '))
);
if ($other->$methodName() !== $expectedValue) {
return false;
}
}
return true;
}
/**
* Text reperesentation of route path assertion
*
* @return string
*/
protected function textRoute()
{
return 'route matches expected one';
}
/**
* Evaluates that before forwarding controller route is equal to expected
*
* @param EcomDev_PHPUnit_Controller_Request_Interface $other
* @return boolean
*/
protected function evaluateBeforeForwardRoute($other)
{
if (!$other->getBeforeForwardInfo()) {
$this->setActualValue(false);
return false;
}
$params = $this->parseRoute($this->_expectedValue);
$this->setActualValue(
$other->getBeforeForwardInfo('route_name') . '/'
. $other->getBeforeForwardInfo('controller_name') . '/'
. $other->getBeforeForwardInfo('action_name')
);
foreach ($params as $propertyName => $expectedValue) {
if ($other->getBeforeForwardInfo($propertyName) !== $expectedValue) {
return false;
}
}
return true;
}
/**
* Text reperesentation of route path assertion
*
* @return string
*/
protected function textBeforeForwardRoute()
{
return 'route before forwarding matches expected one';
}
/**
* Evaluates that request was forwarded
*
* @param EcomDev_PHPUnit_Controller_Request_Interface $other
* @return boolean
*/
protected function evaluateForwarded($other)
{
return (bool)$other->getBeforeForwardInfo();
}
/**
* Text reperesentation of was forwaded request assertion
*
* @return string
*/
protected function textForwarded()
{
return 'is forwarded';
}
/**
* Evaluates that request was forwarded
*
* @param EcomDev_PHPUnit_Controller_Request_Interface $other
* @return boolean
*/
protected function evaluateDispatched($other)
{
return $other->isDispatched();
}
/**
* Text reperesentation of was forwaded request assertion
*
* @return string
*/
protected function textDispatched()
{
return 'is dispatched';
}
/**
* Evaluates that request route name is equal to expected
*
* @param EcomDev_PHPUnit_Controller_Request_Interface $other
* @return boolean
*/
protected function evaluateRouteName($other)
{
$this->setActualValue($other->getRouteName());
return $this->_actualValue === $this->_expectedValue;
}
/**
* Text reperesentation of route name assertion
*
* @return string
*/
protected function textRouteName()
{
return 'route name is equal to expected';
}
/**
* Evaluates that request controller name is equal to expected
*
* @param EcomDev_PHPUnit_Controller_Request_Interface $other
* @return boolean
*/
protected function evaluateControllerName($other)
{
$this->setActualValue($other->getControllerName());
return $this->_actualValue === $this->_expectedValue;
}
/**
* Text reperesentation of controller name assertion
*
* @return string
*/
protected function textControllerName()
{
return 'controller name is equal to expected';
}
/**
* Evaluates that request controller module is equal to expected
*
* @param EcomDev_PHPUnit_Controller_Request_Interface $other
* @return boolean
*/
protected function evaluateControllerModule($other)
{
$this->setActualValue($other->getControllerModule());
return $this->_actualValue === $this->_expectedValue;
}
/**
* Text reperesentation of controller module assertion
*
* @return string
*/
protected function textControllerModule()
{
return 'controller module is equal to expected';
}
/**
* Evaluates that request action name is equal to expected
*
* @param EcomDev_PHPUnit_Controller_Request_Interface $other
* @return boolean
*/
protected function evaluateActionName($other)
{
$this->setActualValue($other->getActionName());
return $this->_actualValue === $this->_expectedValue;
}
/**
* Text reperesentation of action name assertion
*
* @return string
*/
protected function textActionName()
{
return 'action name is equal to expected';
}
/**
* Custom failure description for showing request related errors
* (non-PHPdoc)
* @see PHPUnit_Framework_Constraint::customFailureDescription()
*/
protected function customFailureDescription($other, $description, $not)
{
return sprintf(
'Failed asserting that request %s.',
$this->toString()
);
}
}
<?php
/**
* PHP Unit test suite for Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2011 Ecommerce Developers (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
/**
* Abstract constraint for controller response assetions
*
*/
abstract class EcomDev_PHPUnit_Constraint_Controller_Response_Abstract
extends EcomDev_PHPUnit_Constraint_Abstract
{
/**
* Custom failure description for showing response related errors
* (non-PHPdoc)
* @see PHPUnit_Framework_Constraint::customFailureDescription()
*/
protected function customFailureDescription($other, $description, $not)
{
return sprintf(
'Failed asserting that request %s.',
$this->toString()
);
}
}
\ No newline at end of file
<?php
/**
* PHP Unit test suite for Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2011 Ecommerce Developers (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
/**
* Constraint for controller response body assertions
*
*/
class EcomDev_PHPUnit_Constraint_Controller_Response_Body
extends EcomDev_PHPUnit_Constraint_Controller_Response_Abstract
{
const TYPE_CONSTRAINT = 'constraint';
/**
* Constraint for controller response body assertions
*
* @param PHPUnit_Framework_Constraint $constraint
* @param string $type
*/
public function __construct(PHPUnit_Framework_Constraint $constraint = null, $type = self::TYPE_CONSTRAINT)
{
$this->_expectedValueValidation += array(
self::TYPE_CONSTRAINT => array(true, null, 'PHPUnit_Framework_Constraint')
);
parent::__construct($type, $constraint);
}
/**
* Evaluates controller response body is evaluated by constraint
*
*
* @param EcomDev_PHPUnit_Controller_Response_Interface $other
*/
protected function evaluateConstraint($other)
{
$this->setActualValue($other->getOutputBody());
return $this->_expectedValue->evaluate($this->_actualValue);
}
/**
* Text representation of response body is evaluated by constraint assertion
*
* @return string
*/
protected function textConstraint()
{
return sprintf('body %s', $this->_expectedValue->toString());
}
}
\ No newline at end of file
<?php
/**
* PHP Unit test suite for Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2011 Ecommerce Developers (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
/**
* Constraint for controller response header assertions
*
*/
class EcomDev_PHPUnit_Constraint_Controller_Response_Header
extends EcomDev_PHPUnit_Constraint_Controller_Response_Abstract
{
const TYPE_CONSTRAINT = 'constraint';
const TYPE_SENT = 'sent';
/**
* The name of the header that will be asserted
*
* @var string
*/
protected $_headerName = null;
/**
* Response header assertion
*
* @param string $headerName
* @param string $type
* @param PHPUnit_Framework_Constraint $constraint
*/
public function __construct($headerName, $type, PHPUnit_Framework_Constraint $constraint = null)
{
if (empty($headerName) || !is_string($headerName)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string', $headerName);
}
$this->_expectedValueValidation += array(
self::TYPE_CONSTRAINT => array(true, null, 'PHPUnit_Framework_Constraint')
);
parent::__construct($type, $constraint);
$this->_headerName = $headerName;
}
/**
* Evaluates controller response header is sent
*
*
* @param EcomDev_PHPUnit_Controller_Response_Interface $other
*/
protected function evaluateSent($other)
{
$this->setActualValue($other->getSentHeaders());
return $other->getSentHeader($this->_headerName) !== false;
}
/**
* Text representation of header is sent assertion
*
* @return string
*/
protected function textSent()
{
return sprintf('header "%s" is sent', $this->_headerName);
}
/**
* Evaluates controller response header is evaluated by constraint
*
*
* @param EcomDev_PHPUnit_Controller_Response_Interface $other
*/
protected function evaluateConstraint($other)
{
$this->setActualValue($other->getSentHeader($this->_headerName));
return $this->_expectedValue->evaluate($this->_actualValue);
}
/**
* Text representation of header is evaluated by constraint assertion
*
* @return string
*/
protected function textConstraint()
{
return sprintf('header "%s" value %s', $this->_headerName, $this->_expectedValue->toString());
}
}
...@@ -27,6 +27,10 @@ class EcomDev_PHPUnit_Constraint_Exception extends PHPUnit_Framework_Expectation ...@@ -27,6 +27,10 @@ class EcomDev_PHPUnit_Constraint_Exception extends PHPUnit_Framework_Expectation
public function __construct($description, $diff = '', $message = '') public function __construct($description, $diff = '', $message = '')
{ {
if (!is_scalar($diff)) {
$diff = print_r($diff, true);
}
$this->diff = $diff; $this->diff = $diff;
parent::__construct($description, null, $message); parent::__construct($description, null, $message);
} }
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
* Interface for assertions in layout configuration * Interface for assertions in layout configuration
* *
*/ */
interface EcomDev_PHPUnit_Constraint_Config_Layout_Design_Package_Interface interface EcomDev_PHPUnit_Design_Package_Interface
{ {
/** /**
* Asserts layout file existance in design packages, * Asserts layout file existance in design packages,
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment