testInvalidInputFormatThrowsException()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 7
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace Ddeboer\DataImport\Tests\ValueConverter;
4
5
use Ddeboer\DataImport\ValueConverter\DateTimeValueConverter;
6
7
class DateTimeValueConverterTest extends \PHPUnit_Framework_TestCase
8
{
9
    public function testConvertWithoutInputOrOutputFormatReturnsDateTimeInstance()
10
    {
11
        $value = '2011-10-20 13:05';
12
        $converter = new DateTimeValueConverter;
13
        $output = call_user_func($converter, $value);
14
        $this->assertInstanceOf('\DateTime', $output);
15
        $this->assertEquals('13', $output->format('H'));
16
    }
17
18
    public function testConvertWithFormatReturnsDateTimeInstance()
19
    {
20
        $value = '14/10/2008 09:40:20';
21
        $converter = new DateTimeValueConverter('d/m/Y H:i:s');
22
        $output = call_user_func($converter, $value);
23
        $this->assertInstanceOf('\DateTime', $output);
24
        $this->assertEquals('20', $output->format('s'));
25
    }
26
27
    public function testConvertWithInputAndOutputFormatReturnsString()
28
    {
29
        $value = '14/10/2008 09:40:20';
30
        $converter = new DateTimeValueConverter('d/m/Y H:i:s', 'd-M-Y');
31
        $output = call_user_func($converter, $value);
32
        $this->assertEquals('14-Oct-2008', $output);
33
    }
34
35
    public function testConvertWithNoInputStringWithOutputFormatReturnsString()
36
    {
37
        $value = '2011-10-20 13:05';
38
        $converter = new DateTimeValueConverter(null, 'd-M-Y');
39
        $output = call_user_func($converter, $value);
40
        $this->assertEquals('20-Oct-2011', $output);
41
42
    }
43
44
    public function testInvalidInputFormatThrowsException()
45
    {
46
        $value = '14/10/2008 09:40:20';
47
        $converter = new DateTimeValueConverter('d-m-y', 'd-M-Y');
48
        $this->setExpectedException("UnexpectedValueException", "14/10/2008 09:40:20 is not a valid date/time according to format d-m-y");
49
        call_user_func($converter, $value);
50
    }
51
52
    public function testNullIsReturnedIfNullPassed()
53
    {
54
        $converter = new DateTimeValueConverter('d-m-y', 'd-M-Y');
55
        $this->assertNull(call_user_func($converter, null));
56
    }
57
}
58