-
Notifications
You must be signed in to change notification settings - Fork 2
/
testeroid.php
165 lines (129 loc) · 3.46 KB
/
testeroid.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/**
* Plugin Name: Testeroid
* Description: TDD and simple auto tests with WP CLI
* Version: 0.9
*/
namespace Testeroid;
use WP_CLI, Throwable;
if ( class_exists( 'WP_CLI' ) ) {
WP_CLI::add_command( 'test', function ($terms, $args) {
$results = testing( $terms, $args );
WP_CLI::log(sprintf('success: %s', $results['success']));
if(!empty($results['fails'])){
WP_CLI::log(sprintf('fails: %s', $results['fail']));
WP_CLI::error_multi_line( $results['fails'] );
}
if ( $results['success'] && empty( $results['fails'] ) ) {
WP_CLI::success( 'tests success' );
} else {
WP_CLI::error( 'tests fails', $exit = true );
}
});
}
function testing( $terms, $args ) {
global $test_results, $testerod_tests;
if(isset($args['filter'])){
$text = $args['filter'];
}
$path = __DIR__ . '/includes/';
if ( defined( 'TESTEROID_TESTS_PATH' ) ) {
$path = trailingslashit( TESTEROID_TESTS_PATH );
}
if ( isset( $terms[0] ) ) {
$term = $terms[0];
if ( str_ends_with( $term, '.php' ) ) {
$path_pattern_test = $path_pattern = trailingslashit( $path ) . $term;
if ( file_exists( $path_pattern_test ) ) {
require_once $path_pattern_test;
} else {
WP_CLI::error( 'Tests no found: ' . $path_pattern_test, $exit = false );
}
} else {
WP_CLI::error( 'File name shoud be php format. ' . $term, $exit = false );
}
} else {
$path_pattern = trailingslashit( $path ) . '*.php';
foreach ( glob( $path_pattern ) as $php_include ) {
require_once( $php_include );
}
}
if ( empty( $testerod_tests ) ) {
WP_CLI::error( 'No tests. ' . $term, $exit = false );
}
$progress = \WP_CLI\Utils\make_progress_bar( 'Testing', count( $testerod_tests ), $interval = 1 );
if ( empty( $test_results ) ) {
$test_results = [
'success' => 0,
'fail' => 0,
'fails' => [],
];
}
foreach ( $testerod_tests as $test ) {
$progress->tick();
if(isset($text)){
if($text !== $test['text']){
continue;
}
}
if ( $test['active'] ) {
try {
$result = call_user_func( $test['callback'] );
if ( $result ) {
$test_results['success']++;
} else {
$test_results['fail']++;
$msg = $test['text'];
if(isset($test['debug_backtrace'][0])){
$log = $test['debug_backtrace'][0];
$msg .= ' | File: ' . $log['file'] . ":" . $log['line'];
}
$test_results['fails'][] = $msg;
}
} catch (Throwable $th) {
$test_results['fail']++;
$test_results['fails'][] = $test['text'] . '; ' . $th->getMessage() . '; ' . $th->getFile() . ':' . $th->getLine();
}
}
}
$progress->finish();
return $test_results;
}
function test( $text, $function, $active = true ) {
global $testerod_tests;
if ( empty( $testerod_tests ) ) {
$testerod_tests = [];
}
$testerod_tests[] = [
'text' => $text,
'callback' => $function,
'active' => $active,
'debug_backtrace' => debug_backtrace(),
];
}
/**
* like wc_transaction_query()
* $type: start, rollback or commit
*/
function transaction_query( $type = 'start' ) {
global $wpdb;
$wpdb->hide_errors();
switch ( $type ) {
case 'commit':
$wpdb->query( 'COMMIT' );
break;
case 'rollback':
$wpdb->query( 'ROLLBACK' );
break;
default:
$wpdb->query( 'START TRANSACTION' );
break;
}
}
function ddcli( ...$vars ) {
foreach ( $vars as $key => $var ) {
print_r( $var, false );
echo PHP_EOL;
}
exit;
}