fixing int() should be (int) in file storage
[moodle.git] / lib / simpletestlib.php
CommitLineData
56768525 1<?php // $Id$
3ef8c936 2/**
3 * Utility functions to make unit testing easier.
b9c639d6 4 *
3ef8c936 5 * These functions, particularly the the database ones, are quick and
b9c639d6 6 * dirty methods for getting things done in test cases. None of these
3ef8c936 7 * methods should be used outside test code.
8 *
9 * @copyright &copy; 2006 The Open University
10 * @author T.J.Hunt@open.ac.uk
11 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
12 * @version $Id$
13 * @package SimpleTestEx
14 */
15
16require_once(dirname(__FILE__) . '/../config.php');
17require_once($CFG->libdir . '/simpletestlib/simpletest.php');
18require_once($CFG->libdir . '/simpletestlib/unit_tester.php');
19require_once($CFG->libdir . '/simpletestlib/expectation.php');
4309bd17 20require_once($CFG->libdir . '/simpletestlib/reporter.php');
a205dcdc 21require_once($CFG->libdir . '/simpletestlib/web_tester.php');
76144765 22require_once($CFG->libdir . '/simpletestlib/mock_objects.php');
3ef8c936 23
24/**
25 * Recursively visit all the files in the source tree. Calls the callback
b9c639d6 26 * function with the pathname of each file found.
27 *
28 * @param $path the folder to start searching from.
3ef8c936 29 * @param $callback the function to call with the name of each file found.
30 * @param $fileregexp a regexp used to filter the search (optional).
b9c639d6 31 * @param $exclude If true, pathnames that match the regexp will be ingored. If false,
3ef8c936 32 * only files that match the regexp will be included. (default false).
33 * @param array $ignorefolders will not go into any of these folders (optional).
b9c639d6 34 */
3ef8c936 35function recurseFolders($path, $callback, $fileregexp = '/.*/', $exclude = false, $ignorefolders = array()) {
36 $files = scandir($path);
37
38 foreach ($files as $file) {
39 $filepath = $path .'/'. $file;
40 if ($file == '.' || $file == '..') {
41 continue;
42 } else if (is_dir($filepath)) {
43 if (!in_array($filepath, $ignorefolders)) {
44 recurseFolders($filepath, $callback, $fileregexp, $exclude, $ignorefolders);
45 }
46 } else if ($exclude xor preg_match($fileregexp, $filepath)) {
47 call_user_func($callback, $filepath);
48 }
49 }
50}
51
52/**
53 * An expectation for comparing strings ignoring whitespace.
54 */
55class IgnoreWhitespaceExpectation extends SimpleExpectation {
56 var $expect;
57
58 function IgnoreWhitespaceExpectation($content, $message = '%s') {
59 $this->SimpleExpectation($message);
60 $this->expect=$this->normalise($content);
61 }
62
63 function test($ip) {
64 return $this->normalise($ip)==$this->expect;
65 }
66
67 function normalise($text) {
68 return preg_replace('/\s+/m',' ',trim($text));
69 }
70
71 function testMessage($ip) {
72 return "Input string [$ip] doesn't match the required value.";
73 }
74}
75
76/**
77 * An Expectation that two arrays contain the same list of values.
78 */
79class ArraysHaveSameValuesExpectation extends SimpleExpectation {
80 var $expect;
81
82 function ArraysHaveSameValuesExpectation($expected, $message = '%s') {
83 $this->SimpleExpectation($message);
84 if (!is_array($expected)) {
85 trigger_error('Attempt to create an ArraysHaveSameValuesExpectation ' .
86 'with an expected value that is not an array.');
87 }
88 $this->expect = $this->normalise($expected);
89 }
90
91 function test($actual) {
92 return $this->normalise($actual) == $this->expect;
93 }
94
95 function normalise($array) {
96 sort($array);
97 return $array;
98 }
99
100 function testMessage($actual) {
101 return 'Array [' . implode(', ', $actual) .
102 '] does not contain the expected list of values [' . implode(', ', $this->expect) . '].';
103 }
104}
105
106/**
107 * An Expectation that compares to objects, and ensures that for every field in the
108 * expected object, there is a key of the same name in the actual object, with
109 * the same value. (The actual object may have other fields to, but we ignore them.)
110 */
111class CheckSpecifiedFieldsExpectation extends SimpleExpectation {
112 var $expect;
113
114 function CheckSpecifiedFieldsExpectation($expected, $message = '%s') {
115 $this->SimpleExpectation($message);
116 if (!is_object($expected)) {
117 trigger_error('Attempt to create a CheckSpecifiedFieldsExpectation ' .
118 'with an expected value that is not an object.');
119 }
120 $this->expect = $expected;
121 }
122
123 function test($actual) {
124 foreach ($this->expect as $key => $value) {
125 if (isset($value) && isset($actual->$key) && $actual->$key == $value) {
126 // OK
127 } else if (is_null($value) && is_null($actual->$key)) {
128 // OK
129 } else {
130 return false;
131 }
132 }
133 return true;
134 }
135
136 function testMessage($actual) {
137 $mismatches = array();
138 foreach ($this->expect as $key => $value) {
139 if (isset($value) && isset($actual->$key) && $actual->$key == $value) {
140 // OK
141 } else if (is_null($value) && is_null($actual->$key)) {
142 // OK
143 } else {
144 $mismatches[] = $key;
145 }
146 }
147 return 'Actual object does not have all the same fields with the same values as the expected object (' .
148 implode(', ', $mismatches) . ').';
149 }
150}
151
b9c639d6 152class MoodleUnitTestCase extends UnitTestCase {
153 public function __construct($label = false) {
154 parent::UnitTestCase($label);
155 global $CFG, $DB;
156
157 if (empty($CFG->unittest_prefix)) {
158 print_error("prefixnotset", 'simpletest');
159 }
160
161 if (!$DB->table_exists('user')) {
162 print_error('tablesnotsetup', 'simpletest');
163 }
164 }
165
166 public function setUp() {
167 parent::setUp();
168 }
169
170 public function tearDown() {
171 parent::tearDown();
172 }
173
174 /**
175 * This will execute once all the tests have been run. It should delete the text file holding info about database contents prior to the tests
176 */
177 public function __destruct() {
178
179 }
180}
181
bb48a537 182?>