Passed
Push — develop ( 57a029...1b96c9 )
by Mark
31:53
created

ComplexAssert::testExpectedExceptions()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 3
nop 2
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheetTests\Custom;
4
5
use Complex\Complex;
6
7
class ComplexAssert
8
{
9
    private $errorMessage = '';
10
11
    private function testExpectedExceptions($expected, $actual)
12
    {
13
        //    Expecting an error, so we do a straight string comparison
14
        if ($expected === $actual) {
15
            return true;
16
        } elseif ($expected === INF && $actual === 'INF') {
17
            return true;
18
        }
19
        $this->errorMessage = 'Expected Error: ' . $actual . ' !== ' . $expected;
20
21
        return false;
22
    }
23
24
    private function adjustDelta($expected, $actual, $delta)
25
    {
26
        $adjustedDelta = $delta;
27
28
        if (abs($actual) > 10 && abs($expected) > 10) {
29
            $variance = floor(log10(abs($expected)));
30
            $adjustedDelta *= pow(10, $variance);
31
        }
32
33
        return $adjustedDelta > 1.0 ? 1.0 : $adjustedDelta;
34
    }
35
36
    public function assertComplexEquals($expected, $actual, $delta = 0)
37
    {
38
        if ($expected === INF || $expected[0] === '#') {
39
            return $this->testExpectedExceptions($expected, $actual);
40
        }
41
42
        $expectedComplex = new Complex($expected);
43
        $actualComplex = new Complex($actual);
44
45
        if (!is_numeric($actualComplex->getReal()) || !is_numeric($expectedComplex->getReal())) {
0 ignored issues
show
introduced by
The condition is_numeric($actualComplex->getReal()) is always true.
Loading history...
introduced by
The condition is_numeric($expectedComplex->getReal()) is always true.
Loading history...
46
            if ($actualComplex->getReal() !== $expectedComplex->getReal()) {
47
                $this->errorMessage = 'Mismatched String: ' . $actualComplex->getReal() . ' !== ' . $expectedComplex->getReal();
48
49
                return false;
50
            }
51
52
            return true;
53
        }
54
55
        $adjustedDelta = $this->adjustDelta($expectedComplex->getReal(), $actualComplex->getReal(), $delta);
56
        if (abs($actualComplex->getReal() - $expectedComplex->getReal()) > $adjustedDelta) {
57
            $this->errorMessage = 'Mismatched Real part: ' . $actualComplex->getReal() . ' != ' . $expectedComplex->getReal();
58
59
            return false;
60
        }
61
62
        $adjustedDelta = $this->adjustDelta($expectedComplex->getImaginary(), $actualComplex->getImaginary(), $delta);
63
        if (abs($actualComplex->getImaginary() - $expectedComplex->getImaginary()) > $adjustedDelta) {
64
            $this->errorMessage = 'Mismatched Imaginary part: ' . $actualComplex->getImaginary() . ' != ' . $expectedComplex->getImaginary();
65
66
            return false;
67
        }
68
69
        if ($actualComplex->getSuffix() !== $actualComplex->getSuffix()) {
70
            $this->errorMessage = 'Mismatched Suffix: ' . $actualComplex->getSuffix() . ' != ' . $expectedComplex->getSuffix();
71
72
            return false;
73
        }
74
75
        return true;
76
    }
77
78
    public function getErrorMessage()
79
    {
80
        return $this->errorMessage;
81
    }
82
}
83