2013-04-22 04:09:50 +00:00
|
|
|
<?php defined('SYSPATH') or die('No direct script access.');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A version of the stock PHPUnit testcase that includes some extra helpers
|
|
|
|
* and default settings
|
|
|
|
*/
|
|
|
|
abstract class Kohana_Unittest_TestCase extends PHPUnit_Framework_TestCase {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make sure PHPUnit backs up globals
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $backupGlobals = FALSE;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A set of unittest helpers that are shared between normal / database
|
|
|
|
* testcases
|
|
|
|
* @var Kohana_Unittest_Helpers
|
|
|
|
*/
|
|
|
|
protected $_helpers = NULL;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A default set of environment to be applied before each test
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $environmentDefault = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a predefined environment using the default environment
|
|
|
|
*
|
|
|
|
* Extending classes that have their own setUp() should call
|
|
|
|
* parent::setUp()
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$this->_helpers = new Unittest_Helpers;
|
|
|
|
|
|
|
|
$this->setEnvironment($this->environmentDefault);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restores the original environment overriden with setEnvironment()
|
|
|
|
*
|
|
|
|
* Extending classes that have their own tearDown()
|
|
|
|
* should call parent::tearDown()
|
|
|
|
*/
|
|
|
|
public function tearDown()
|
|
|
|
{
|
|
|
|
$this->_helpers->restore_environment();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes all kohana related cache files in the cache directory
|
|
|
|
*/
|
|
|
|
public function cleanCacheDir()
|
|
|
|
{
|
|
|
|
return Unittest_Helpers::clean_cache_dir();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function that replaces all occurences of '/' with
|
|
|
|
* the OS-specific directory separator
|
|
|
|
*
|
|
|
|
* @param string $path The path to act on
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function dirSeparator($path)
|
|
|
|
{
|
|
|
|
return Unittest_Helpers::dir_separator($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allows easy setting & backing up of enviroment config
|
|
|
|
*
|
|
|
|
* Option types are checked in the following order:
|
|
|
|
*
|
|
|
|
* * Server Var
|
|
|
|
* * Static Variable
|
|
|
|
* * Config option
|
|
|
|
*
|
|
|
|
* @param array $environment List of environment to set
|
|
|
|
*/
|
|
|
|
public function setEnvironment(array $environment)
|
|
|
|
{
|
|
|
|
return $this->_helpers->set_environment($environment);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check for internet connectivity
|
|
|
|
*
|
|
|
|
* @return boolean Whether an internet connection is available
|
|
|
|
*/
|
|
|
|
public function hasInternet()
|
|
|
|
{
|
|
|
|
return Unittest_Helpers::has_internet();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-01 10:50:24 +00:00
|
|
|
* Evaluate an HTML or XML string and assert its structure and/or contents.
|
2013-04-22 04:09:50 +00:00
|
|
|
*
|
2016-05-01 10:50:24 +00:00
|
|
|
* NOTE:
|
|
|
|
* Overriding this method to remove the deprecation error
|
|
|
|
* when tested with PHPUnit 4.2.0+
|
2013-04-22 04:09:50 +00:00
|
|
|
*
|
2016-05-01 10:50:24 +00:00
|
|
|
* TODO:
|
|
|
|
* this should be removed when phpunit-dom-assertions gets released
|
|
|
|
* https://github.com/phpunit/phpunit-dom-assertions
|
2013-04-22 04:09:50 +00:00
|
|
|
*
|
2016-05-01 10:50:24 +00:00
|
|
|
* @param array $matcher
|
|
|
|
* @param string $actual
|
2013-04-22 04:09:50 +00:00
|
|
|
* @param string $message
|
2016-05-01 10:50:24 +00:00
|
|
|
* @param bool $isHtml
|
|
|
|
* @uses Unittest_TestCase::tag_match
|
2013-04-22 04:09:50 +00:00
|
|
|
*/
|
2016-05-01 10:50:24 +00:00
|
|
|
public static function assertTag($matcher, $actual, $message = '', $isHtml = true)
|
2013-04-22 04:09:50 +00:00
|
|
|
{
|
2016-05-01 10:50:24 +00:00
|
|
|
//trigger_error(__METHOD__ . ' is deprecated', E_USER_DEPRECATED);
|
2013-04-22 04:09:50 +00:00
|
|
|
|
2016-05-01 10:50:24 +00:00
|
|
|
$matched = static::tag_match($matcher, $actual, $message, $isHtml);
|
|
|
|
static::assertTrue($matched, $message);
|
2013-04-22 04:09:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-01 10:50:24 +00:00
|
|
|
* This assertion is the exact opposite of assertTag
|
2013-04-22 04:09:50 +00:00
|
|
|
*
|
2016-05-01 10:50:24 +00:00
|
|
|
* Rather than asserting that $matcher results in a match, it asserts that
|
|
|
|
* $matcher does not match
|
2013-04-22 04:09:50 +00:00
|
|
|
*
|
2016-05-01 10:50:24 +00:00
|
|
|
* NOTE:
|
|
|
|
* Overriding this method to remove the deprecation error
|
|
|
|
* when tested with PHPUnit 4.2.0+
|
2013-04-22 04:09:50 +00:00
|
|
|
*
|
2016-05-01 10:50:24 +00:00
|
|
|
* TODO:
|
|
|
|
* this should be removed when phpunit-dom-assertions gets released
|
|
|
|
* https://github.com/phpunit/phpunit-dom-assertions
|
2013-04-22 04:09:50 +00:00
|
|
|
*
|
2016-05-01 10:50:24 +00:00
|
|
|
* @param array $matcher
|
|
|
|
* @param string $actual
|
2013-04-22 04:09:50 +00:00
|
|
|
* @param string $message
|
2016-05-01 10:50:24 +00:00
|
|
|
* @param bool $isHtml
|
|
|
|
* @uses Unittest_TestCase::tag_match
|
2013-04-22 04:09:50 +00:00
|
|
|
*/
|
2016-05-01 10:50:24 +00:00
|
|
|
public static function assertNotTag($matcher, $actual, $message = '', $isHtml = true)
|
2013-04-22 04:09:50 +00:00
|
|
|
{
|
2016-05-01 10:50:24 +00:00
|
|
|
//trigger_error(__METHOD__ . ' is deprecated', E_USER_DEPRECATED);
|
2013-04-22 04:09:50 +00:00
|
|
|
|
2016-05-01 10:50:24 +00:00
|
|
|
$matched = static::tag_match($matcher, $actual, $message, $isHtml);
|
|
|
|
static::assertFalse($matched, $message);
|
2013-04-22 04:09:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-01 10:50:24 +00:00
|
|
|
* Helper function to match HTML string tags against certain criteria
|
2013-04-22 04:09:50 +00:00
|
|
|
*
|
2016-05-01 10:50:24 +00:00
|
|
|
* TODO:
|
|
|
|
* this should be removed when phpunit-dom-assertions gets released
|
|
|
|
* https://github.com/phpunit/phpunit-dom-assertions
|
|
|
|
*
|
|
|
|
* @param array $matcher
|
|
|
|
* @param string $actual
|
2013-04-22 04:09:50 +00:00
|
|
|
* @param string $message
|
2016-05-01 10:50:24 +00:00
|
|
|
* @param bool $isHtml
|
|
|
|
* @return bool TRUE if there is a match FALSE otherwise
|
2013-04-22 04:09:50 +00:00
|
|
|
*/
|
2016-05-01 10:50:24 +00:00
|
|
|
protected static function tag_match($matcher, $actual, $message = '', $isHtml = true)
|
2013-04-22 04:09:50 +00:00
|
|
|
{
|
2016-05-01 10:50:24 +00:00
|
|
|
$dom = PHPUnit_Util_XML::load($actual, $isHtml);
|
|
|
|
$tags = PHPUnit_Util_XML::findNodes($dom, $matcher, $isHtml);
|
|
|
|
return count($tags) > 0 && $tags[0] instanceof DOMNode;
|
2013-04-22 04:09:50 +00:00
|
|
|
}
|
|
|
|
}
|