ConverterTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 87
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testPlural() 0 13 1
A testDates() 0 25 1
A testArrays() 0 17 1
A validateArray() 0 6 1
A testStrings() 0 19 1
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 testPlural()
12
    {
13
        $converter = $this->get(Converter::class);
14
        $forms = ['яблоко', 'яблока', 'яблок'];
15
16
        $this->assertSame($converter->getPluralForm(0, $forms), 'яблок');
17
        $this->assertSame($converter->getPluralForm(1, $forms), 'яблоко');
18
        $this->assertSame($converter->getPluralForm(2, $forms), 'яблока');
19
        $this->assertSame($converter->getPluralForm(5, $forms), 'яблок');
20
        $this->assertSame($converter->getPluralForm(21, $forms), 'яблоко');
21
        $this->assertSame($converter->getPluralForm(23, $forms), 'яблока');
22
        $this->assertSame($converter->getPluralForm(25, $forms), 'яблок');
23
    }
24
25
    public function testDates()
26
    {
27
        $this->assertSame(time(), $this->get(Converter::class)->getTimestamp('now'));
28
29
        $midnight = $this->get(Converter::class)->getTimestamp(date('Ymd'));
30
        $constructed = $this->get(Converter::class)->getDate(+date('Y'), +date('m'), +date('d'));
31
32
        $this->assertSame($midnight, $constructed->timestamp);
33
34
        $toolkited = $this->getDate(+date('Y'), +date('m'), +date('d'));
35
        $this->assertSame($midnight, $toolkited->timestamp);
36
37
        // carbon test tune
38
        $yesterday = Carbon::parse('yesterday');
39
        Carbon::setTestNow($yesterday);
40
        $this->assertSame(
41
            $yesterday->timestamp,
42
            $this->get(Converter::class)->getTimestamp('now')
43
        );
44
45
        $this->assertSame(
46
            $midnight,
47
            $this->get(Converter::class)->getDate('tomorrow')->timestamp
48
        );
49
    }
50
51
    public function testArrays()
52
    {
53
        $this->validateArray([]);
54
        $this->validateArray(['name' => 'tester']);
55
        $this->validateArray(['name' => null]);
56
        $this->validateArray(['config' => []]);
57
        $this->validateArray(['config' => ['a' => 1]]);
58
        $this->validateArray(['config' => ['a' => null]]);
59
        $this->validateArray(['config' => ['a' => null, 'b' => [1]]]);
60
        $this->validateArray(['config' => ['a' => null, 'b' => [null]]]);
61
62
        $this->assertEquals($this->get(Converter::class)->toObject((object) ['q' => 1]), (object) [
63
            'q' => 1
64
        ]);
65
66
        $this->assertTrue($this->app->get(Converter::class)->isTuple((object) []));
67
    }
68
69
    private function validateArray($array)
70
    {
71
        $object = $this->app->get(Converter::class)->toObject($array);
72
        $candidate = $this->app->get(Converter::class)->toArray($object);
73
        $this->assertSame($array, $candidate);
74
    }
75
76
    public function testStrings()
77
    {
78
        $converter = $this->app->get(Converter::class);
79
        $this->assertSame($converter, $this->app->get(Converter::class));
80
81
        $this->assertSame('a', $converter->toCamelCase('a'));
82
        $this->assertSame('A', $converter->toCamelCase('a', true));
83
84
        $this->assertSame('personState', $converter->toCamelCase('person_state'));
85
        $this->assertSame('PersonState', $converter->toCamelCase('person_state', true));
86
87
        $this->assertSame('person_role', $converter->toUnderscore('personRole'));
88
        $this->assertSame('person_role', $converter->toUnderscore('PersonRole'));
89
90
        $this->assertSame('test', $converter->toObject(['test' => 'test'])->test);
91
        $this->assertSame(['gateway', 'audit'], $converter->toObject([
92
            'names' => ['gateway', 'audit']
93
        ])->names);
94
    }
95
}
96