Issues (22)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

tests/DataValues/UnboundedQuantityValueTest.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace DataValues\Tests;
4
5
use DataValues\DecimalValue;
6
use DataValues\IllegalValueException;
7
use DataValues\QuantityValue;
8
use DataValues\UnboundedQuantityValue;
9
10
/**
11
 * @covers DataValues\UnboundedQuantityValue
12
 *
13
 * @group DataValue
14
 * @group DataValueExtensions
15
 *
16
 * @license GPL-2.0+
17
 * @author Daniel Kinzler
18
 */
19
class UnboundedQuantityValueTest extends DataValueTest {
20
21
	/**
22
	 * @see DataValueTest::getClass
23
	 *
24
	 * @return string
25
	 */
26
	public function getClass() {
27
		return UnboundedQuantityValue::class;
28
	}
29
30
	public function validConstructorArgumentsProvider() {
31
		$argLists = [];
32
33
		$argLists[] = [ new DecimalValue( '+42' ), '1' ];
34
		$argLists[] = [ new DecimalValue( '+0.01' ), '1' ];
35
		$argLists[] = [ new DecimalValue( '-0.5' ), '1' ];
36
37
		return $argLists;
38
	}
39
40
	public function invalidConstructorArgumentsProvider() {
41
		$argLists = [];
42
43
		$argLists[] = [ new DecimalValue( '+0' ), '' ];
44
		$argLists[] = [ new DecimalValue( '+0' ), 1 ];
45
46
		return $argLists;
47
	}
48
49
	/**
50
	 * @dataProvider instanceProvider
51
	 */
52
	public function testGetValue( UnboundedQuantityValue $quantity, array $arguments ) {
0 ignored issues
show
The parameter $arguments is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
53
		$this->assertSame( $quantity, $quantity->getValue() );
54
	}
55
56
	/**
57
	 * @dataProvider instanceProvider
58
	 */
59
	public function testGetAmount( UnboundedQuantityValue $quantity, array $arguments ) {
60
		$this->assertSame( $arguments[0], $quantity->getAmount() );
61
	}
62
63
	/**
64
	 * @dataProvider instanceProvider
65
	 */
66
	public function testGetUnit( UnboundedQuantityValue $quantity, array $arguments ) {
67
		$this->assertSame( $arguments[1], $quantity->getUnit() );
68
	}
69
70
	/**
71
	 * @dataProvider newFromNumberProvider
72
	 */
73
	public function testNewFromNumber( $amount, $unit, UnboundedQuantityValue $expected ) {
74
		$quantity = UnboundedQuantityValue::newFromNumber( $amount, $unit );
75
76
		$this->assertEquals( $expected->getAmount()->getValue(), $quantity->getAmount()->getValue() );
77
	}
78
79
	public function newFromNumberProvider() {
80
		return [
81
			[
82
				42, '1',
83
				new UnboundedQuantityValue( new DecimalValue( '+42' ), '1' )
84
			],
85
			[
86
				-0.05, '1',
87
				new UnboundedQuantityValue( new DecimalValue( '-0.05' ), '1' )
88
			],
89
			[
90
				0, 'm',
91
				new UnboundedQuantityValue( new DecimalValue( '+0' ), 'm' )
92
			],
93
			[
94
				'+23', '1',
95
				new UnboundedQuantityValue( new DecimalValue( '+23' ), '1' )
96
			],
97
			[
98
				'+42', '1',
99
				new UnboundedQuantityValue( new DecimalValue( '+42' ), '1' )
100
			],
101
			[
102
				'-0.05', 'm',
103
				new UnboundedQuantityValue( new DecimalValue( '-0.05' ), 'm' )
104
			],
105
			[
106
				new DecimalValue( '+42' ), '1',
107
				new UnboundedQuantityValue( new DecimalValue( '+42' ), '1' )
108
			],
109
		];
110
	}
111
112
	/**
113
	 * @dataProvider validArraySerializationProvider
114
	 */
115
	public function testNewFromArray( $data, UnboundedQuantityValue $expected ) {
116
		$value = UnboundedQuantityValue::newFromArray( $data );
0 ignored issues
show
Deprecated Code introduced by
The method DataValues\UnboundedQuantityValue::newFromArray() has been deprecated with message: since 0.8.3. Static DataValue::newFromArray constructors like this are underspecified (not in the DataValue interface), and misleadingly named (should be named newFromArrayValue). Instead, use DataValue builder callbacks in @see DataValueDeserializer.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
117
		$this->assertTrue( $expected->equals( $value ), $value . ' should equal ' . $expected );
118
	}
119
120
	public function validArraySerializationProvider() {
121
		return [
122
			'unbounded' => [
123
				[
124
					'amount' => '+2',
125
					'unit' => '1',
126
				],
127
				UnboundedQuantityValue::newFromNumber( '+2', '1' )
128
			],
129
			'unbounded with existing array keys' => [
130
				[
131
					'amount' => '+2',
132
					'unit' => '1',
133
					'upperBound' => null,
134
					'lowerBound' => null,
135
				],
136
				UnboundedQuantityValue::newFromNumber( '+2', '1' )
137
			],
138
			'with-extra' => [
139
				[
140
					'amount' => '+2',
141
					'unit' => '1',
142
					'upperBound' => '+2.5',
143
					'lowerBound' => '+1.5',
144
				],
145
				QuantityValue::newFromNumber( '+2', '1', '+2.5', '+1.5' )
146
			],
147
		];
148
	}
149
150
	/**
151
	 * @dataProvider invalidArraySerializationProvider
152
	 */
153
	public function testNewFromArray_failure( $data ) {
154
		$this->setExpectedException( IllegalValueException::class );
155
		UnboundedQuantityValue::newFromArray( $data );
0 ignored issues
show
Deprecated Code introduced by
The method DataValues\UnboundedQuantityValue::newFromArray() has been deprecated with message: since 0.8.3. Static DataValue::newFromArray constructors like this are underspecified (not in the DataValue interface), and misleadingly named (should be named newFromArrayValue). Instead, use DataValue builder callbacks in @see DataValueDeserializer.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
156
	}
157
158
	public function invalidArraySerializationProvider() {
159
		return [
160
			'not an array (string)' => [
161
				'foo'
162
			],
163
			'not an array (int)' => [
164
				303
165
			],
166
			'not an array (object)' => [
167
				new \stdClass()
168
			],
169
			'no-amount' => [
170
				[
171
					'unit' => '1',
172
				]
173
			],
174
			'no-unit' => [
175
				[
176
					'amount' => '+2',
177
				]
178
			],
179
			'no-upperBound' => [
180
				[
181
					'amount' => '+2',
182
					'unit' => '1',
183
					'lowerBound' => '+1.5',
184
				]
185
			],
186
			'no-lowerBound' => [
187
				[
188
					'amount' => '+2',
189
					'unit' => '1',
190
					'upperBound' => '+2.5',
191
				]
192
			],
193
			'bad-amount' => [
194
				[
195
					'amount' => 'x',
196
					'unit' => '1',
197
				]
198
			],
199
			'bad-upperBound' => [
200
				[
201
					'amount' => '+2',
202
					'unit' => '1',
203
					'upperBound' => 'x',
204
					'lowerBound' => '+1.5',
205
				]
206
			],
207
			'bad-lowerBound' => [
208
				[
209
					'amount' => '+2',
210
					'unit' => '1',
211
					'upperBound' => '+2.5',
212
					'lowerBound' => 'x',
213
				]
214
			],
215
		];
216
	}
217
218
	/**
219
	 * @see https://phabricator.wikimedia.org/T110728
220
	 * @see http://www.regular-expressions.info/anchors.html#realend
221
	 */
222
	public function testTrailingNewlineRobustness() {
223
		$value = UnboundedQuantityValue::newFromArray( [
0 ignored issues
show
Deprecated Code introduced by
The method DataValues\UnboundedQuantityValue::newFromArray() has been deprecated with message: since 0.8.3. Static DataValue::newFromArray constructors like this are underspecified (not in the DataValue interface), and misleadingly named (should be named newFromArrayValue). Instead, use DataValue builder callbacks in @see DataValueDeserializer.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
224
			'amount' => "-0.0\n",
225
			'unit' => "1\n",
226
		] );
227
228
		$this->assertSame( [
229
			'amount' => '+0.0',
230
			'unit' => "1\n",
231
		], $value->getArrayValue() );
232
	}
233
234
	/**
235
	 * @dataProvider instanceProvider
236
	 */
237
	public function testGetSortKey( UnboundedQuantityValue $quantity ) {
238
		$this->assertSame( $quantity->getAmount()->getValueFloat(), $quantity->getSortKey() );
239
	}
240
241
	/**
242
	 * @dataProvider transformProvider
243
	 */
244
	public function testTransform( UnboundedQuantityValue $quantity, $transformation, UnboundedQuantityValue $expected ) {
245
		$args = func_get_args();
246
		$extraArgs = array_slice( $args, 3 );
247
248
		$call = [ $quantity, 'transform' ];
249
		$callArgs = array_merge( [ 'x', $transformation ], $extraArgs );
250
		$actual = call_user_func_array( $call, $callArgs );
251
252
		$this->assertSame( 'x', $actual->getUnit() );
253
		$this->assertEquals( $expected->getAmount()->getValue(), $actual->getAmount()->getValue(), 'value' );
254
	}
255
256
	public function transformProvider() {
257
		$identity = function ( DecimalValue $value ) {
258
			return $value;
259
		};
260
261
		$square = function ( DecimalValue $value ) {
262
			$v = $value->getValueFloat();
263
			return new DecimalValue( $v * $v * $v );
264
		};
265
266
		$scale = function ( DecimalValue $value, $factor ) {
267
			return new DecimalValue( $value->getValueFloat() * $factor );
268
		};
269
270
		return [
271
			0 => [
272
				UnboundedQuantityValue::newFromNumber( '+10', '1' ),
273
				$identity,
274
				UnboundedQuantityValue::newFromNumber( '+10', '?' )
275
			],
276
			1 => [
277
				UnboundedQuantityValue::newFromNumber( '-0.5', '1' ),
278
				$identity,
279
				UnboundedQuantityValue::newFromNumber( '-0.5', '?' )
280
			],
281
			2 => [
282
				UnboundedQuantityValue::newFromNumber( '+0', '1' ),
283
				$square,
284
				UnboundedQuantityValue::newFromNumber( '+0', '?' )
285
			],
286
			3 => [
287
				UnboundedQuantityValue::newFromNumber( '+10', '1' ),
288
				$square,
289
				UnboundedQuantityValue::newFromNumber( '+1000', '?' )
290
			],
291
			4 => [
292
				UnboundedQuantityValue::newFromNumber( '+0.5', '1' ),
293
				$scale,
294
				UnboundedQuantityValue::newFromNumber( '+0.25', '?' ),
295
				0.5
296
			],
297
298
			// note: absolutely exact values require conversion with infinite precision!
299
			10 => [
300
				UnboundedQuantityValue::newFromNumber( '+100', '1' ),
301
				$scale,
302
				UnboundedQuantityValue::newFromNumber( '+12825', '?' ),
303
				128.25
304
			],
305
			13 => [
306
				UnboundedQuantityValue::newFromNumber( '+100', '1' ),
307
				$scale,
308
				UnboundedQuantityValue::newFromNumber( '+333.33', '?' ),
309
				3.3333
310
			],
311
		];
312
	}
313
314
}
315