|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Knp\FriendlyContexts\Utils; |
|
4
|
|
|
|
|
5
|
|
|
use Knp\FriendlyContexts\Table\NodesBuilder; |
|
6
|
|
|
|
|
7
|
|
|
class Asserter |
|
8
|
|
|
{ |
|
9
|
|
|
protected $formater; |
|
10
|
|
|
|
|
11
|
|
|
public function __construct(TextFormater $formater) |
|
12
|
|
|
{ |
|
13
|
|
|
$this->formater = $formater; |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
public function assertArrayEquals(array $expected, array $real, $fullText = false) |
|
17
|
|
|
{ |
|
18
|
|
|
$message = sprintf("The given array\r\n\r\n%s\r\nis not equals to expected\r\n\r\n%s", $this->explode($real), $this->explode($expected)); |
|
19
|
|
|
|
|
20
|
|
|
if (false === $fullText) { |
|
21
|
|
|
return $this->assertEquals( |
|
22
|
|
|
$expected, |
|
23
|
|
|
$real, |
|
24
|
|
|
$message |
|
25
|
|
|
); |
|
26
|
|
|
} else { |
|
27
|
|
|
return $this->assertEquals( |
|
28
|
|
|
$this->explode($expected), |
|
29
|
|
|
$this->explode($real), |
|
30
|
|
|
$message |
|
31
|
|
|
); |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function assertArrayContains(array $expected, array $real, $message = null) |
|
36
|
|
|
{ |
|
37
|
|
|
$message = $message ?: sprintf("The given array\r\n\r\n%s\r\ndoes not contain the following rows\r\n\r\n%s", $this->explode($real), $this->explode($expected)); |
|
38
|
|
|
$indexes = []; |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
foreach ($expected as $row) { |
|
41
|
|
|
$this->assert(is_array($row), $message); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
foreach ($real as $row) { |
|
45
|
|
|
$this->assert(is_array($row), $message); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$nodes = (new NodesBuilder)->build($real); |
|
49
|
|
|
$nodes = $nodes->search(current(current($expected))); |
|
50
|
|
|
|
|
51
|
|
|
foreach ($nodes as $initial) { |
|
52
|
|
|
$result = true; |
|
|
|
|
|
|
53
|
|
|
$cells = $expected; |
|
54
|
|
|
$lineStart = $initial; |
|
55
|
|
|
do { |
|
56
|
|
|
$columns = array_shift($cells); |
|
57
|
|
|
$columnElement = $lineStart; |
|
58
|
|
|
do { |
|
59
|
|
|
$content = array_shift($columns); |
|
|
|
|
|
|
60
|
|
|
$result = $columnElement |
|
|
|
|
|
|
61
|
|
|
? $content === $columnElement->getContent() |
|
62
|
|
|
: false |
|
63
|
|
|
; |
|
64
|
|
|
$columnElement = $columnElement ? $columnElement->getRight() : null; |
|
65
|
|
|
} while (!empty($columns) && $result); |
|
66
|
|
|
$lineStart = $lineStart ? $lineStart->getBottom() : null; |
|
67
|
|
|
} while (!empty($cells) && $result); |
|
68
|
|
|
|
|
69
|
|
|
if ($result) { |
|
70
|
|
|
|
|
71
|
|
|
return true; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$this->assert(false, $message); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function assertEquals($expected, $real, $message = "Failing to assert equals.") |
|
|
|
|
|
|
79
|
|
|
{ |
|
80
|
|
|
return $this->assert($expected === $real, $message); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function assertNotEquals($expected, $real, $message = "Failing to assert not equals.") |
|
|
|
|
|
|
84
|
|
|
{ |
|
85
|
|
|
return $this->assert($expected !== $real, $message); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function assert($result, $message = "Assert failure") |
|
|
|
|
|
|
89
|
|
|
{ |
|
90
|
|
|
if (false === $result) { |
|
91
|
|
|
throw new \Exception($message, 1); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return true; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
private function explode($value) |
|
98
|
|
|
{ |
|
99
|
|
|
if (!is_array($value)) { |
|
100
|
|
|
return (string) $value; |
|
101
|
|
|
} else { |
|
102
|
|
|
return $this->formater->tableToString($value); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.