Failed Conditions
Pull Request — master (#4141)
by Owen
13:32
created

StringValueBinder2Test   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
eloc 78
c 1
b 0
f 0
dl 0
loc 112
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testStringValueBinderIgnoredErrorsDefault() 0 31 5
A setUp() 0 3 1
B testStringValueBinderPreserveNumeric() 0 31 7
A tearDown() 0 3 1
A testStringValueBinderIgnoredErrorsTrue() 0 32 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpOffice\PhpSpreadsheetTests\Cell;
6
7
use DateTime;
8
use PhpOffice\PhpSpreadsheet\Cell\Cell;
9
use PhpOffice\PhpSpreadsheet\Cell\DataType;
10
use PhpOffice\PhpSpreadsheet\Cell\IValueBinder;
11
use PhpOffice\PhpSpreadsheet\Cell\StringValueBinder;
12
use PhpOffice\PhpSpreadsheet\RichText\RichText;
13
use PhpOffice\PhpSpreadsheet\Spreadsheet;
14
use PHPUnit\Framework\TestCase;
15
16
class StringValueBinder2Test extends TestCase
17
{
18
    private IValueBinder $valueBinder;
19
20
    protected function setUp(): void
21
    {
22
        $this->valueBinder = Cell::getValueBinder();
23
    }
24
25
    protected function tearDown(): void
26
    {
27
        Cell::setValueBinder($this->valueBinder);
28
    }
29
30
    public function testStringValueBinderIgnoredErrorsDefault(): void
31
    {
32
        $valueBinder = new StringValueBinder();
33
        Cell::setValueBinder($valueBinder);
34
        $spreadsheet = new Spreadsheet();
35
        $sheet = $spreadsheet->getActiveSheet();
36
        $richText = new RichText();
37
        $richText->createTextRun('6');
38
        $richText2 = new RichText();
39
        $richText2->createTextRun('a');
40
        $sheet->fromArray([
41
            [1, 'x', 3.2],
42
            ['y', -5, 'z'],
43
            [new DateTime(), $richText, $richText2],
44
            [new StringableObject('a'), new StringableObject(2), 'z'],
45
        ]);
46
        $ignoredCells = [];
47
        foreach ($sheet->getRowIterator() as $row) {
48
            foreach ($row->getCellIterator() as $cell) {
49
                $coordinate = $cell->getCoordinate();
50
                $dataType = $cell->getDataType();
51
                if ($dataType !== DataType::TYPE_INLINE) {
52
                    self::assertSame(DataType::TYPE_STRING, $dataType, "not string for cell $coordinate");
53
                }
54
                if ($cell->getIgnoredErrors()->getNumberStoredAsText()) {
55
                    $ignoredCells[] = $coordinate;
56
                }
57
            }
58
        }
59
        self::assertSame([], $ignoredCells);
60
        $spreadsheet->disconnectWorksheets();
61
    }
62
63
    public function testStringValueBinderIgnoredErrorsTrue(): void
64
    {
65
        $valueBinder = new StringValueBinder();
66
        $valueBinder->setSetIgnoredErrors(true);
67
        Cell::setValueBinder($valueBinder);
68
        $spreadsheet = new Spreadsheet();
69
        $sheet = $spreadsheet->getActiveSheet();
70
        $richText = new RichText();
71
        $richText->createTextRun('6');
72
        $richText2 = new RichText();
73
        $richText2->createTextRun('a');
74
        $sheet->fromArray([
75
            [1, 'x', 3.2],
76
            ['y', -5, 'z'],
77
            [new DateTime(), $richText, $richText2],
78
            [new StringableObject('a'), new StringableObject(2), 'z'],
79
        ]);
80
        $ignoredCells = [];
81
        foreach ($sheet->getRowIterator() as $row) {
82
            foreach ($row->getCellIterator() as $cell) {
83
                $coordinate = $cell->getCoordinate();
84
                $dataType = $cell->getDataType();
85
                if ($dataType !== DataType::TYPE_INLINE) {
86
                    self::assertSame(DataType::TYPE_STRING, $dataType, "not string for cell $coordinate");
87
                }
88
                if ($cell->getIgnoredErrors()->getNumberStoredAsText()) {
89
                    $ignoredCells[] = $coordinate;
90
                }
91
            }
92
        }
93
        self::assertSame(['A1', 'C1', 'B2', 'B3', 'B4'], $ignoredCells);
94
        $spreadsheet->disconnectWorksheets();
95
    }
96
97
    public function testStringValueBinderPreserveNumeric(): void
98
    {
99
        $valueBinder = new StringValueBinder();
100
        $valueBinder->setNumericConversion(false);
101
        $valueBinder->setSetIgnoredErrors(true);
102
        Cell::setValueBinder($valueBinder);
103
        $spreadsheet = new Spreadsheet();
104
        $sheet = $spreadsheet->getActiveSheet();
105
        $richText = new RichText();
106
        $richText->createTextRun('6');
107
        $richText2 = new RichText();
108
        $richText2->createTextRun('a');
109
        $sheet->fromArray([
110
            [1, 'x', 3.2],
111
            ['y', -5, 'z'],
112
            [new DateTime(), $richText, $richText2],
113
            [new StringableObject('a'), new StringableObject(2), 'z'],
114
        ]);
115
        $ignoredCells = [];
116
        foreach ($sheet->getRowIterator() as $row) {
117
            foreach ($row->getCellIterator() as $cell) {
118
                $coordinate = $cell->getCoordinate();
119
                $expected = (is_int($cell->getValue()) || is_float($cell->getValue())) ? DataType::TYPE_NUMERIC : (($cell->getValue() instanceof RichText) ? DataType::TYPE_INLINE : DataType::TYPE_STRING);
120
                self::assertSame($expected, $cell->getDataType(), "wrong type for cell $coordinate");
121
                if ($cell->getIgnoredErrors()->getNumberStoredAsText()) {
122
                    $ignoredCells[] = $coordinate;
123
                }
124
            }
125
        }
126
        self::assertSame(['B3', 'B4'], $ignoredCells);
127
        $spreadsheet->disconnectWorksheets();
128
    }
129
}
130