Passed
Branch develop (bae466)
by Paul
06:12
created

CastTest::test_to_int()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Tests;
4
5
use GeminiLabs\SiteReviews\Helpers\Cast;
6
use WP_UnitTestCase;
0 ignored issues
show
Bug introduced by
The type WP_UnitTestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
8
/**
9
 * Test case for the Plugin.
10
 * @group plugin
11
 */
12
class CastTest extends WP_UnitTestCase
13
{
14
    public function test_to()
15
    {
16
        $this->assertTrue(is_array(Cast::to('array', '')));
17
        $this->assertTrue(is_bool(Cast::to('bool', '')));
18
        $this->assertTrue(is_float(Cast::to('float', '')));
19
        $this->assertTrue(is_int(Cast::to('int', '12.3')));
20
        $this->assertTrue(is_object(Cast::to('object', '')));
21
        $this->assertTrue(is_string(Cast::to('string', [])));
22
        $this->assertEquals(Cast::to('xyz', 'abc'), 'abc');
23
    }
24
25
    public function test_to_array()
26
    {
27
        $this->assertEquals(Cast::toArray(''), []);
28
        $this->assertEquals(Cast::toArray('abc'), ['abc']);
29
        $this->assertEquals(Cast::toArray('a,b,c'), ['a', 'b', 'c']);
30
        $this->assertEquals(Cast::toArray('a,b,c', false), ['a,b,c']);
31
        $this->assertEquals(Cast::toArray(true), [true]);
32
        $this->assertEquals(Cast::toArray(false), [false]);
33
        $this->assertEquals(Cast::toArray(1), [1]);
34
        $this->assertEquals(Cast::toArray([1]), [1]);
35
        $this->assertEquals(Cast::toArray((object) ['a' => 123]), ['a' => 123]);
36
    }
37
38
    public function test_to_bool()
39
    {
40
        $this->assertFalse(Cast::toBool(''));
41
        $this->assertFalse(Cast::toBool(0));
42
        $this->assertFalse(Cast::toBool('0'));
43
        $this->assertFalse(Cast::toBool([]));
44
        $this->assertFalse(Cast::toBool([1]));
45
        $this->assertTrue(Cast::toBool(1));
46
        $this->assertTrue(Cast::toBool('1'));
47
        $this->assertTrue(Cast::toBool('true'));
48
    }
49
50
    public function test_to_float()
51
    {
52
        $this->assertEquals(Cast::toFloat(''), 0);
53
        $this->assertEquals(Cast::toFloat([]), 0);
54
        $this->assertEquals(Cast::toFloat('abc'), 0);
55
        $this->assertEquals(Cast::toFloat('123.123'), 123.123);
56
        $this->assertEquals(Cast::toFloat(123), 123);
57
        $this->assertEquals(Cast::toFloat(123.123), 123.123);
58
    }
59
60
    public function test_to_int()
61
    {
62
        $this->assertEquals(Cast::toInt(''), 0);
63
        $this->assertEquals(Cast::toInt([]), 0);
64
        $this->assertEquals(Cast::toInt('abc'), 0);
65
        $this->assertEquals(Cast::toInt('123.123'), 123);
66
        $this->assertEquals(Cast::toInt('123'), 123);
67
        $this->assertEquals(Cast::toInt(123.123), 123);
68
    }
69
70
    public function test_to_object()
71
    {
72
        $this->assertEquals(Cast::toObject(''), (object) []);
73
        $this->assertEquals(Cast::toObject((object) []), (object) []);
74
    }
75
76
    public function test_to_string()
77
    {
78
        $this->assertEquals(Cast::toString([]), '');
79
        $this->assertEquals(Cast::toString(123), '123');
80
        $this->assertEquals(Cast::toString([123]), '123');
81
        $this->assertEquals(Cast::toString([123], false), '123');
82
        $this->assertEquals(Cast::toString([1,2,3], false), '1, 2, 3');
83
        $this->assertEquals(Cast::toString(['a' => 1, 'b' => 2, 'c' => 3], false), 'a:3:{s:1:"a";i:1;s:1:"b";i:2;s:1:"c";i:3;}');
84
        $this->assertEquals(Cast::toString(new MockClass()), '123');
85
    }
86
}
87
88