Completed
Push — master ( 03e47f...db33ac )
by WEBEWEB
06:42 queued 01:35
created

IntegerUtilityTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 54
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testParseBoolean() 0 6 1
B testParseString() 0 33 5
1
<?php
2
3
/**
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2017 NdC/WBW
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Library\Core\Tests\Utility;
13
14
use Exception;
15
use PHPUnit_Framework_TestCase;
16
use WBW\Library\Core\Exception\Argument\IntegerArgumentException;
17
use WBW\Library\Core\Utility\IntegerUtility;
18
19
/**
20
 * Integer utility test.
21
 *
22
 * @author NdC/WBW <https://github.com/webeweb/>
23
 * @package WBW\Library\Core\Tests\Utility
24
 * @final
25
 */
26
final class IntegerUtilityTest extends PHPUnit_Framework_TestCase {
27
28
	/**
29
	 * Tests the parseBoolean() method.
30
	 *
31
	 * @return void
32
	 */
33
	public function testParseBoolean() {
34
35
		$this->assertEquals(0, IntegerUtility::parseBoolean(null));
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a boolean.

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...
36
		$this->assertEquals(0, IntegerUtility::parseBoolean(false));
37
		$this->assertEquals(1, IntegerUtility::parseBoolean(true));
38
	}
39
40
	/**
41
	 * Tests the parseString() method.
42
	 *
43
	 * @return void
44
	 */
45
	public function testParseString() {
46
47
		try {
48
			IntegerUtility::parseString("exception");
49
		} catch (Exception $ex) {
50
			$this->assertInstanceOf(IntegerArgumentException::class, $ex);
51
			$this->assertEquals("The argument \"exception\" is not an integer", $ex->getMessage());
52
		}
53
54
		try {
55
			IntegerUtility::parseString("1A");
56
		} catch (Exception $ex) {
57
			$this->assertInstanceOf(IntegerArgumentException::class, $ex);
58
			$this->assertEquals("The argument \"1A\" is not an integer", $ex->getMessage());
59
		}
60
61
		try {
62
			IntegerUtility::parseString("1.");
63
		} catch (Exception $ex) {
64
			$this->assertInstanceOf(IntegerArgumentException::class, $ex);
65
			$this->assertEquals("The argument \"1.\" is not an integer", $ex->getMessage());
66
		}
67
68
		try {
69
			IntegerUtility::parseString("1.0");
70
		} catch (Exception $ex) {
71
			$this->assertInstanceOf(IntegerArgumentException::class, $ex);
72
			$this->assertEquals("The argument \"1.0\" is not an integer", $ex->getMessage());
73
		}
74
75
		$this->assertEquals(null, IntegerUtility::parseString(null));
76
		$this->assertEquals(1, IntegerUtility::parseString("1"));
77
	}
78
79
}
80