-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.php
127 lines (105 loc) · 3.84 KB
/
test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
namespace plough\stats;
use plough\log;
require_once("config.php");
require_once("local-wp-mock.php");
require_once("updater.php");
require_once(__DIR__ . "/../logger.php");
require_once(__DIR__ . "/../utils.php");
const TEST_DIR = __DIR__ . "/test/test/";
const BASELINE_ROOT_DIR = __DIR__ . "/test/baseline/";
const RESULT_ROOT_DIR = __DIR__ . "/test/result/";
const IGNORE_PHRASES = array("Last updated");
log\init(true);
log\info("");
log\info("Initialising test harness");
$tests_passed = 0;
$tests_failed = 0;
if ($argc == 1)
$filter = "*";
elseif ($argc == 2)
$filter = $argv[1];
foreach (glob(TEST_DIR . "{$filter}.xml") as $test_file)
{
$test_name = basename($test_file, ".xml");
log\info("");
log\info(\plough\SEPARATOR_LINE);
log\info("Test [" . $test_name . "]");
log\info("");
$config = Config::fromXmlFile($test_file);
// Backup DB
$db_path = \plough\get_stats_db_path($config);
$db_path_backup = $db_path . ".backup";
if (file_exists($db_path) && !$config->clearDb())
copy($db_path, $db_path_backup);
$updater = new Updater($config);
$updater->update_stats();
$baseline_dir = BASELINE_ROOT_DIR . $test_name;
$result_dir = RESULT_ROOT_DIR . $test_name;
$test_passed = true;
foreach (glob($result_dir . "/*.csv") as $result_path)
{
$output_filename = basename($result_path);
$baseline_path = $baseline_dir . "/" . $output_filename;
if (!file_exists($baseline_path))
{
log\warning("Test [" . $test_name . "] - baseline file not found for [" . $output_filename . "]");
$test_passed = false;
}
$baseline_file = file($baseline_path);
$result_file = file($result_path);
if (count($baseline_file) != count($result_file))
{
log\error("Test [" . $test_name . "] failed - line count difference found in [" . $output_filename . "]");
$test_passed = false;
}
else
{
foreach ($baseline_file as $idx => $base_content)
{
$result_content = $result_file[$idx];
if ($base_content != $result_content)
{
// Check if difference should be ignored
$ignore_difference = false;
foreach (IGNORE_PHRASES as $ignore_phrase)
{
if (strpos($base_content, $ignore_phrase) !== false)
{
$ignore_difference = true;
break;
}
}
if (!$ignore_difference)
{
log\error("Test [" . $test_name . "] failed - difference found in [" . $output_filename . "]");
$test_passed = false;
break;
}
}
}
}
}
if ($test_passed)
{
$tests_passed += 1;
// Restore DB as test has passed
if (file_exists($db_path_backup) && !$config->clearDb())
{
copy($db_path_backup, $db_path);
unlink($db_path_backup);
}
}
else
{
$tests_failed += 1;
}
}
log\info("");
log\info(\plough\SEPARATOR_LINE);
log\info("");
log\info("Tests completed");
log\info(" Tests passed: " . $tests_passed);
log\info(" Tests failed: " . $tests_failed);
log\info("");
?>