-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-jasmine-test-runner.php
executable file
·103 lines (92 loc) · 2.8 KB
/
class-jasmine-test-runner.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
<?php
/**
* Jasmine_Test_Runner.
*
* @package JasmineTestRunner
*/
/**
* Class Jasmine_Test_Runner.
*/
class Jasmine_Test_Runner {
/**
* Add hooks/filters for plugin.
*
* @since 0.1
*/
public function __construct() {
$settings = array(
'loads_in' => array(),
'tests' => array(),
'deps' => array(
'jquery',
'underscore',
),
);
$this->settings = apply_filters( 'jasmine_test_runner', $settings );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) );
}
public function enqueue( $hook ) {
$setting = $this->settings['loads_in'];
if ( in_array( $hook, $setting ) || $setting === array( 'all' ) || isset( $_GET['jtrunner'] ) ) {
self::scripts( $hook );
self::styles( $hook );
}
}
/**
* Jasmine Test Runner Styles.
*
* @since 0.1
*/
private function styles( $hook ) {
wp_enqueue_style( 'jasmine-css', plugins_url( '/assets/css/jasmine.css', __FILE__ ) );
wp_enqueue_style( 'jasmine-test-runner-css', plugins_url( '/assets/css/jasmine-test-runner.css', __FILE__, array( 'jasmine-css' ) ) );
}
/**
* Jasmine Test Runner Scripts.
*
* @since 0.1
*/
private function scripts( $hook ) {
$setting = $this->settings['loads_in'];
if ( in_array( $hook, $setting ) || $setting === array( 'all' ) || isset( $_GET['jtrunner'] ) ) {
wp_enqueue_script( 'jasmine', plugins_url( 'assets/js/jasmine.js', __FILE__ ), array( 'jquery' ) );
wp_enqueue_script( 'jasmine-html', plugins_url( 'assets/js/jasmine-html.js', __FILE__ ), array( 'jquery' ) );
wp_enqueue_script( 'jasmine-boot', plugins_url( 'assets/js/boot.js', __FILE__ ), array( 'jquery' ) );
wp_enqueue_script( 'jasmine-test-runner', plugins_url( 'assets/js/jasmine-test-runner.js', __FILE__ ), array( 'jquery' ) );
self::tests();
}
}
/**
* Enqueue Tests to run.
*
* @since 0.1
*/
private function tests() {
$tests = $this->settings['tests'];
// Use provided test directory if passed in.
if ( isset( $_GET['jtrunner'] ) && ! empty( $_GET['jtrunner'] ) ) {
$dir = trailingslashit( ltrim( $_GET['jtrunner'], '/' ) );
$test_path = WP_CONTENT_DIR . "/$dir";
$files = array();
// Check provided test dir for js files.
foreach ( glob( "{$test_path}/*.js" ) as $filename ) {
$file = basename( $filename );
$files[] = content_url( "/{$dir}{$file}" );
}
$tests = $files;
}
// Default deps which shouldn't be overridden by filter.
$jtrunner_deps = array(
'jasmine',
'jasmine-boot',
'jasmine-html',
'jasmine-test-runner'
);
// Add any deps that were supplied in the filter override.
$deps = array_unique( array_merge( $jtrunner_deps, $this->settings['deps'] ) );
// Load each of the tests with all deps provided.
foreach ( $tests as $test ) {
wp_enqueue_script( 'jtr-' . strtolower( $test ), $test, $this->settings['deps'] );
}
}
}