Passed
Push — master ( f2bba7...82b3a3 )
by Adrien
27:13 queued 06:24
created

DataTypeTest::randr()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 5
eloc 14
c 2
b 1
f 0
nc 5
nop 1
dl 0
loc 22
rs 9.4888
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheetTests\Cell;
4
5
use PhpOffice\PhpSpreadsheet\Cell\DataType;
6
use PhpOffice\PhpSpreadsheet\RichText\RichText;
7
use PHPUnit\Framework\TestCase;
8
9
class DataTypeTest extends TestCase
10
{
11
    public function testGetErrorCodes()
12
    {
13
        $result = DataType::getErrorCodes();
14
        self::assertIsArray($result);
15
        self::assertGreaterThan(0, count($result));
16
        self::assertArrayHasKey('#NULL!', $result);
17
    }
18
19
    public function testCheckString()
20
    {
21
        $richText = new RichText();
22
        $result1 = DataType::checkString($richText);
23
        self::assertSame($richText, $result1);
24
25
        $stringLimit = 32767;
26
        $randString = $this->randr($stringLimit + 10);
27
        $result2 = DataType::checkString($randString);
28
        self::assertSame($stringLimit, strlen($result2));
29
30
        $dirtyString = "bla bla\r\n bla\r test\n";
31
        $expected = "bla bla\n bla\n test\n";
32
        $result3 = DataType::checkString($dirtyString);
33
        self::assertSame($expected, $result3);
34
    }
35
36
    /**
37
     * @param int $length
38
     *
39
     * @return string
40
     */
41
    private function randr($length = 8)
42
    {
43
        $string = '';
44
        for ($i = 0; $i < $length; ++$i) {
45
            $x = mt_rand(0, 2);
46
            switch ($x) {
47
                case 0:
48
                    $string .= chr(mt_rand(97, 122));
49
50
                    break;
51
                case 1:
52
                    $string .= chr(mt_rand(65, 90));
53
54
                    break;
55
                case 2:
56
                    $string .= chr(mt_rand(48, 57));
57
58
                    break;
59
            }
60
        }
61
62
        return $string;
63
    }
64
}
65