• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

misantron / dbunit / 4562384438

pending completion
4562384438

push

github

Aleksandr Ivanov
Fix build status badge display

635 of 1200 relevant lines covered (52.92%)

5.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

36.0
/src/DataSet/AbstractTable.php
1
<?php
2

3
/*
4
 * This file is part of DbUnit.
5
 *
6
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
namespace PHPUnit\DbUnit\DataSet;
13

14
use PHPUnit\DbUnit\Exception\InvalidArgumentException;
15

16
/**
17
 * Provides a basic functionality for dbunit tables
18
 */
19
class AbstractTable implements ITable
20
{
21
    /**
22
     * @var ITableMetadata
23
     */
24
    protected $tableMetaData;
25

26
    /**
27
     * A 2-dimensional array containing the data for this table.
28
     *
29
     * @var array
30
     */
31
    protected $data;
32

33
    /**
34
     * @var ITable|null
35
     */
36
    private $other;
37

38
    public function __toString()
39
    {
40
        $columns = $this->getTableMetaData()->getColumns();
×
41
        $count   = \count($columns);
×
42

43
        // if count less than 0 (when table is empty), then set count to 1
44
        $count         = $count >= 1 ? $count : 1;
×
45
        $lineSeparator = str_repeat('+----------------------', $count) . "+\n";
×
46
        $lineLength    = \strlen($lineSeparator) - 1;
×
47

48
        $tableString = $lineSeparator;
×
49
        $tblName     = $this->getTableMetaData()->getTableName();
×
50
        $tableString .= '| ' . str_pad(
×
51
            $tblName,
×
52
            $lineLength - 4,
×
53
            ' ',
×
54
            STR_PAD_RIGHT
×
55
        ) . " |\n";
×
56
        $tableString .= $lineSeparator;
×
57
        $rows = $this->rowToString($columns);
×
58
        $tableString .= !empty($rows) ? $rows . $lineSeparator : '';
×
59

60
        $rowCount = $this->getRowCount();
×
61

62
        for ($i = 0; $i < $rowCount; $i++) {
×
63
            $values = [];
×
64

65
            foreach ($columns as $columnName) {
×
66
                if ($this->other) {
×
67
                    try {
68
                        if ($this->getValue($i, $columnName) !== $this->other->getValue($i, $columnName)) {
×
69
                            $values[] = sprintf(
×
70
                                '%s != actual %s',
×
71
                                var_export($this->getValue($i, $columnName), true),
×
72
                                var_export($this->other->getValue($i, $columnName), true)
×
73
                            );
×
74
                        } else {
75
                            $values[] = $this->getValue($i, $columnName);
×
76
                        }
77
                    } catch (\InvalidArgumentException $ex) {
×
78
                        $values[] = $this->getValue($i, $columnName) . ': no row';
×
79
                    }
80
                } else {
81
                    $values[] = $this->getValue($i, $columnName);
×
82
                }
83
            }
84

85
            $tableString .= $this->rowToString($values) . $lineSeparator;
×
86
        }
87

88
        return ($this->other ? '(table diff enabled)' : '') . "\n" . $tableString . "\n";
×
89
    }
90

91
    /**
92
     * Returns the table's meta data.
93
     *
94
     * @return ITableMetadata
95
     */
96
    public function getTableMetaData(): ITableMetadata
97
    {
98
        return $this->tableMetaData;
77✔
99
    }
100

101
    /**
102
     * Returns the number of rows in this table.
103
     *
104
     * @return int
105
     */
106
    public function getRowCount(): int
107
    {
108
        return \count($this->data);
35✔
109
    }
110

111
    /**
112
     * Returns the value for the given column on the given row.
113
     *
114
     * @param int $row
115
     * @param string $column
116
     *
117
     * @return mixed|string
118
     */
119
    public function getValue(int $row, string $column)
120
    {
121
        if (!isset($this->data[$row]) || !\in_array($column, $this->getTableMetaData()->getColumns(), true)) {
38✔
122
            throw new InvalidArgumentException(
×
123
                "The given row ({$row}) and column ({$column}) do not exist in table {$this->getTableMetaData()->getTableName()}"
×
124
            );
×
125
        }
126

127
        $value = $this->data[$row][$column];
38✔
128

129
        return $value instanceof \SimpleXMLElement ? (string) $value : $value;
38✔
130
    }
131

132
    /**
133
     * Returns the an associative array keyed by columns for the given row.
134
     *
135
     * @param int $row
136
     *
137
     * @return array
138
     */
139
    public function getRow(int $row): array
140
    {
141
        if (!isset($this->data[$row])) {
2✔
142
            throw new InvalidArgumentException(
×
143
                "The given row ({$row}) does not exist in table {$this->getTableMetaData()->getTableName()}"
×
144
            );
×
145
        }
146

147
        return $this->data[$row];
2✔
148
    }
149

150
    /**
151
     * Asserts that the given table matches this table.
152
     *
153
     * @param ITable $other
154
     *
155
     * @return bool
156
     */
157
    public function matches(ITable $other): bool
158
    {
159
        $thisMetaData = $this->getTableMetaData();
44✔
160
        $otherMetaData = $other->getTableMetaData();
44✔
161

162
        if (!$thisMetaData->matches($otherMetaData) || $this->getRowCount() !== $other->getRowCount()) {
44✔
163
            return false;
3✔
164
        }
165

166
        $columns = $thisMetaData->getColumns();
41✔
167
        $rowCount = $this->getRowCount();
41✔
168

169
        for ($i = 0; $i < $rowCount; $i++) {
41✔
170
            foreach ($columns as $columnName) {
39✔
171
                $thisValue = $this->getValue($i, $columnName);
39✔
172
                $otherValue = $other->getValue($i, $columnName);
39✔
173

174
                if (is_numeric($thisValue) && is_numeric($otherValue)) {
39✔
175
                    if ($thisValue != $otherValue) {
35✔
176
                        $this->other = $other;
2✔
177

178
                        return false;
35✔
179
                    }
180
                } elseif ($thisValue !== $otherValue) {
32✔
181
                    $this->other = $other;
4✔
182

183
                    return false;
4✔
184
                }
185
            }
186
        }
187

188
        return true;
35✔
189
    }
190

191
    /**
192
     * Checks if a given row is in the table
193
     *
194
     * @param array $row
195
     *
196
     * @return bool
197
     */
198
    public function assertContainsRow(array $row): bool
199
    {
200
        return \in_array($row, $this->data, true);
3✔
201
    }
202

203
    /**
204
     * Sets the metadata for this table.
205
     *
206
     * @param ITableMetadata $tableMetaData
207
     *
208
     * @deprecated
209
     */
210
    protected function setTableMetaData(ITableMetadata $tableMetaData): void
211
    {
212
        $this->tableMetaData = $tableMetaData;
69✔
213
    }
214

215
    protected function rowToString(array $row): string
216
    {
217
        $rowString = '';
×
218

219
        foreach ($row as $value) {
×
220
            if ($value === null) {
×
221
                $value = 'NULL';
×
222
            }
223

224
            $value_str = mb_substr($value, 0, 20);
×
225

226
            // make str_pad act in multi byte manner
227
            $correction = \strlen($value_str) - mb_strlen($value_str);
×
228
            $rowString .= '| ' . str_pad($value_str, 20 + $correction, ' ', STR_PAD_BOTH) . ' ';
×
229
        }
230

231
        /** @see https://github.com/sebastianbergmann/dbunit/issues/195 */
232
        $rowString = !empty($row) ? $rowString . "|\n" : '';
×
233

234
        return $rowString;
×
235
    }
236
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc