summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/tests/phpunit/Utils/Mock/MockObjectBuilder.php
blob: 473c9602b753385c0d94276e8a732541fd2f6afb (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php

namespace SMW\Tests\Utils\Mock;

use InvalidArgumentException;
use OutOfBoundsException;
use SMW\ObjectDictionary;
use SMW\Options;

/**
 * @codeCoverageIgnore
 *
 * MockObject builder provides methods that are being used by the mock repository
 * to define and create a mock object
 *
 * $title = new MockObjectBuilder()
 * $title->newObject( 'Foo', array(
 * 	'Bar' => ...
 * ) )
 *
 *
 * @group SMW
 * @group SMWExtension
 *
 * @licence GNU GPL v2+
 * @since 1.9
 *
 * @author mwjames
 */
class MockObjectBuilder extends \PHPUnit_Framework_TestCase {

	/** @var ObjectDictionary */
	protected $configuration;

	/** @var MockObjectRepository */
	protected $repository = [];

	/**
	 * @since  1.9
	 *
	 * @param MockObjectRepository|null $repository
	 */
	public function __construct( MockObjectRepository $repository = null ) {

		if ( $repository === null ) {
			$repository = new CoreMockObjectRepository();
		}

		$this->registerRepository( $repository );
	}

	/**
	 * @since 1.9
	 *
	 * @param MockObjectRepository $repository
	 */
	public function registerRepository( MockObjectRepository $repository ) {
		$this->repository[] = $repository;
	}

	/**
	 * Helper method that stores configuration settings
	 *
	 * @since 1.9
	 *
	 * @param $objectName
	 * @param $objectArguments
	 *
	 * @return mixed
	 */
	public function newObject( $objectName, $objectArguments = [] ) {

		if ( !is_string( $objectName ) ) {
			throw new InvalidArgumentException( "Object name is not a string" );
		}

		if ( $objectArguments !== [] && !is_array( $objectArguments ) ) {
			throw new InvalidArgumentException( "Arguments are not an array type" );
		}

		$repository = $this->findRepositoryForObject( $objectName );

		if ( !$repository instanceof MockObjectRepository ) {
			throw new OutOfBoundsException( "{$objectName} method doesn't exists" );
		}

		$repository->registerBuilder( $this );
		$this->setupConfiguration( $objectArguments );

		return $repository->{$objectName}();
	}

	/**
	 * Returns invoked configuration keys
	 *
	 * @since 1.9
	 *
	 * @return array
	 */
	public function getInvokedMethods() {
		return array_keys( $this->configuration->getOptions() );
	}

	/**
	 * Helper method that returns a random string
	 *
	 * @since 1.9
	 *
	 * @param $length
	 * @param $prefix identify a specific random string during testing
	 *
	 * @return string
	 */
	public function newRandomString( $length = 10, $prefix = null ) {
		return $prefix . ( $prefix ? '-' : '' ) . substr( str_shuffle( "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ), 0, $length );
	}

	/**
	 * Whether the configuration is known
	 *
	 * @since 1.9
	 *
	 * @param $key
	 *
	 * @return boolean
	 */
	public function hasValue( $key ) {
		return $this->configuration->has( $key );
	}

	/**
	 * Sets value
	 *
	 * @since 1.9
	 *
	 * @param $key
	 * @param $default
	 *
	 * @return mixed|null
	 */
	public function setValue( $key, $default = null ) {
		return $this->configuration->has( $key ) ? $this->configuration->get( $key ) : $default;
	}

	/**
	 * Determine callback function otherwise return simple value
	 *
	 * @since 1.9
	 *
	 * @param $key
	 * @param $default
	 *
	 * @return mixed|null
	 */
	public function setCallback( $key, $default = null ) {
		return is_callable( $this->setValue( $key ) ) ? $this->returnCallback( $this->setValue( $key ) ) : $this->returnValue( $this->setValue( $key, $default ) );
	}

	/**
	 * @since 1.9
	 *
	 * @param $objectName
	 *
	 * @return MockObjectRepository|null
	 */
	protected function findRepositoryForObject( $objectName ) {

		foreach ( $this->repository as $repository ) {
			if ( method_exists( $repository, $objectName ) ) {
				return $repository;
			}
		}

		return null;
	}

	/**
	 * @since 1.9
	 *
	 * @param $config
	 */
	protected function setupConfiguration( $config ) {

		$configuration = new Options( $config );

		if ( $this->configuration instanceof Options ) {
			return $this->configuration = new Options(
				array_merge( $this->configuration->getOptions(), $configuration->getOptions() )
			);
		}

		$this->configuration = $configuration;
	}

}