Completed
Push — master ( d0ca26...98fb90 )
by mw
37:57
created

TestEnvironment::getFixturesLocation()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 6
nc 8
nop 2
dl 0
loc 11
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace SMW\Tests;
4
5
use SMW\ApplicationFactory;
6
use SMW\DataValueFactory;
7
use SMW\DeferredCallableUpdate;
8
use SMW\Store;
9
use SMW\Localizer;
10
use SMW\Tests\Utils\UtilityFactory;
11
use SMW\Tests\Utils\Mock\ConfigurableStub;
12
use RuntimeException;
13
14
/**
15
 * @license GNU GPL v2+
16
 * @since 2.4
17
 *
18
 * @author mwjames
19
 */
20
class TestEnvironment {
21
22
	/**
23
	 * @var ApplicationFactory
24
	 */
25
	private $applicationFactory = null;
26
27
	/**
28
	 * @var DataValueFactory
29
	 */
30
	private $dataValueFactory = null;
31
32
	/**
33
	 * @var array
34
	 */
35
	private $configuration = array();
36
37
	/**
38
	 * @since 2.4
39
	 *
40
	 * @param array $configuration
41
	 */
42
	public function __construct( array $configuration = array() ) {
43
		$this->applicationFactory = ApplicationFactory::getInstance();
44
		$this->dataValueFactory = DataValueFactory::getInstance();
45
46
		$this->withConfiguration( $configuration );
47
	}
48
49
	/**
50
	 * @since 2.4
51
	 */
52
	public static function executePendingDeferredUpdates() {
53
		DeferredCallableUpdate::releasePendingUpdates();
54
		\DeferredUpdates::doUpdates();
55
	}
56
57
	/**
58
	 * @since 2.4
59
	 */
60
	public static function clearPendingDeferredUpdates() {
61
		DeferredCallableUpdate::releasePendingUpdates();
62
		\DeferredUpdates::clearPendingUpdates();
63
	}
64
65
	/**
66
	 * @since 2.4
67
	 *
68
	 * @param string $key
69
	 * @param mixed $value
70
	 *
71
	 * @return self
72
	 */
73
	public function addConfiguration( $key, $value ) {
74
		return $this->withConfiguration( array( $key => $value ) );
75
	}
76
77
	/**
78
	 * @since 2.4
79
	 *
80
	 * @param array $configuration
81
	 *
82
	 * @return self
83
	 */
84
	public function withConfiguration( array $configuration = array() ) {
0 ignored issues
show
Coding Style introduced by
withConfiguration uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
85
86
		foreach ( $configuration as $key => $value ) {
87
			$this->configuration[$key] = $GLOBALS[$key];
88
			$GLOBALS[$key] = $value;
89
			$this->applicationFactory->getSettings()->set( $key, $value );
90
		}
91
92
		return $this;
93
	}
94
95
	/**
96
	 * @since 2.4
97
	 *
98
	 * @param string $name
99
	 */
100
	public function resetMediaWikiService( $name ) {
101
102
		// MW 1.27+ (yet 1.27.0.rc has no access to "resetServiceForTesting")
103
		if ( !class_exists( '\MediaWiki\MediaWikiServices' ) || !method_exists( \MediaWiki\MediaWikiServices::getInstance(), 'resetServiceForTesting' ) ) {
104
			return null;
105
		}
106
107
		try {
108
			\MediaWiki\MediaWikiServices::getInstance()->resetServiceForTesting( $name );
109
		} catch( \Exception $e ) {
110
			// Do nothing just avoid a
111
			// MediaWiki\Services\NoSuchServiceException: No such service ...
112
		}
113
114
		return $this;
115
	}
116
117
	/**
118
	 * @since 2.4
119
	 *
120
	 * @param string|array $poolCache
121
	 *
122
	 * @return self
123
	 */
124
	public function resetPoolCacheFor( $poolCache ) {
125
126
		if ( is_array( $poolCache ) ) {
127
			foreach ( $poolCache as $pc ) {
128
				$this->resetPoolCacheFor( $pc );
129
			}
130
		}
131
132
		$this->applicationFactory->getInMemoryPoolCache()->resetPoolCacheFor( $poolCache );
0 ignored issues
show
Bug introduced by
It seems like $poolCache defined by parameter $poolCache on line 124 can also be of type array; however, SMW\InMemoryPoolCache::resetPoolCacheFor() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
133
134
		return $this;
135
	}
136
137
	/**
138
	 * @since 2.4
139
	 *
140
	 * @param string $id
141
	 * @param mixed $object
142
	 *
143
	 * @return self
144
	 */
145
	public function registerObject( $id, $object ) {
146
		$this->applicationFactory->registerObject( $id, $object );
0 ignored issues
show
Documentation introduced by
$object is of type *, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
147
		return $this;
148
	}
149
150
	/**
151
	 * @since 2.4
152
	 */
153
	public function tearDown() {
0 ignored issues
show
Coding Style introduced by
tearDown uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
154
155
		foreach ( $this->configuration as $key => $value ) {
156
			$GLOBALS[$key] = $value;
157
			$this->applicationFactory->getSettings()->set( $key, $value );
158
		}
159
160
		$this->applicationFactory->clear();
161
		$this->dataValueFactory->clear();
162
	}
163
164
	/**
165
	 * @since 2.5
166
	 *
167
	 * @param $originalClassName
168
	 * @param array $configuration
169
	 *
170
	 * @return PHPUnit_Framework_MockObject_MockObject
171
	 */
172
	public function createConfiguredStub( $originalClassName, array $configuration ) {
173
		$configurableStub = new ConfigurableStub();
174
		return $configurableStub->createConfiguredStub( $originalClassName, $configuration );
175
	}
176
177
	/**
178
	 * @since 2.5
179
	 *
180
	 * @param $originalClassName
181
	 * @param array $configuration
182
	 *
183
	 * @return PHPUnit_Framework_MockObject_MockObject
184
	 */
185
	public function createConfiguredAbstractStub( $originalClassName, array $configuration ) {
186
		$configurableStub = new ConfigurableStub();
187
		return $configurableStub->createConfiguredAbstractStub( $originalClassName, $configuration );
188
	}
189
190
	/**
191
	 * @since 2.5
192
	 *
193
	 * @param array $pages
194
	 */
195
	public function flushPages( $pages ) {
196
		$this->getUtilityFactory()->newPageDeleter()->doDeletePoolOfPages( $pages );
197
	}
198
199
	/**
200
	 * @since 2.4
201
	 *
202
	 * @return UtilityFactory
203
	 */
204
	public function getUtilityFactory() {
205
		return UtilityFactory::getInstance();
206
	}
207
208
	/**
209
	 * @since 2.5
210
	 *
211
	 * @param integer $ns
212
	 * @param string $url
0 ignored issues
show
Bug introduced by
There is no parameter named $url. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
213
	 *
214
	 * @return string
215
	 */
216
	public function getLocalizedTextByNamespace( $ns, $text ) {
217
218
		$namespace = Localizer::getInstance()->getNamespaceTextById( $ns );
219
220
		return str_replace(
221
			\MWNamespace::getCanonicalName( $ns ) . ':',
222
			$namespace . ':',
223
			$text
224
		);
225
	}
226
227
	/**
228
	 * @since 2.5
229
	 *
230
	 * @param string $target
231
	 * @param string $file
232
	 *
233
	 * @return string
234
	 * @throws RuntimeException
235
	 */
236
	public function getFixturesLocation( $target = '', $file = '' ) {
237
238
		$fixturesLocation = __DIR__ . '/Fixtures' . ( $target !== '' ? "/{$target}" :  '' ) . ( $file !== '' ? '/' . $file : '' );
239
		$fixturesLocation = str_replace( array( '\\', '/' ), DIRECTORY_SEPARATOR, $fixturesLocation );
240
241
		if ( !file_exists( $fixturesLocation ) && !is_dir( $fixturesLocation ) ) {
242
			throw new RuntimeException( "{$fixturesLocation} does not exist." );
243
		}
244
245
		return $fixturesLocation;
246
	}
247
248
}
249