5 * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * * Neither the name of Sebastian Bergmann nor the names of his
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
39 * @author Mike Lively <m@digitalsandwich.com>
40 * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
41 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
43 * @link http://www.phpunit.de/
44 * @since File available since Release 3.2.0
47 require_once 'PHPUnit/Framework.php';
48 require_once 'PHPUnit/Util/Filter.php';
50 require_once 'PHPUnit/Extensions/Database/DataSet/ITable.php';
52 PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
55 * Provides a basic functionality for dbunit tables
59 * @author Mike Lively <m@digitalsandwich.com>
60 * @copyright 2009 Mike Lively <m@digitalsandwich.com>
61 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
62 * @version Release: 3.3.17
63 * @link http://www.phpunit.de/
64 * @since Class available since Release 3.2.0
66 class PHPUnit_Extensions_Database_DataSet_AbstractTable implements PHPUnit_Extensions_Database_DataSet_ITable
70 * @var PHPUnit_Extensions_Database_DataSet_ITableMetaData
72 protected $tableMetaData;
75 * A 2-dimensional array containing the data for this table.
82 * Sets the metadata for this table.
84 * @param PHPUnit_Extensions_Database_DataSet_ITableMetaData $tableMetaData
87 protected function setTableMetaData(PHPUnit_Extensions_Database_DataSet_ITableMetaData $tableMetaData)
89 $this->tableMetaData = $tableMetaData;
93 * Returns the table's meta data.
95 * @return PHPUnit_Extensions_Database_DataSet_ITableMetaData
97 public function getTableMetaData()
99 return $this->tableMetaData;
103 * Returns the number of rows in this table.
107 public function getRowCount()
109 return count($this->data);
113 * Returns the value for the given column on the given row.
117 * @todo reorganize this function to throw the exception first.
119 public function getValue($row, $column)
121 if (isset($this->data[$row][$column])) {
122 return (string)$this->data[$row][$column];
124 if (!in_array($column, $this->getTableMetaData()->getColumns()) || $this->getRowCount() <= $row) {
125 throw new InvalidArgumentException("The given row ({$row}) and column ({$column}) do not exist in table {$this->getTableMetaData()->getTableName()}");
133 * Returns the an associative array keyed by columns for the given row.
138 public function getRow($row)
140 if (isset($this->data[$row])) {
141 return $this->data[$row];
143 if ($this->getRowCount() <= $row) {
144 throw new InvalidArgumentException("The given row ({$row}) does not exist in table {$this->getTableMetaData()->getTableName()}");
152 * Asserts that the given table matches this table.
154 * @param PHPUnit_Extensions_Database_DataSet_ITable $other
156 public function assertEquals(PHPUnit_Extensions_Database_DataSet_ITable $other)
158 $thisMetaData = $this->getTableMetaData();
159 $otherMetaData = $other->getTableMetaData();
161 $thisMetaData->assertEquals($otherMetaData);
163 if ($this->getRowCount() != $other->getRowCount()) {
164 throw new Exception("Expected row count of {$this->getRowCount()}, has a row count of {$other->getRowCount()}");
167 $columns = $thisMetaData->getColumns();
168 for ($i = 0; $i < $this->getRowCount(); $i++) {
169 foreach ($columns as $columnName) {
170 if ($this->getValue($i, $columnName) != $other->getValue($i, $columnName)) {
171 throw new Exception("Expected value of {$this->getValue($i, $columnName)} for row {$i} column {$columnName}, has a value of {$other->getValue($i, $columnName)}");
179 public function __toString()
181 $columns = $this->getTableMetaData()->getColumns();
183 $lineSeperator = str_repeat('+----------------------', count($columns)) . "+\n";
184 $lineLength = strlen($lineSeperator) - 1;
186 $tableString = $lineSeperator;
187 $tableString .= '| ' . str_pad($this->getTableMetaData()->getTableName(), $lineLength - 4, ' ', STR_PAD_RIGHT) . " |\n";
188 $tableString .= $lineSeperator;
189 $tableString .= $this->rowToString($columns);
190 $tableString .= $lineSeperator;
192 for ($i = 0; $i < $this->getRowCount(); $i++) {
194 foreach ($columns as $columnName) {
195 $values[] = $this->getValue($i, $columnName);
198 $tableString .= $this->rowToString($values);
199 $tableString .= $lineSeperator;
202 return "\n" . $tableString . "\n";
205 protected function rowToString(Array $row)
208 foreach ($row as $value) {
209 if (is_null($value)) {
212 $rowString .= '| ' . str_pad(substr($value, 0, 20), 20, ' ', STR_PAD_BOTH) . ' ';
215 return $rowString . "|\n";