Passed
Pull Request — master (#4139)
by Owen
11:02
created

NumericCellTypeTest.php$0 ➔ setUp()   A

Complexity

Conditions 4

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
rs 9.568
cc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A NumericCellTypeTest.php$0 ➔ bindValue() 0 13 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
6
7
use PhpOffice\PhpSpreadsheet\Cell\Cell;
8
use PhpOffice\PhpSpreadsheet\Cell\DataType;
9
use PhpOffice\PhpSpreadsheet\Cell\IValueBinder;
10
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as Reader;
11
use PhpOffice\PhpSpreadsheet\Shared\File;
12
use PhpOffice\PhpSpreadsheet\Spreadsheet;
13
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as Writer;
14
use PHPUnit\Framework\TestCase;
15
16
class NumericCellTypeTest extends TestCase
17
{
18
    private IValueBinder $oldBinder;
19
20
    private string $filename = '';
21
22
    protected function setUp(): void
23
    {
24
        $this->oldBinder = Cell::getValueBinder();
25
26
        $binder = new class () implements IValueBinder {
27
            public function bindValue(Cell $cell, mixed $value): bool
28
            {
29
                if (is_float($value) || is_int($value)) {
30
                    $type = DataType::TYPE_NUMERIC;
31
                } elseif (is_string($value)) {
32
                    $type = DataType::TYPE_STRING;
33
                } else {
34
                    return false;
35
                }
36
37
                $cell->setValueExplicit($value, $type);
38
39
                return true;
40
            }
41
        };
42
43
        Cell::setValueBinder($binder);
44
    }
45
46
    protected function tearDown(): void
47
    {
48
        Cell::setValueBinder($this->oldBinder);
49
        if ($this->filename !== '') {
50
            unlink($this->filename);
51
            $this->filename = '';
52
        }
53
    }
54
55
    public function testCellShouldHaveNumericTypeAttribute(): void
56
    {
57
        $spreadsheet = new Spreadsheet();
58
        $sheet = $spreadsheet->getActiveSheet();
59
        $array = [
60
            ['1.0'],
61
            [1.0],
62
            ['-1.0'],
63
            [-1.0],
64
            ['0'],
65
            [0],
66
            ['0.0'],
67
            [0.0],
68
            ['1e1'],
69
            [1e1],
70
        ];
71
        $sheet->fromArray($array, null, 'A1', true);
72
73
        $this->filename = File::temporaryFilename();
74
        $writer = new Writer($spreadsheet);
75
        $writer->save($this->filename);
76
        $spreadsheet->disconnectWorksheets();
77
78
        $reader = new Reader();
79
        $spreadsheet2 = $reader->load($this->filename);
80
81
        $sheet2 = $spreadsheet2->getActiveSheet();
82
        self::assertSame($array, $sheet2->toArray(null, false, false));
83
        $spreadsheet2->disconnectWorksheets();
84
    }
85
}
86