Completed
Push — master ( 184377...e7ee0f )
by Dmitry
05:03
created

ConverterTest::testDates()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 25
rs 8.8571
c 1
b 0
f 0
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
3
namespace Test;
4
5
use Carbon\Carbon;
6
use Basis\Converter;
7
use Basis\Test;
8
9
class ConverterTest extends Test
10
{
11
    public function testDates()
12
    {
13
        $this->assertSame(time(), $this->get(Converter::class)->getTimestamp('now'));
14
15
        $midnight = $this->get(Converter::class)->getTimestamp(date('Ymd'));
16
        $constructed = $this->get(Converter::class)->getDate(+date('Y'), +date('m'), +date('d'));
17
18
        $this->assertSame($midnight, $constructed->timestamp);
19
20
        $toolkited = $this->getDate(+date('Y'), +date('m'), +date('d'));
21
        $this->assertSame($midnight, $toolkited->timestamp);
22
23
        // carbon test tune
24
        $yesterday = Carbon::parse('yesterday');
25
        Carbon::setTestNow($yesterday);
26
        $this->assertSame(
27
            $yesterday->timestamp,
28
            $this->get(Converter::class)->getTimestamp('now')
29
        );
30
31
        $this->assertSame(
32
            $midnight,
33
            $this->get(Converter::class)->getDate('tomorrow')->timestamp
34
        );
35
    }
36
37
    public function testArrays()
38
    {
39
        $this->validateArray([]);
40
        $this->validateArray(['name' => 'tester']);
41
        $this->validateArray(['name' => null]);
42
        $this->validateArray(['config' => []]);
43
        $this->validateArray(['config' => ['a' => 1]]);
44
        $this->validateArray(['config' => ['a' => null]]);
45
        $this->validateArray(['config' => ['a' => null, 'b' => [1]]]);
46
        $this->validateArray(['config' => ['a' => null, 'b' => [null]]]);
47
48
        $this->assertEquals($this->get(Converter::class)->toObject((object) ['q' => 1]), (object) [
49
            'q' => 1
50
        ]);
51
    }
52
53
    private function validateArray($array)
54
    {
55
        $object = $this->app->get(Converter::class)->toObject($array);
56
        $candidate = $this->app->get(Converter::class)->toArray($object);
57
        $this->assertSame($array, $candidate);
58
    }
59
60
    public function testStrings()
61
    {
62
        $converter = $this->app->get(Converter::class);
63
        $this->assertSame($converter, $this->app->get(Converter::class));
64
65
        $this->assertSame('a', $converter->toCamelCase('a'));
66
        $this->assertSame('A', $converter->toCamelCase('a', true));
67
68
        $this->assertSame('personState', $converter->toCamelCase('person_state'));
69
        $this->assertSame('PersonState', $converter->toCamelCase('person_state', true));
70
71
        $this->assertSame('person_role', $converter->toUnderscore('personRole'));
72
        $this->assertSame('person_role', $converter->toUnderscore('PersonRole'));
73
74
        $this->assertSame('test', $converter->toObject(['test' => 'test'])->test);
75
        $this->assertSame(['gateway', 'audit'], $converter->toObject([
76
            'names' => ['gateway', 'audit']
77
        ])->names);
78
    }
79
}
80