Elgg  Version 3.0
SimpletestCommand.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Cli;
4 
9 use TestSuite;
10 use TextReporter;
11 
15 class SimpletestCommand extends Command {
16 
20  protected function configure() {
21  $this->setName('simpletest')
22  ->setDescription('Run simpletest test suite')
23  ->addOption('config', 'c', InputOption::VALUE_OPTIONAL,
24  'Path to settings file that the Elgg Application should be bootstrapped with'
25  )
26  ->addOption('plugins', 'p', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
27  'A list of plugins to enable for testing or "all" to enable all plugins'
28  )
29  ->addOption('filter', 'f', InputOption::VALUE_OPTIONAL,
30  'Only run tests that match filter pattern'
31  );
32  }
33 
37  protected function command() {
38 
39  if (!class_exists('ElggCoreUnitTest')) {
40  elgg_log('You must install your Elgg application using "composer install --dev"', 'ERROR');
41 
42  return 1;
43  }
44 
45  if (!date_default_timezone_get()) {
46  date_default_timezone_set('America/Los_Angeles');
47  }
48 
49  error_reporting(E_ALL | E_STRICT);
50 
51  // Disable maximum execution time.
52  // Tests take a while...
53  set_time_limit(0);
54 
55  $settings_path = $this->option('config');
56  if (!$settings_path) {
57  $settings_path = Paths::elgg() . 'engine/tests/elgg-config/simpletest.php';
58  }
59 
60  $sp = _elgg_services();
61  $app = ElggApplication::factory([
62  'settings_path' => $settings_path,
63  'service_provider' => $sp,
64  'handle_exceptions' => false,
65  'handle_shutdown' => false,
66  ]);
67 
68  ElggApplication::setInstance($app);
69 
70  $app->bootCore();
71 
72  // turn off system log
73  _elgg_services()->hooks->unregisterHandler('all', 'all', 'system_log_listener');
74  _elgg_services()->hooks->unregisterHandler('log', 'systemlog', 'system_log_default_logger');
75 
76  // disable emails
77  elgg_set_email_transport(new \Zend\Mail\Transport\InMemory());
78 
79  $plugins = (array) $this->option('plugins');
80  if (in_array('all', $plugins)) {
81  $plugins = [];
82  $plugin_entities = elgg_get_plugins('inactive');
83  foreach ($plugin_entities as $plugin_entity) {
84  $plugins[] = $plugin_entity->getID();
85  }
86  }
87 
88  $activated_plugins = [];
89 
90  // activate plugins that are not activated on install
91  foreach ($plugins as $key => $id) {
93  if (!$plugin || $plugin->isActive()) {
94  unset($plugins[$key]);
95  continue;
96  }
97  if ($plugin->activate()) {
99  }
100  }
101 
102  $suite = new TestSuite('Elgg Core Unit Tests');
103 
104  $test_cases = _elgg_services()->hooks->trigger('unit_test', 'system', null, []);
105  foreach ($test_cases as $file) {
106  $filter = $this->option('filter');
107  if ($filter && !preg_match("/$filter/i", $file)) {
108  continue;
109  }
110 
111  if (substr($file, -4, 4) === '.php') {
112  $suite->addFile($file);
113  } else if (class_exists($file)) {
114  if (is_subclass_of($file, \UnitTestCase::class)) {
115  $suite->add($file);
116  } else {
117  elgg_log($file . ' is not a valid simpletest class');
118  }
119  }
120  }
121 
122  $start_time = microtime(true);
123 
124  if (!$this->option('quiet')) {
125  $reporter = new TextReporter();
126  } else {
127  $reporter = new \SimpleReporter();
128  }
129 
130  $result = $suite->Run($reporter);
131 
132  // deactivate plugins that were activated for test suite
133  foreach ($activated_plugins as $key => $id) {
135  $plugin->deactivate();
136  }
137 
138  $formatter = new FormatterHelper();
139  $message = $formatter->formatBlock(sprintf("Time: %.2f seconds, Memory: %.2fMb\n",
140  microtime(true) - $start_time,
141  memory_get_peak_usage() / 1048576.0 // in megabytes
142  ), 'info');
143 
144  $this->output->writeln($message);
145 
146  return $result ? 0 : 1;
147  }
148 }
$plugin
if(!array_key_exists($filename, $text_files)) $file
static elgg()
Get the Elgg codebase path with "/".
Definition: Paths.php:44
elgg-cli simpletest [–config] [–plugins]
if(!is_array($plugin_guids)) $activated_plugins
Definition: activate.php:17
$plugins
Definition: categories.php:3
$id
River item delete action.
Definition: delete.php:6
elgg_set_email_transport(\Zend\Mail\Transport\TransportInterface $mailer)
Replace default email transport.
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:786
Abstract command with some utility methods.
Definition: Command.php:12
elgg_get_plugins($status= 'active')
Returns an ordered list of plugins.
Definition: plugins.php:76
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:55
class
Definition: placeholder.php:21
_elgg_services()
Get the global service provider.
Definition: elgglib.php:1292
$filter
Layout body.
Definition: body.php:18
option($name)
Returns option value.
Namespace for generating in-memory filesystems.
Definition: InMemory.php:14
elgg_get_plugin_from_id($plugin_id)
Returns an object with the path $path.
Definition: plugins.php:28