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.3.0
47 require_once 'PHPUnit/Framework.php';
48 require_once 'PHPUnit/Util/Filter.php';
49 require_once 'PHPUnit/Extensions/Database/DataSet/ITable.php';
51 PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
54 * Allows for replacing arbitrary strings in your data sets with other values.
58 * @author Mike Lively <m@digitalsandwich.com>
59 * @copyright 2009 Mike Lively <m@digitalsandwich.com>
60 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
61 * @version Release: 3.3.17
62 * @link http://www.phpunit.de/
63 * @since Class available since Release 3.3.0
64 * @todo When setTableMetaData() is taken out of the AbstractTable this class should extend AbstractTable.
66 class PHPUnit_Extensions_Database_DataSet_ReplacementTable implements PHPUnit_Extensions_Database_DataSet_ITable
69 * @var PHPUnit_Extensions_Database_DataSet_ITable
76 protected $fullReplacements;
81 protected $subStrReplacements;
84 * Creates a new replacement table
86 * @param PHPUnit_Extensions_Database_DataSet_ITable $table
87 * @param array $fullReplacements
88 * @param array $subStrReplacements
90 public function __construct(PHPUnit_Extensions_Database_DataSet_ITable $table, Array $fullReplacements = array(), Array $subStrReplacements = array())
92 $this->table = $table;
93 $this->fullReplacements = $fullReplacements;
94 $this->subStrReplacements = $subStrReplacements;
98 * Adds a new full replacement
100 * Full replacements will only replace values if the FULL value is a match
102 * @param string $value
103 * @param string $replacement
105 public function addFullReplacement($value, $replacement)
107 $this->fullReplacements[$value] = $replacement;
111 * Adds a new substr replacement
113 * Substr replacements will replace all occurances of the substr in every column
115 * @param string $value
116 * @param string $replacement
118 public function addSubStrReplacement($value, $replacement)
120 $this->subStrReplacements[$value] = $replacement;
124 * Returns the table's meta data.
126 * @return PHPUnit_Extensions_Database_DataSet_ITableMetaData
128 public function getTableMetaData()
130 return $this->table->getTableMetaData();
134 * Returns the number of rows in this table.
138 public function getRowCount()
140 return $this->table->getRowCount();
144 * Returns the value for the given column on the given row.
149 public function getValue($row, $column)
151 return $this->getReplacedValue($this->table->getValue($row, $column));
155 * Returns the an associative array keyed by columns for the given row.
160 public function getRow($row)
162 $row = $this->table->getRow($row);
164 return array_map(array($this, 'getReplacedValue'), $row);
168 * Asserts that the given table matches this table.
170 * @param PHPUnit_Extensions_Database_DataSet_ITable $other
172 public function assertEquals(PHPUnit_Extensions_Database_DataSet_ITable $other)
174 $thisMetaData = $this->getTableMetaData();
175 $otherMetaData = $other->getTableMetaData();
177 $thisMetaData->assertEquals($otherMetaData);
179 if ($this->getRowCount() != $other->getRowCount()) {
180 throw new Exception("Expected row count of {$this->getRowCount()}, has a row count of {$other->getRowCount()}");
183 $columns = $thisMetaData->getColumns();
184 for ($i = 0; $i < $this->getRowCount(); $i++) {
185 foreach ($columns as $columnName) {
186 if ($this->getValue($i, $columnName) != $other->getValue($i, $columnName)) {
187 throw new Exception("Expected value of {$this->getValue($i, $columnName)} for row {$i} column {$columnName}, has a value of {$other->getValue($i, $columnName)}");
195 public function __toString()
197 $columns = $this->getTableMetaData()->getColumns();
199 $lineSeperator = str_repeat('+----------------------', count($columns)) . "+\n";
200 $lineLength = strlen($lineSeperator) - 1;
202 $tableString = $lineSeperator;
203 $tableString .= '| ' . str_pad($this->getTableMetaData()->getTableName(), $lineLength - 4, ' ', STR_PAD_RIGHT) . " |\n";
204 $tableString .= $lineSeperator;
205 $tableString .= $this->rowToString($columns);
206 $tableString .= $lineSeperator;
208 for ($i = 0; $i < $this->getRowCount(); $i++) {
210 foreach ($columns as $columnName) {
211 $values[] = $this->getValue($i, $columnName);
214 $tableString .= $this->rowToString($values);
215 $tableString .= $lineSeperator;
218 return "\n" . $tableString . "\n";
221 protected function rowToString(Array $row)
224 foreach ($row as $value) {
225 if (is_null($value)) {
228 $rowString .= '| ' . str_pad(substr($value, 0, 20), 20, ' ', STR_PAD_BOTH) . ' ';
231 return $rowString . "|\n";
234 protected function getReplacedValue($value)
236 if (is_scalar($value) && array_key_exists((string)$value, $this->fullReplacements))
238 return $this->fullReplacements[$value];
240 elseif (count($this->subStrReplacements))
242 return str_replace(array_keys($this->subStrReplacements), array_values($this->subStrReplacements), $value);