File: ValueToStringTraitTest.php

package info (click to toggle)
php-ramsey-collection 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 612 kB
  • sloc: php: 3,239; xml: 31; makefile: 22
file content (108 lines) | stat: -rw-r--r-- 3,181 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php

declare(strict_types=1);

namespace Ramsey\Collection\Test\Tool;

use DateTimeImmutable;
use Ramsey\Collection\Test\TestCase;
use Ramsey\Collection\Test\Tool\Mock\ObjectWithInvoke;
use Ramsey\Collection\Test\Tool\Mock\ObjectWithToString;
use Ramsey\Collection\Tool\ValueToStringTrait;
use stdClass;

use function get_resource_type;
use function opendir;

/**
 * Tests for ValueToStringTrait
 */
class ValueToStringTraitTest extends TestCase
{
    use ValueToStringTrait;

    public function testValueNull(): void
    {
        $this->assertSame('NULL', $this->toolValueToString(null));
    }

    public function testValueBoolean(): void
    {
        $this->assertSame('TRUE', $this->toolValueToString(true));
        $this->assertSame('FALSE', $this->toolValueToString(false));
    }

    public function testValueArray(): void
    {
        $this->assertSame('Array', $this->toolValueToString([]));
    }

    public function testValueScalar(): void
    {
        $this->assertSame('', $this->toolValueToString(''));
        $this->assertSame('foo', $this->toolValueToString('foo'));
        $this->assertSame('9', $this->toolValueToString(9));
    }

    public function testValueResource(): void
    {
        // get_resource_type behaves different on php and hhvm
        $resource = opendir(__DIR__);

        $this->assertIsResource($resource);

        $expected = '(' . get_resource_type($resource) . ' resource #' . (int) $resource . ')';

        $this->assertSame($expected, $this->toolValueToString($resource));
    }

    public function testValueObjectWithToString(): void
    {
        $this->assertSame('BAZ', $this->toolValueToString(new ObjectWithToString()));
    }

    public function testValueDateTime(): void
    {
        // datetimes are objects but are returned as iso dates, not as generic objects
        $expected = '2016-12-31T23:59:59+00:00';
        $date = new DateTimeImmutable('2016-12-31T23:59:59+00:00');
        $this->assertSame($expected, $this->toolValueToString($date));
    }

    public function testValueObject(): void
    {
        $expected = '(stdClass Object)';

        $value = new stdClass();
        $casted = $this->toolValueToString($value);

        $this->assertSame($expected, $casted);
    }

    public function testValueClosure(): void
    {
        // do not return a message like 'callable', cast it as object
        $startWith = '(Closure';
        $endsWith = ' Object)';

        $value = fn (): bool => false;
        $casted = $this->toolValueToString($value);

        $this->assertStringStartsWith($startWith, $casted);
        $this->assertStringEndsWith($endsWith, $casted);
    }

    public function testValueObjectWithInvoke(): void
    {
        // the object has a public __invoke method, is detected as callable
        // do not return a message like 'callable', cast it as object
        $startWith = '(' . ObjectWithInvoke::class . ' ';
        $endsWith = ' Object)';

        $value = new ObjectWithInvoke();
        $casted = $this->toolValueToString($value);

        $this->assertStringStartsWith($startWith, $casted);
        $this->assertStringEndsWith($endsWith, $casted);
    }
}