StringCasterTest::testCastsAsInteger()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 15
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace RemotelyLiving\PHPEnv\Tests\Unit;
4
5
use RemotelyLiving\PHPEnv\Exceptions;
6
use RemotelyLiving\PHPEnv\StringCaster;
7
8
class StringCasterTest extends AbstractTestCase
9
{
10
11
    public function testCastsAsString(): void
12
    {
13
        $this->assertSame(
14
            'foobar',
15
            StringCaster::cast('foobar')->asString(),
16
        );
17
    }
18
19
    public function testCastsAsInteger(): void
20
    {
21
        $this->assertSame(
22
            2,
23
            StringCaster::cast('2')->asInteger(),
24
        );
25
26
        $this->assertSame(
27
            2,
28
            StringCaster::cast('2.1')->asInteger(),
29
        );
30
31
        $this->assertSame(
32
            3,
33
            StringCaster::cast('2.5')->asInteger(),
34
        );
35
    }
36
37
    /**
38
     * @dataProvider invalidNumericTypes
39
     */
40
    public function testDisallowsInvalidCastsAsInteger(string $invalidType): void
41
    {
42
        $this->expectException(Exceptions\InvalidArgument::class);
43
        StringCaster::cast($invalidType)->asInteger();
44
    }
45
46
    public function testCastsAsFloat(): void
47
    {
48
        $this->assertSame(
49
            2.0,
50
            StringCaster::cast('2')->asFloat(),
51
        );
52
53
        $this->assertSame(
54
            2.1769459034,
55
            StringCaster::cast('2.1769459034')->asFloat(),
56
        );
57
    }
58
59
    /**
60
     * @dataProvider invalidNumericTypes
61
     */
62
    public function testDisallowsInvalidCastsAsFloat(string $invalidType): void
63
    {
64
        $this->expectException(Exceptions\InvalidArgument::class);
65
        StringCaster::cast($invalidType)->asFloat();
66
    }
67
68
    public function testCastsAsBoolean(): void
69
    {
70
        $this->assertFalse(StringCaster::cast('false')->asBoolean());
71
        $this->assertFalse(StringCaster::cast('0')->asBoolean());
72
73
        $this->assertTrue(StringCaster::cast('true')->asBoolean());
74
        $this->assertTrue(StringCaster::cast('1')->asBoolean());
75
    }
76
77
    /**
78
     * @dataProvider invalidBooleanTypeProvider
79
     */
80
    public function testDisallowsInvalidCastsAsBoolean(string $invalidType): void
81
    {
82
        $this->expectException(Exceptions\InvalidArgument::class);
83
        StringCaster::cast($invalidType)->asBoolean();
84
    }
85
86
    public function testCastsAsJsonObject(): void
87
    {
88
        $this->assertEquals(
89
            new \stdClass(),
90
            StringCaster::cast('{}')->asJSONDecodedObject()
91
        );
92
    }
93
94
    /**
95
     * @dataProvider invalidJsonObjectTypeProvider
96
     */
97
    public function testDisallowsInvalidCastsAsJSONDecodedObject(string $invalidType): void
98
    {
99
        $this->expectException(Exceptions\InvalidArgument::class);
100
        StringCaster::cast($invalidType)->asJSONDecodedObject();
101
    }
102
103
    public function testCastsAsUnserializedObject(): void
104
    {
105
        $expected = new \stdClass();
106
        $expected->foo = 'bar';
107
        $this->assertEquals(
108
            $expected,
109
            StringCaster::cast('O:8:"stdClass":1:{s:3:"foo";s:3:"bar";}')->asUnserializedObject()
110
        );
111
    }
112
113
    /**
114
     * @dataProvider invalidSerializedObjectTypeProvider
115
     */
116
    public function testDisallowsInvalidCastsAsUnserializedObject(string $invalidType): void
117
    {
118
        $this->expectException(Exceptions\InvalidArgument::class);
119
        StringCaster::cast($invalidType)->asUnserializedObject();
120
    }
121
122
    public function testCastsSeparatedValuesAsArray(): void
123
    {
124
        $list1 = 'foo,bar,baz';
125
        $expectedList1 = ['foo', 'bar', 'baz'];
126
        $list2 = 'hey:you:all';
127
        $expectedList2 = ['hey', 'you', 'all'];
128
        $list3 = 'hey';
129
        $expectedList3 = ['hey'];
130
131
        $this->assertEquals(
132
            $expectedList1,
133
            StringCaster::cast($list1)->asArray()
134
        );
135
136
        $this->assertEquals(
137
            $expectedList2,
138
            StringCaster::cast($list2)->asArray(':')
139
        );
140
141
        $this->assertEquals(
142
            $expectedList3,
143
            StringCaster::cast($list3)->asArray(':')
144
        );
145
    }
146
147
    public function testDisallowsInvalidCastsAsArray(): void
148
    {
149
        $this->expectException(Exceptions\InvalidArgument::class);
150
        StringCaster::cast('hey there')->asArray('');
151
    }
152
153
    public function invalidBooleanTypeProvider(): array
154
    {
155
        return [
156
            'invalid string' => ['not valid boolean'],
157
            'invalid number' => ['300'],
158
            'invalid empty string' => ['']
159
        ];
160
    }
161
162
    public function invalidJsonObjectTypeProvider(): array
163
    {
164
        return [
165
            'invalid json' => ['not valid json'],
166
        ];
167
    }
168
169
    public function invalidSerializedObjectTypeProvider(): array
170
    {
171
        return [
172
            'invalid string' => ['not valid json'],
173
            'invalid array' => ['a:1:{s:3:"foo";s:3:"bar";}'],
174
            'invalid number' => ['s:1:"1";'],
175
            'invalid bool' => ['b:1;'],
176
        ];
177
    }
178
179
    public function invalidNumericTypes(): array
180
    {
181
        return [
182
            'invalid string' => ['not valid numeric'],
183
            'invalid bool' => ['true'],
184
        ];
185
    }
186
}
187