|
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 DateTime; |
|
15
|
|
|
use Exception; |
|
16
|
|
|
use PHPUnit_Framework_TestCase; |
|
17
|
|
|
use WBW\Library\Core\Argument\ArgumentConverter; |
|
18
|
|
|
use WBW\Library\Core\Argument\ArgumentInterface; |
|
19
|
|
|
use WBW\Library\Core\Exception\Argument\DateArgumentException; |
|
20
|
|
|
use WBW\Library\Core\Exception\Argument\IllegalArgumentException; |
|
21
|
|
|
use WBW\Library\Core\Exception\Argument\TimestampArgumentException; |
|
22
|
|
|
use WBW\Library\Core\Exception\Pointer\NullPointerException; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Argument converter test. |
|
26
|
|
|
* |
|
27
|
|
|
* @author NdC/WBW <https://github.com/webeweb/> |
|
28
|
|
|
* @package WBW\Library\Core\Tests\Argument |
|
29
|
|
|
* @final |
|
30
|
|
|
*/ |
|
31
|
|
|
final class ArgumentConverterTest extends PHPUnit_Framework_TestCase { |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Tests the convert() method. |
|
35
|
|
|
* |
|
36
|
|
|
* @return void |
|
37
|
|
|
*/ |
|
38
|
|
|
public function testConvert() { |
|
39
|
|
|
|
|
40
|
|
|
try { |
|
41
|
|
|
ArgumentConverter::convert(null, ArgumentInterface::TYPE_ARRAY); |
|
42
|
|
|
} catch (Exception $ex) { |
|
43
|
|
|
$this->assertInstanceOf(IllegalArgumentException::class, $ex); |
|
44
|
|
|
$this->assertEquals("The type \"63\" is not implemented", $ex->getMessage()); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
try { |
|
48
|
|
|
ArgumentConverter::convert(null, ArgumentInterface::TYPE_DATE); |
|
49
|
|
|
} catch (Exception $ex) { |
|
50
|
|
|
$this->assertInstanceOf(NullPointerException::class, $ex); |
|
51
|
|
|
$this->assertEquals("The date format is null", $ex->getMessage()); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
try { |
|
55
|
|
|
ArgumentConverter::convert("exception", ArgumentInterface::TYPE_DATE, "Y-m-d"); |
|
56
|
|
|
} catch (Exception $ex) { |
|
57
|
|
|
$this->assertInstanceOf(DateArgumentException::class, $ex); |
|
58
|
|
|
$this->assertEquals("The argument \"exception\" is not a date", $ex->getMessage()); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
try { |
|
62
|
|
|
ArgumentConverter::convert(null, ArgumentInterface::TYPE_NUMBER); |
|
63
|
|
|
} catch (Exception $ex) { |
|
64
|
|
|
$this->assertInstanceOf(IllegalArgumentException::class, $ex); |
|
65
|
|
|
$this->assertEquals("The type \"73\" is not implemented", $ex->getMessage()); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
try { |
|
69
|
|
|
ArgumentConverter::convert(null, ArgumentInterface::TYPE_OBJECT); |
|
70
|
|
|
} catch (Exception $ex) { |
|
71
|
|
|
$this->assertInstanceOf(IllegalArgumentException::class, $ex); |
|
72
|
|
|
$this->assertEquals("The type \"55\" is not implemented", $ex->getMessage()); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
try { |
|
76
|
|
|
ArgumentConverter::convert(null, ArgumentInterface::TYPE_RESOURCE); |
|
77
|
|
|
} catch (Exception $ex) { |
|
78
|
|
|
$this->assertInstanceOf(IllegalArgumentException::class, $ex); |
|
79
|
|
|
$this->assertEquals("The type \"104\" is not implemented", $ex->getMessage()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
try { |
|
83
|
|
|
ArgumentConverter::convert(null, ArgumentInterface::TYPE_TIMESTAMP); |
|
84
|
|
|
} catch (Exception $ex) { |
|
85
|
|
|
$this->assertInstanceOf(NullPointerException::class, $ex); |
|
86
|
|
|
$this->assertEquals("The datetime format is null", $ex->getMessage()); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
try { |
|
90
|
|
|
ArgumentConverter::convert("exception", ArgumentInterface::TYPE_TIMESTAMP, "Y-m-d H:m:s"); |
|
91
|
|
|
} catch (Exception $ex) { |
|
92
|
|
|
$this->assertInstanceOf(TimestampArgumentException::class, $ex); |
|
93
|
|
|
$this->assertEquals("The argument \"exception\" is not a timestamp", $ex->getMessage()); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$this->assertEquals(true, ArgumentConverter::convert("1", ArgumentInterface::TYPE_BOOLEAN)); |
|
97
|
|
|
$this->assertInstanceOf(DateTime::class, ArgumentConverter::convert("2017-11-27", ArgumentInterface::TYPE_DATE, "Y-m-d")); |
|
98
|
|
|
$this->assertEquals(1, ArgumentConverter::convert("1", ArgumentInterface::TYPE_DOUBLE)); |
|
99
|
|
|
$this->assertEquals(1, ArgumentConverter::convert("1", ArgumentInterface::TYPE_FLOAT)); |
|
100
|
|
|
$this->assertEquals(1, ArgumentConverter::convert("1", ArgumentInterface::TYPE_INTEGER)); |
|
101
|
|
|
$this->assertEquals("1", ArgumentConverter::convert("1", ArgumentInterface::TYPE_STRING)); |
|
102
|
|
|
$this->assertInstanceOf(DateTime::class, ArgumentConverter::convert("2017-11-27 11:20:00", ArgumentInterface::TYPE_TIMESTAMP, "Y-m-d H:m:s")); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|