Completed
Push — master ( ed0837...22809e )
by Dmitry
07:58
created

ConverterTest::testDates()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
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
17
        // carbon test tune
18
        $yesterday = Carbon::parse('yesterday');
19
        Carbon::setTestNow($yesterday);
20
        $this->assertSame(
21
            $yesterday->timestamp,
22
            $this->get(Converter::class)->getTimestamp('now')
23
        );
24
25
        $this->assertSame(
26
            $midnight,
27
            $this->get(Converter::class)->getDate('tomorrow')->timestamp
28
        );
29
    }
30
31
    public function testArrays()
32
    {
33
        $this->validateArray([]);
34
        $this->validateArray(['name' => 'tester']);
35
        $this->validateArray(['name' => null]);
36
        $this->validateArray(['config' => []]);
37
        $this->validateArray(['config' => ['a' => 1]]);
38
        $this->validateArray(['config' => ['a' => null]]);
39
        $this->validateArray(['config' => ['a' => null, 'b' => [1]]]);
40
        $this->validateArray(['config' => ['a' => null, 'b' => [null]]]);
41
42
        $this->assertEquals($this->get(Converter::class)->toObject((object) ['q' => 1]), (object) [
43
            'q' => 1
44
        ]);
45
    }
46
47
    private function validateArray($array)
48
    {
49
        $object = $this->app->get(Converter::class)->toObject($array);
50
        $candidate = $this->app->get(Converter::class)->toArray($object);
51
        $this->assertSame($array, $candidate);
52
    }
53
54
    public function testStrings()
55
    {
56
        $converter = $this->app->get(Converter::class);
57
        $this->assertSame($converter, $this->app->get(Converter::class));
58
59
        $this->assertSame('a', $converter->toCamelCase('a'));
60
        $this->assertSame('A', $converter->toCamelCase('a', true));
61
62
        $this->assertSame('personState', $converter->toCamelCase('person_state'));
63
        $this->assertSame('PersonState', $converter->toCamelCase('person_state', true));
64
65
        $this->assertSame('person_role', $converter->toUnderscore('personRole'));
66
        $this->assertSame('person_role', $converter->toUnderscore('PersonRole'));
67
68
        $this->assertSame('test', $converter->toObject(['test' => 'test'])->test);
69
        $this->assertSame(['gateway', 'audit'], $converter->toObject([
70
            'names' => ['gateway', 'audit']
71
        ])->names);
72
    }
73
}
74