summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/tests/phpunit/Utils/Runners/MaintenanceRunner.php
blob: 17741cea566afa6896ddc08719623ac7a9e3b278 (plain)
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
<?php

namespace SMW\Tests\Utils\Runners;

use DomainException;
use Onoi\MessageReporter\MessageReporterAwareTrait;
use RuntimeException;
use SMW\ApplicationFactory;

/**
 * Running maintenance scripts via phpunit is not really possible but instead
 * this class allows to execute script related classes that are equivalent to:
 * `php rebuildData.php --< myOptions >`
 *
 * @group SMW
 * @group SMWExtension
 *
 * @license GNU GPL v2+
 * @since 1.9.2
 *
 * @author mwjames
 */
class MaintenanceRunner {

	use MessageReporterAwareTrait;

	protected $maintenanceClass = null;
	protected $options = [];
	protected $output = null;
	protected $quiet = false;

	/**
	 * @since 1.9.2
	 *
	 * @param string $maintenanceClass
	 * @param array $options
	 */
	public function __construct( $maintenanceClass, $options = [] ) {
		$this->maintenanceClass = $maintenanceClass;
		$this->options = $options;
	}

	/**
	 * @since 1.9.2
	 *
	 * @param array $options
	 *
	 * @return MaintenanceRunner
	 */
	public function setOptions( array $options ) {
		$this->options = $options;
		return $this;
	}

	/**
	 * @since 1.9.2
	 *
	 * @return MaintenanceRunner
	 */
	public function setQuiet() {
		$this->options['quiet'] = true;
		return $this;
	}

	/**
	 * @since 1.9.2
	 *
	 * @return boolean
	 * @throws RuntimeException
	 * @throws DomainException
	 */
	public function run() {

		if ( !class_exists( $this->maintenanceClass ) ) {
			throw new RuntimeException( "Expected a valid {$this->maintenanceClass} class" );
		}

		$obLevel = ob_get_level();

		// Avoid outdated reference to invoked store instance
		ApplicationFactory::getInstance()->clear();
		$maintenance = new $this->maintenanceClass;

		if ( !( $maintenance instanceof \Maintenance ) ) {
			throw new DomainException( "Expected a Maintenance instance" );
		}

		// isset/ null
		if ( isset( $this->options['quiet'] ) && $this->options['quiet'] === false ) {
			unset( $this->options['quiet'] );
		}

		$maintenance->loadParamsAndArgs(
			$this->maintenanceClass,
			$this->options
		);

		if ( $this->messageReporter !== null && method_exists( $maintenance, 'setMessageReporter' ) ) {
			$maintenance->setMessageReporter( $this->messageReporter );
		}

		ob_start();
		$result = $maintenance->execute();
		$this->output = ob_get_contents();

		while ( ob_get_level() > $obLevel ) {
			ob_end_clean();
		}

		return $result;
	}

	/**
	 * @since 1.9.2
	 *
	 * @return string
	 */
	public function getOutput() {
		return $this->output;
	}

}

// FIXME SemanticGlossary usage
class_alias( 'SMW\Tests\Utils\Runners\MaintenanceRunner', 'SMW\Tests\Util\MaintenanceRunner' );