diff options
author | Dries Buytaert <dries@buytaert.net> | 2008-04-20 18:34:43 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2008-04-20 18:34:43 +0000 |
commit | ffc0e93c4eb0555a08f0b58bed0735416e6ba41f (patch) | |
tree | 7f741d0b40124633c5b337dc7210711bfcd4b876 /modules/simpletest/unit_tester.php | |
parent | af474609e3e80db9ba1d16b9ad2eae89775f51c8 (diff) | |
download | brdo-ffc0e93c4eb0555a08f0b58bed0735416e6ba41f.tar.gz brdo-ffc0e93c4eb0555a08f0b58bed0735416e6ba41f.tar.bz2 |
- Added a test framework to Drupal along with a first batch of tests for
Drupal core! This is an important milestone for the project so enable
the module and check it out ... :)
Thanks to Rok Žlender, Károly Négyesi, Jimmy Berry, Kevin Bridges, Charlie
Gordon, Douglas Hubler, Miglius Alaburda, Andy Kirkham, Dimitri13, Kieran
Lal, Moshe Weitzman, and the many other people that helped with testing
over the past years and that drove this home.
It all works but it is still rough around the edges (i.e. documentation
is still being written, the coding style is not 100% yet, a number of
tests still fail) but we spent the entire weekend working on it in Paris
and made a ton of progress. The best way to help and to get up to speed,
is to start writing and contributing some tests ... as well as fixing
some of the failures.
For those willing to help with improving the test framework, here are
some next steps and issues to resolve:
- How to best approach unit tests and mock functions?
- How to test drupal_mail() and drupal_http_request()?
- How to improve the admin UI so we have a nice progress bar?
- How best to do code coverage?
- See http://g.d.o/node/10099 for more ...
Diffstat (limited to 'modules/simpletest/unit_tester.php')
-rw-r--r-- | modules/simpletest/unit_tester.php | 178 |
1 files changed, 178 insertions, 0 deletions
diff --git a/modules/simpletest/unit_tester.php b/modules/simpletest/unit_tester.php new file mode 100644 index 000000000..cf4ea16ba --- /dev/null +++ b/modules/simpletest/unit_tester.php @@ -0,0 +1,178 @@ +<?php +// $Id$ + +/** + * Standard unit test class for day to day testing + * of PHP code XP style. Adds some useful standard + * assertions. + */ +class UnitTestCase extends SimpleTestCase { + + /** + * Creates an empty test case. Should be subclassed + * with test methods for a functional test case. + * @param string $label Name of test case. Will use + * the class name if none specified. + * @access public + */ + function UnitTestCase($label = false) { + if (!$label) { + $label = get_class($this); + } + $this->SimpleTestCase($label); + } + + /** + * Called from within the test methods to register + * passes and failures. + * @param boolean $result Pass on true. + * @param string $message Message to display describing + * the test state. + * @return boolean True on pass + * @access public + */ + function assertTrue($result, $message = FALSE, $group = 'Other') { + return $this->assert(new TrueExpectation(), $result, $message, $group); + } + + /** + * Will be true on false and vice versa. False + * is the PHP definition of false, so that null, + * empty strings, zero and an empty array all count + * as false. + * @param boolean $result Pass on false. + * @param string $message Message to display. + * @return boolean True on pass + * @access public + */ + function assertFalse($result, $message = '%s', $group = 'Other') { + $dumper = &new SimpleDumper(); + $message = sprintf($message, 'Expected false, got ['. $dumper->describeValue($result) .']'); + return $this->assertTrue(!$result, $message, $group); + } + + /** + * Will be true if the value is null. + * @param null $value Supposedly null value. + * @param string $message Message to display. + * @return boolean True on pass + * @access public + */ + function assertNull($value, $message = '%s', $group = 'Other') { + $dumper = &new SimpleDumper(); + $message = sprintf($message, '['. $dumper->describeValue($value) .'] should be null'); + return $this->assertTrue(!isset($value), $message, $group); + } + + /** + * Will be true if the value is set. + * @param mixed $value Supposedly set value. + * @param string $message Message to display. + * @return boolean True on pass. + * @access public + */ + function assertNotNull($value, $message = '%s', $group = 'Other') { + $dumper = &new SimpleDumper(); + $message = sprintf($message, '['. $dumper->describeValue($value) .'] should not be null'); + return $this->assertTrue(isset($value), $message, $group); + } + + /** + * Will trigger a pass if the two parameters have + * the same value only. Otherwise a fail. + * @param mixed $first Value to compare. + * @param mixed $second Value to compare. + * @param string $message Message to display. + * @return boolean True on pass + * @access public + */ + function assertEqual($first, $second, $message = '%s', $group = 'Other') { + $dumper = &new SimpleDumper(); + $message = sprintf($message, 'Expected '. $dumper->describeValue($first) .', got ['. $dumper->describeValue($second) .']'); + $this->assertTrue($first == $second, $message, $group); + } + + /** + * Will trigger a pass if the two parameters have + * a different value. Otherwise a fail. + * @param mixed $first Value to compare. + * @param mixed $second Value to compare. + * @param string $message Message to display. + * @return boolean True on pass + * @access public + */ + function assertNotEqual($first, $second, $message = '%s', $group = 'Other') { + $dumper = &new SimpleDumper(); + $message = sprintf($message, 'Expected '. $dumper->describeValue($first) .', not equal to '. $dumper->describeValue($second)); + $this->assertTrue($first != $second, $message, $group); + } + + /** + * Will trigger a pass if the two parameters have + * the same value and same type. Otherwise a fail. + * @param mixed $first Value to compare. + * @param mixed $second Value to compare. + * @param string $message Message to display. + * @return boolean True on pass + * @access public + */ + function assertIdentical($first, $second, $message = '%s', $group = 'Other') { + $dumper = &new SimpleDumper(); + $message = sprintf($message, 'Expected '. $dumper->describeValue($first) .', got ['. $dumper->describeValue($second) .']'); + $this->assertTrue($first === $second, $message, $group); + } + + /** + * Will trigger a pass if the two parameters have + * the different value or different type. + * @param mixed $first Value to compare. + * @param mixed $second Value to compare. + * @param string $message Message to display. + * @return boolean True on pass + * @access public + */ + function assertNotIdentical($first, $second, $message = '%s', $group = 'Other') { + $dumper = &new SimpleDumper(); + $message = sprintf($message, 'Expected '. $dumper->describeValue($first) .', not identical to '. $dumper->describeValue($second)); + $this->assertTrue($first !== $second, $message, $group); + } + + /** + * Will trigger a pass if the Perl regex pattern + * is found in the subject. Fail otherwise. + * @param string $pattern Perl regex to look for including + * the regex delimiters. + * @param string $subject String to search in. + * @param string $message Message to display. + * @return boolean True on pass + * @access public + */ + function assertPattern($pattern, $subject, $message = '%s', $group = 'Other') { + $dumper = &new SimpleDumper(); + $replace = 'Pattern '. $pattern .' detected in ['. $dumper->describeValue($subject) .']'; + $found = preg_match($pattern, $subject, $matches); + if ($found) { + $position = strpos($subject, $matches[0]); + $replace .= ' in region ['. $dumper->clipString($subject, 100, $position) .']'; + } + $message = sprintf($message, $replace); + $this->assertTrue($found, $message, $group); + } + + /** + * Will trigger a pass if the perl regex pattern + * is not present in subject. Fail if found. + * @param string $pattern Perl regex to look for including + * the regex delimiters. + * @param string $subject String to search in. + * @param string $message Message to display. + * @return boolean True on pass + * @access public + */ + function assertNoPattern($pattern, $subject, $message = '%s', $group = 'Other') { + $dumper = &new SimpleDumper(); + $found = preg_match($pattern, $subject); + $message = sprintf($message, 'Pattern '. $pattern .' not detected in ['. $dumper->describeValue($subject) .']'); + $this->assertFalse($found, $message, $group = 'Other'); + } +}
\ No newline at end of file |