Passed
Push — master ( f13f78...5c1b24 )
by Ismayil
04:22
created

SimpletestCommand   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 0
loc 129
rs 10
c 0
b 0
f 0
wmc 19
lcom 1
cbo 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 10 1
F command() 0 108 18
1
<?php
2
3
namespace Elgg\Cli;
4
5
use Elgg\Application;
6
use Elgg\Config;
7
use Exception;
8
use RuntimeException;
9
use Symfony\Component\Console\Input\InputOption;
10
use TestSuite;
11
use TextReporter;
12
13
/**
14
 * elgg-cli simpletest [--config] [--plugins]
15
 */
16
class SimpletestCommand extends Command {
17
18
	/**
19
	 * {@inheritdoc}
20
	 */
21
	protected function configure() {
22
		$this->setName('simpletest')
23
			->setDescription('Run simpletest test suite')
24
			->addOption('config', 'c', InputOption::VALUE_OPTIONAL,
25
				'Path to settings file that the Elgg Application should be bootstrapped with'
26
			)
27
			->addOption('plugins', 'p', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
28
				'A list of plugins to enable for testing or "all" to enable all plugins'
29
			);
30
	}
31
32
	/**
33
	 * {@inheritdoc}
34
	 */
35
	protected function command() {
36
37
		if (!class_exists('ElggCoreUnitTest')) {
38
			elgg_log('You must install your Elgg application using "composer install --dev"', 'ERROR');
39
40
			return 1;
41
		}
42
43
		// Disable maximum execution time.
44
		// Tests take a while...
45
		set_time_limit(0);
46
47
		ob_start();
48
49
		$error = 0;
50
51
		try {
52
			$settings_path = $this->option('config');
53
			if ($settings_path) {
54
				$sp = _elgg_services();
55
				$app = Application::factory([
56
					'settings_path' => $settings_path,
57
					'service_provider' => $sp,
58
				]);
59
				Application::setInstance($app);
60
			}
61
62
			// turn off system log
63
			_elgg_services()->hooks->unregisterHandler('all', 'all', 'system_log_listener');
64
			_elgg_services()->hooks->unregisterHandler('log', 'systemlog', 'system_log_default_logger');
65
66
			$admin = array_shift(elgg_get_admins(['limit' => 1]));
0 ignored issues
show
Bug introduced by
elgg_get_admins(array('limit' => 1)) cannot be passed to array_shift() as the parameter $array expects a reference.
Loading history...
67
			if (!login($admin)) {
68
				throw new RuntimeException("Failed to login as administrator.");
69
			}
70
71
			// disable emails
72
			elgg_set_email_transport(new \Zend\Mail\Transport\InMemory());
73
74
			$plugins = $this->option('plugins');
75
			if (in_array('all', $plugins)) {
76
				$plugins = [];
77
				$plugin_entities = elgg_get_plugins('inactive');
78
				foreach ($plugin_entities as $plugin_entity) {
79
					$plugins[] = $plugin_entity->getID();
80
				}
81
			} else if (empty($plugins)) {
82
				// plugins that contain unit tests
83
				$plugins = [
84
					'groups',
85
					'thewire',
86
					'web_services'
87
				];
88
			}
89
90
			$activated_plugins = [];
91
92
			// activate plugins that are not activated on install
93
			foreach ($plugins as $key => $id) {
94
				$plugin = elgg_get_plugin_from_id($id);
95
				if (!$plugin || $plugin->isActive()) {
96
					unset($plugins[$key]);
97
					continue;
98
				}
99
				if ($plugin->activate()) {
100
					$activated_plugins[] = $id;
101
				}
102
			}
103
104
			$suite = new TestSuite('Elgg Core Unit Tests');
105
106
			$test_cases = _elgg_services()->hooks->trigger('unit_test', 'system', null, []);
107
			foreach ($test_cases as $file) {
108
				if (substr($file, -4, 4) === '.php') {
109
					$suite->addFile($file);
110
				} else if (class_exists($file)) {
111
					$suite->add($file);
112
				}
113
			}
114
115
			$start_time = microtime(true);
116
117
			$reporter = new TextReporter();
118
			$result = $suite->Run($reporter);
119
120
			// deactivate plugins that were activated for test suite
121
			foreach ($activated_plugins as $key => $id) {
122
				$plugin = elgg_get_plugin_from_id($id);
123
				$plugin->deactivate();
124
			}
125
126
			echo PHP_EOL . sprintf("Time: %.2f seconds, Memory: %.2fMb\n",
127
					microtime(true) - $start_time,
128
					memory_get_peak_usage() / 1048576.0 // in megabytes
129
				) . PHP_EOL;
130
131
			if (!$result) {
132
				throw new RuntimeException('One or more tests have failed');
133
			}
134
		} catch (Exception $e) {
135
			$error = $e->getCode() ? : 1;
136
			elgg_log("Test suite has failed with " . get_class($e) . ': ' . $e->getMessage(), 'ERROR');
137
		}
138
139
		$this->write(ob_get_clean());
140
141
		return $error;
142
	}
143
144
}