|
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\Argument; |
|
13
|
|
|
|
|
14
|
|
|
use Exception; |
|
15
|
|
|
use PHPUnit_Framework_TestCase; |
|
16
|
|
|
use WBW\Library\Core\Argument\ArgumentInterface; |
|
17
|
|
|
use WBW\Library\Core\Argument\ArgumentValidator; |
|
18
|
|
|
use WBW\Library\Core\Exception\Argument\ArrayArgumentException; |
|
19
|
|
|
use WBW\Library\Core\Exception\Argument\BooleanArgumentException; |
|
20
|
|
|
use WBW\Library\Core\Exception\Argument\DateArgumentException; |
|
21
|
|
|
use WBW\Library\Core\Exception\Argument\DoubleArgumentException; |
|
22
|
|
|
use WBW\Library\Core\Exception\Argument\FloatArgumentException; |
|
23
|
|
|
use WBW\Library\Core\Exception\Argument\IllegalArgumentException; |
|
24
|
|
|
use WBW\Library\Core\Exception\Argument\IntegerArgumentException; |
|
25
|
|
|
use WBW\Library\Core\Exception\Argument\NumberArgumentException; |
|
26
|
|
|
use WBW\Library\Core\Exception\Argument\ObjectArgumentException; |
|
27
|
|
|
use WBW\Library\Core\Exception\Argument\ResourceArgumentException; |
|
28
|
|
|
use WBW\Library\Core\Exception\Argument\StringArgumentException; |
|
29
|
|
|
use WBW\Library\Core\Exception\Argument\TimestampArgumentException; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Argument validator test. |
|
33
|
|
|
* |
|
34
|
|
|
* @author NdC/WBW <https://github.com/webeweb/> |
|
35
|
|
|
* @package WBW\Library\Core\Tests\Argument |
|
36
|
|
|
* @final |
|
37
|
|
|
*/ |
|
38
|
|
|
final class ArgumentValidatorTest extends PHPUnit_Framework_TestCase { |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Tests the isValid() method. |
|
42
|
|
|
* |
|
43
|
|
|
* @return void |
|
44
|
|
|
*/ |
|
45
|
|
|
public function testIsValid() { |
|
46
|
|
|
|
|
47
|
|
|
try { |
|
48
|
|
|
ArgumentValidator::isValid(null, -1); |
|
49
|
|
|
} catch (Exception $ex) { |
|
50
|
|
|
$this->assertInstanceOf(IllegalArgumentException::class, $ex); |
|
51
|
|
|
$this->assertEquals("The type \"-1\" is not implemented", $ex->getMessage()); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$rsc = fopen(getcwd() . "/Tests/Argument/ArgumentValidatorTest.php", "r"); |
|
55
|
|
|
|
|
56
|
|
|
$this->assertTrue(ArgumentValidator::isValid([], ArgumentInterface::TYPE_ARRAY)); |
|
57
|
|
|
$this->assertTrue(ArgumentValidator::isValid(true, ArgumentInterface::TYPE_BOOLEAN)); |
|
58
|
|
|
$this->assertTrue(ArgumentValidator::isValid("2017-10-20", ArgumentInterface::TYPE_DATE)); |
|
59
|
|
|
$this->assertTrue(ArgumentValidator::isValid(0.1, ArgumentInterface::TYPE_DOUBLE)); |
|
60
|
|
|
$this->assertTrue(ArgumentValidator::isValid(1.0, ArgumentInterface::TYPE_FLOAT)); |
|
61
|
|
|
$this->assertTrue(ArgumentValidator::isValid(1, ArgumentInterface::TYPE_INTEGER)); |
|
62
|
|
|
$this->assertTrue(ArgumentValidator::isValid(2, ArgumentInterface::TYPE_NUMBER)); |
|
63
|
|
|
$this->assertTrue(ArgumentValidator::isValid($this, ArgumentInterface::TYPE_OBJECT)); |
|
64
|
|
|
$this->assertTrue(ArgumentValidator::isValid($rsc, ArgumentInterface::TYPE_RESOURCE)); |
|
65
|
|
|
$this->assertTrue(ArgumentValidator::isValid("", ArgumentInterface::TYPE_STRING)); |
|
66
|
|
|
$this->assertTrue(ArgumentValidator::isValid("2017-10-20 15:41:10", ArgumentInterface::TYPE_TIMESTAMP)); |
|
67
|
|
|
|
|
68
|
|
|
try { |
|
69
|
|
|
ArgumentValidator::isValid(null, ArgumentInterface::TYPE_ARRAY); |
|
70
|
|
|
} catch (Exception $ex) { |
|
71
|
|
|
$this->assertInstanceOf(ArrayArgumentException::class, $ex); |
|
72
|
|
|
$this->assertEquals("The argument \"\" is not an array", $ex->getMessage()); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
try { |
|
76
|
|
|
ArgumentValidator::isValid(null, ArgumentInterface::TYPE_BOOLEAN); |
|
77
|
|
|
} catch (Exception $ex) { |
|
78
|
|
|
$this->assertInstanceOf(BooleanArgumentException::class, $ex); |
|
79
|
|
|
$this->assertEquals("The argument \"\" is not a boolean", $ex->getMessage()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
try { |
|
83
|
|
|
ArgumentValidator::isValid(null, ArgumentInterface::TYPE_DATE); |
|
84
|
|
|
} catch (Exception $ex) { |
|
85
|
|
|
$this->assertInstanceOf(DateArgumentException::class, $ex); |
|
86
|
|
|
$this->assertEquals("The argument \"\" is not a date", $ex->getMessage()); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
try { |
|
90
|
|
|
ArgumentValidator::isValid(null, ArgumentInterface::TYPE_DOUBLE); |
|
91
|
|
|
} catch (Exception $ex) { |
|
92
|
|
|
$this->assertInstanceOf(DoubleArgumentException::class, $ex); |
|
93
|
|
|
$this->assertEquals("The argument \"\" is not a double", $ex->getMessage()); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
try { |
|
97
|
|
|
ArgumentValidator::isValid(null, ArgumentInterface::TYPE_FLOAT); |
|
98
|
|
|
} catch (Exception $ex) { |
|
99
|
|
|
$this->assertInstanceOf(FloatArgumentException::class, $ex); |
|
100
|
|
|
$this->assertEquals("The argument \"\" is not a float", $ex->getMessage()); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
try { |
|
104
|
|
|
ArgumentValidator::isValid(null, ArgumentInterface::TYPE_INTEGER); |
|
105
|
|
|
} catch (Exception $ex) { |
|
106
|
|
|
$this->assertInstanceOf(IntegerArgumentException::class, $ex); |
|
107
|
|
|
$this->assertEquals("The argument \"\" is not an integer", $ex->getMessage()); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
try { |
|
111
|
|
|
ArgumentValidator::isValid(null, ArgumentInterface::TYPE_NUMBER); |
|
112
|
|
|
} catch (Exception $ex) { |
|
113
|
|
|
$this->assertInstanceOf(NumberArgumentException::class, $ex); |
|
114
|
|
|
$this->assertEquals("The argument \"\" is not a number", $ex->getMessage()); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
try { |
|
118
|
|
|
ArgumentValidator::isValid(null, ArgumentInterface::TYPE_OBJECT); |
|
119
|
|
|
} catch (Exception $ex) { |
|
120
|
|
|
$this->assertInstanceOf(ObjectArgumentException::class, $ex); |
|
121
|
|
|
$this->assertEquals("The argument \"\" is not an object", $ex->getMessage()); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
try { |
|
125
|
|
|
ArgumentValidator::isValid(null, ArgumentInterface::TYPE_RESOURCE); |
|
126
|
|
|
} catch (Exception $ex) { |
|
127
|
|
|
$this->assertInstanceOf(ResourceArgumentException::class, $ex); |
|
128
|
|
|
$this->assertEquals("The argument \"\" is not a resource", $ex->getMessage()); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
try { |
|
132
|
|
|
ArgumentValidator::isValid(null, ArgumentInterface::TYPE_STRING); |
|
133
|
|
|
} catch (Exception $ex) { |
|
134
|
|
|
$this->assertInstanceOf(StringArgumentException::class, $ex); |
|
135
|
|
|
$this->assertEquals("The argument \"\" is not a string", $ex->getMessage()); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
try { |
|
139
|
|
|
ArgumentValidator::isValid(null, ArgumentInterface::TYPE_TIMESTAMP); |
|
140
|
|
|
} catch (Exception $ex) { |
|
141
|
|
|
$this->assertInstanceOf(TimestampArgumentException::class, $ex); |
|
142
|
|
|
$this->assertEquals("The argument \"\" is not a timestamp", $ex->getMessage()); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
} |
|
147
|
|
|
|