Completed
Push — master ( 487ef8...69e5c8 )
by mw
04:55 queued 04:46
created

TestEnvironment::resetMediaWikiService()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 5
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 11
rs 9.4285
1
<?php
2
3
namespace SMW\Tests;
4
5
use SMW\ApplicationFactory;
6
use SMW\DataValueFactory;
7
use SMW\Store;
8
use SMW\Tests\Utils\UtilityFactory;
9
10
/**
11
 * @license GNU GPL v2+
12
 * @since 2.4
13
 *
14
 * @author mwjames
15
 */
16
class TestEnvironment {
17
18
	/**
19
	 * @var ApplicationFactory
20
	 */
21
	private $applicationFactory = null;
22
23
	/**
24
	 * @var DataValueFactory
25
	 */
26
	private $dataValueFactory = null;
27
28
	/**
29
	 * @var array
30
	 */
31
	private $configuration = array();
32
33
	/**
34
	 * @since 2.4
35
	 *
36
	 * @param array $configuration
37
	 */
38
	public function __construct( array $configuration = array() ) {
39
		$this->applicationFactory = ApplicationFactory::getInstance();
40
		$this->dataValueFactory = DataValueFactory::getInstance();
41
42
		$this->withConfiguration( $configuration );
43
	}
44
45
	/**
46
	 * @since 2.4
47
	 */
48
	public static function executePendingDeferredUpdates() {
49
		\DeferredUpdates::doUpdates();
50
	}
51
52
	/**
53
	 * @since 2.4
54
	 */
55
	public static function clearPendingDeferredUpdates() {
56
		\DeferredUpdates::clearPendingUpdates();
57
	}
58
59
	/**
60
	 * @since 2.4
61
	 *
62
	 * @param string $key
63
	 * @param mixed $value
64
	 *
65
	 * @return self
66
	 */
67
	public function addConfiguration( $key, $value ) {
68
		return $this->withConfiguration( array( $key => $value ) );
69
	}
70
71
	/**
72
	 * @since 2.4
73
	 *
74
	 * @param array $configuration
75
	 *
76
	 * @return self
77
	 */
78
	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...
79
80
		foreach ( $configuration as $key => $value ) {
81
			$this->configuration[$key] = $GLOBALS[$key];
82
			$GLOBALS[$key] = $value;
83
			$this->applicationFactory->getSettings()->set( $key, $value );
84
		}
85
86
		return $this;
87
	}
88
89
	/**
90
	 * @since 2.4
91
	 *
92
	 * @param string $name
93
	 */
94
	public function resetMediaWikiService( $name ) {
95
96
		// MW 1.27+
97
		if ( !class_exists( '\MediaWiki\MediaWikiServices' ) ) {
98
			return null;
99
		}
100
101
		\MediaWiki\MediaWikiServices::getInstance()->resetServiceForTesting( $name );
102
103
		return $this;
104
	}
105
106
	/**
107
	 * @since 2.4
108
	 *
109
	 * @param string $poolCache
110
	 *
111
	 * @return self
112
	 */
113
	public function resetPoolCacheFor( $poolCache ) {
114
		$this->applicationFactory->getInMemoryPoolCache()->resetPoolCacheFor( $poolCache );
115
		return $this;
116
	}
117
118
	/**
119
	 * @since 2.4
120
	 *
121
	 * @param string $id
122
	 * @param mixed $object
123
	 *
124
	 * @return self
125
	 */
126
	public function registerObject( $id, $object ) {
127
		$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...
128
		return $this;
129
	}
130
131
	/**
132
	 * @since 2.4
133
	 */
134
	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...
135
136
		foreach ( $this->configuration as $key => $value ) {
137
			$GLOBALS[$key] = $value;
138
			$this->applicationFactory->getSettings()->set( $key, $value );
139
		}
140
141
		$this->applicationFactory->clear();
142
		$this->dataValueFactory->clear();
143
	}
144
145
	/**
146
	 * @since 2.4
147
	 *
148
	 * @return UtilityFactory
149
	 */
150
	public function getUtilityFactory() {
151
		return UtilityFactory::getInstance();
152
	}
153
154
}
155