| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* PHPUnit
|
|
|
4 |
*
|
|
|
5 |
* Copyright (c) 2002-2010, Sebastian Bergmann <sb@sebastian-bergmann.de>.
|
|
|
6 |
* All rights reserved.
|
|
|
7 |
*
|
|
|
8 |
* Redistribution and use in source and binary forms, with or without
|
|
|
9 |
* modification, are permitted provided that the following conditions
|
|
|
10 |
* are met:
|
|
|
11 |
*
|
|
|
12 |
* * Redistributions of source code must retain the above copyright
|
|
|
13 |
* notice, this list of conditions and the following disclaimer.
|
|
|
14 |
*
|
|
|
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
|
|
|
18 |
* distribution.
|
|
|
19 |
*
|
|
|
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.
|
|
|
23 |
*
|
|
|
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.
|
|
|
36 |
*
|
|
|
37 |
* @category Testing
|
|
|
38 |
* @package PHPUnit
|
|
|
39 |
* @author Mike Lively <m@digitalsandwich.com>
|
|
|
40 |
* @copyright 2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
|
|
|
41 |
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
|
|
42 |
* @link http://www.phpunit.de/
|
|
|
43 |
* @since File available since Release 3.3.0
|
|
|
44 |
*/
|
|
|
45 |
|
|
|
46 |
require_once 'PHPUnit/Framework.php';
|
|
|
47 |
require_once 'PHPUnit/Util/Filter.php';
|
|
|
48 |
require_once 'PHPUnit/Extensions/Database/DataSet/ITable.php';
|
|
|
49 |
|
|
|
50 |
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Allows for replacing arbitrary strings in your data sets with other values.
|
|
|
54 |
*
|
|
|
55 |
* @category Testing
|
|
|
56 |
* @package PHPUnit
|
|
|
57 |
* @author Mike Lively <m@digitalsandwich.com>
|
|
|
58 |
* @copyright 2010 Mike Lively <m@digitalsandwich.com>
|
|
|
59 |
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
|
|
60 |
* @version Release: 3.4.15
|
|
|
61 |
* @link http://www.phpunit.de/
|
|
|
62 |
* @since Class available since Release 3.3.0
|
|
|
63 |
* @todo When setTableMetaData() is taken out of the AbstractTable this class should extend AbstractTable.
|
|
|
64 |
*/
|
|
|
65 |
class PHPUnit_Extensions_Database_DataSet_ReplacementTable implements PHPUnit_Extensions_Database_DataSet_ITable
|
|
|
66 |
{
|
|
|
67 |
/**
|
|
|
68 |
* @var PHPUnit_Extensions_Database_DataSet_ITable
|
|
|
69 |
*/
|
|
|
70 |
protected $table;
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* @var array
|
|
|
74 |
*/
|
|
|
75 |
protected $fullReplacements;
|
|
|
76 |
|
|
|
77 |
/**
|
|
|
78 |
* @var array
|
|
|
79 |
*/
|
|
|
80 |
protected $subStrReplacements;
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* Creates a new replacement table
|
|
|
84 |
*
|
|
|
85 |
* @param PHPUnit_Extensions_Database_DataSet_ITable $table
|
|
|
86 |
* @param array $fullReplacements
|
|
|
87 |
* @param array $subStrReplacements
|
|
|
88 |
*/
|
|
|
89 |
public function __construct(PHPUnit_Extensions_Database_DataSet_ITable $table, Array $fullReplacements = array(), Array $subStrReplacements = array())
|
|
|
90 |
{
|
|
|
91 |
$this->table = $table;
|
|
|
92 |
$this->fullReplacements = $fullReplacements;
|
|
|
93 |
$this->subStrReplacements = $subStrReplacements;
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
/**
|
|
|
97 |
* Adds a new full replacement
|
|
|
98 |
*
|
|
|
99 |
* Full replacements will only replace values if the FULL value is a match
|
|
|
100 |
*
|
|
|
101 |
* @param string $value
|
|
|
102 |
* @param string $replacement
|
|
|
103 |
*/
|
|
|
104 |
public function addFullReplacement($value, $replacement)
|
|
|
105 |
{
|
|
|
106 |
$this->fullReplacements[$value] = $replacement;
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
/**
|
|
|
110 |
* Adds a new substr replacement
|
|
|
111 |
*
|
|
|
112 |
* Substr replacements will replace all occurances of the substr in every column
|
|
|
113 |
*
|
|
|
114 |
* @param string $value
|
|
|
115 |
* @param string $replacement
|
|
|
116 |
*/
|
|
|
117 |
public function addSubStrReplacement($value, $replacement)
|
|
|
118 |
{
|
|
|
119 |
$this->subStrReplacements[$value] = $replacement;
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
/**
|
|
|
123 |
* Returns the table's meta data.
|
|
|
124 |
*
|
|
|
125 |
* @return PHPUnit_Extensions_Database_DataSet_ITableMetaData
|
|
|
126 |
*/
|
|
|
127 |
public function getTableMetaData()
|
|
|
128 |
{
|
|
|
129 |
return $this->table->getTableMetaData();
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* Returns the number of rows in this table.
|
|
|
134 |
*
|
|
|
135 |
* @return int
|
|
|
136 |
*/
|
|
|
137 |
public function getRowCount()
|
|
|
138 |
{
|
|
|
139 |
return $this->table->getRowCount();
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
/**
|
|
|
143 |
* Returns the value for the given column on the given row.
|
|
|
144 |
*
|
|
|
145 |
* @param int $row
|
|
|
146 |
* @param int $column
|
|
|
147 |
*/
|
|
|
148 |
public function getValue($row, $column)
|
|
|
149 |
{
|
|
|
150 |
return $this->getReplacedValue($this->table->getValue($row, $column));
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
/**
|
|
|
154 |
* Returns the an associative array keyed by columns for the given row.
|
|
|
155 |
*
|
|
|
156 |
* @param int $row
|
|
|
157 |
* @return array
|
|
|
158 |
*/
|
|
|
159 |
public function getRow($row)
|
|
|
160 |
{
|
|
|
161 |
$row = $this->table->getRow($row);
|
|
|
162 |
|
|
|
163 |
return array_map(array($this, 'getReplacedValue'), $row);
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
/**
|
|
|
167 |
* Asserts that the given table matches this table.
|
|
|
168 |
*
|
|
|
169 |
* @param PHPUnit_Extensions_Database_DataSet_ITable $other
|
|
|
170 |
*/
|
|
|
171 |
public function assertEquals(PHPUnit_Extensions_Database_DataSet_ITable $other)
|
|
|
172 |
{
|
|
|
173 |
$thisMetaData = $this->getTableMetaData();
|
|
|
174 |
$otherMetaData = $other->getTableMetaData();
|
|
|
175 |
|
|
|
176 |
$thisMetaData->assertEquals($otherMetaData);
|
|
|
177 |
|
|
|
178 |
if ($this->getRowCount() != $other->getRowCount()) {
|
|
|
179 |
throw new Exception("Expected row count of {$this->getRowCount()}, has a row count of {$other->getRowCount()}");
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
$columns = $thisMetaData->getColumns();
|
|
|
183 |
for ($i = 0; $i < $this->getRowCount(); $i++) {
|
|
|
184 |
foreach ($columns as $columnName) {
|
|
|
185 |
if ($this->getValue($i, $columnName) != $other->getValue($i, $columnName)) {
|
|
|
186 |
throw new Exception("Expected value of {$this->getValue($i, $columnName)} for row {$i} column {$columnName}, has a value of {$other->getValue($i, $columnName)}");
|
|
|
187 |
}
|
|
|
188 |
}
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
return TRUE;
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
public function __toString()
|
|
|
195 |
{
|
|
|
196 |
$columns = $this->getTableMetaData()->getColumns();
|
|
|
197 |
|
|
|
198 |
$lineSeperator = str_repeat('+----------------------', count($columns)) . "+\n";
|
|
|
199 |
$lineLength = strlen($lineSeperator) - 1;
|
|
|
200 |
|
|
|
201 |
$tableString = $lineSeperator;
|
|
|
202 |
$tableString .= '| ' . str_pad($this->getTableMetaData()->getTableName(), $lineLength - 4, ' ', STR_PAD_RIGHT) . " |\n";
|
|
|
203 |
$tableString .= $lineSeperator;
|
|
|
204 |
$tableString .= $this->rowToString($columns);
|
|
|
205 |
$tableString .= $lineSeperator;
|
|
|
206 |
|
|
|
207 |
for ($i = 0; $i < $this->getRowCount(); $i++) {
|
|
|
208 |
$values = array();
|
|
|
209 |
foreach ($columns as $columnName) {
|
|
|
210 |
$values[] = $this->getValue($i, $columnName);
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
$tableString .= $this->rowToString($values);
|
|
|
214 |
$tableString .= $lineSeperator;
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
return "\n" . $tableString . "\n";
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
protected function rowToString(Array $row)
|
|
|
221 |
{
|
|
|
222 |
$rowString = '';
|
|
|
223 |
foreach ($row as $value) {
|
|
|
224 |
if (is_null($value)) {
|
|
|
225 |
$value = 'NULL';
|
|
|
226 |
}
|
|
|
227 |
$rowString .= '| ' . str_pad(substr($value, 0, 20), 20, ' ', STR_PAD_BOTH) . ' ';
|
|
|
228 |
}
|
|
|
229 |
|
|
|
230 |
return $rowString . "|\n";
|
|
|
231 |
}
|
|
|
232 |
|
|
|
233 |
protected function getReplacedValue($value)
|
|
|
234 |
{
|
|
|
235 |
if (is_scalar($value) && array_key_exists((string)$value, $this->fullReplacements))
|
|
|
236 |
{
|
|
|
237 |
return $this->fullReplacements[$value];
|
|
|
238 |
}
|
|
|
239 |
elseif (count($this->subStrReplacements))
|
|
|
240 |
{
|
|
|
241 |
return str_replace(array_keys($this->subStrReplacements), array_values($this->subStrReplacements), $value);
|
|
|
242 |
}
|
|
|
243 |
else
|
|
|
244 |
{
|
|
|
245 |
return $value;
|
|
|
246 |
}
|
|
|
247 |
}
|
|
|
248 |
}
|
|
|
249 |
?>
|