aboutsummaryrefslogtreecommitdiff
path: root/tests/UtilsTest.php
blob: f851f32b04e3c4cff09a7b1914ebf909ff7f8828 (plain) (blame)
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
<?php

declare(strict_types=1);

namespace RssBridge\Tests;

use PHPUnit\Framework\TestCase;

final class UtilsTest extends TestCase
{
    public function testTruncate()
    {
        $this->assertSame('f...', truncate('foo', 1));
        $this->assertSame('fo...', truncate('foo', 2));
        $this->assertSame('foo', truncate('foo', 3));
        $this->assertSame('foo', truncate('foo', 4));
        $this->assertSame('fo[...]', truncate('foo', 2, '[...]'));
    }

    public function testFormatBytes()
    {
        $this->assertSame('1 B', format_bytes(1));
        $this->assertSame('1 KB', format_bytes(1024));
        $this->assertSame('1 MB', format_bytes(1024 ** 2));
        $this->assertSame('1 GB', format_bytes(1024 ** 3));
        $this->assertSame('1 TB', format_bytes(1024 ** 4));
    }

    public function testSanitizePathName()
    {
        $this->assertSame('index.php', _sanitize_path_name('/home/satoshi/rss-bridge/index.php', '/home/satoshi/rss-bridge'));
        $this->assertSame('tests/UtilsTest.php', _sanitize_path_name('/home/satoshi/rss-bridge/tests/UtilsTest.php', '/home/satoshi/rss-bridge'));
        $this->assertSame('bug in lib/kek.php', _sanitize_path_name('bug in /home/satoshi/rss-bridge/lib/kek.php', '/home/satoshi/rss-bridge'));
    }

    public function testSanitizePathNameInErrorMessage()
    {
        $raw       = 'Error: Argument 1 passed to foo() must be an instance of kk, string given, called in /home/satoshi/rss-bridge/bridges/RumbleBridge.php';
        $sanitized = 'Error: Argument 1 passed to foo() must be an instance of kk, string given, called in bridges/RumbleBridge.php';
        $this->assertSame($sanitized, _sanitize_path_name($raw, '/home/satoshi/rss-bridge'));
    }

    public function testCreateRandomString()
    {
        $this->assertSame(2, strlen(create_random_string(1)));
        $this->assertSame(4, strlen(create_random_string(2)));
        $this->assertSame(6, strlen(create_random_string(3)));
    }

    public function testUrljoin()
    {
        $base = '/';
        $rel = 'https://example.com/foo';

        $url = urljoin($base, $rel);

        $this->assertSame($rel, $url);
    }
}