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