Completed
Push — develop ( 888c61...656149 )
by Adrien
35:11
created

DataValidatorTest::testNoValidation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheetTests\Cell;
4
5
use PhpOffice\PhpSpreadsheet\Cell\DataValidation;
6
use PhpOffice\PhpSpreadsheet\Spreadsheet;
7
use PHPUnit_Framework_TestCase;
8
9
class DataValidatorTest extends PHPUnit_Framework_TestCase
10
{
11
    public function testNoValidation()
12
    {
13
        $spreadsheet = new Spreadsheet();
14
        $sheet = $spreadsheet->getActiveSheet();
15
        $testCell = $sheet->getCell('A1');
16
17
        self::assertTrue($testCell->hasValidValue(), 'a cell without any validation data is always valid');
18
    }
19
20
    public function testUnsupportedType()
21
    {
22
        $spreadsheet = new Spreadsheet();
23
        $sheet = $spreadsheet->getActiveSheet();
24
        $testCell = $sheet->getCell('A1');
25
26
        $validation = $testCell->getDataValidation();
27
        $validation->setType(DataValidation::TYPE_CUSTOM);
28
        $validation->setAllowBlank(true);
29
30
        self::assertFalse($testCell->hasValidValue(), 'cannot assert that value is valid when the validation type is not supported');
31
    }
32
33
    public function testList()
34
    {
35
        $spreadsheet = new Spreadsheet();
36
        $sheet = $spreadsheet->getActiveSheet();
37
        $testCell = $sheet->getCell('A1');
38
39
        $validation = $testCell->getDataValidation();
40
        $validation->setType(DataValidation::TYPE_LIST);
41
42
        // blank value
43
        $testCell->setValue('');
44
        $validation->setAllowBlank(true);
45
        self::assertTrue($testCell->hasValidValue(), 'cell can be empty');
46
        $validation->setAllowBlank(false);
47
        self::assertFalse($testCell->hasValidValue(), 'cell can not be empty');
48
49
        // inline list
50
        $validation->setFormula1('"yes,no"');
51
        $testCell->setValue('foo');
52
        self::assertFalse($testCell->hasValidValue(), "cell value ('foo') is not allowed");
53
        $testCell->setValue('yes');
54
        self::assertTrue($testCell->hasValidValue(), "cell value ('yes') has to be allowed");
55
56
        // list from cells
57
        $sheet->getCell('B1')->setValue(5);
58
        $sheet->getCell('B2')->setValue(6);
59
        $sheet->getCell('B3')->setValue(7);
60
        $testCell = $sheet->getCell('A1'); // redefine $testCell, because it has broken coordinates after using other cells
61
        $validation->setFormula1('B1:B3');
62
        $testCell->setValue('10');
63
        self::assertFalse($testCell->hasValidValue(), "cell value ('10') is not allowed");
64
        $testCell = $sheet->getCell('A1'); // redefine $testCell, because it has broken coordinates after using other cells
65
        $testCell->setValue('5');
66
        self::assertTrue($testCell->hasValidValue(), "cell value ('5') has to be allowed");
67
68
        $testCell = $sheet->getCell('A1'); // redefine $testCell, because it has broken coordinates after using other cells
69
        $validation->setFormula1('broken : cell : coordinates');
70
71
        self::assertFalse($testCell->hasValidValue(), 'invalid formula should not throw exceptions');
72
    }
73
}
74