/craft/app/vendor/phpunit/dbunit/PHPUnit/Extensions/Database/DataSet/ReplacementTable.php

https://gitlab.com/madebycloud/derekman · PHP · 217 lines · 107 code · 31 blank · 79 comment · 15 complexity · 5cc65efc64cf4ba628fca700270db830 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of DBUnit.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * Allows for replacing arbitrary strings in your data sets with other values.
  12. *
  13. * @package DbUnit
  14. * @author Mike Lively <m@digitalsandwich.com>
  15. * @copyright 2010-2014 Mike Lively <m@digitalsandwich.com>
  16. * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
  17. * @version Release: @package_version@
  18. * @link http://www.phpunit.de/
  19. * @since Class available since Release 1.0.0
  20. * @todo When setTableMetaData() is taken out of the AbstractTable this class should extend AbstractTable.
  21. */
  22. class PHPUnit_Extensions_Database_DataSet_ReplacementTable implements PHPUnit_Extensions_Database_DataSet_ITable
  23. {
  24. /**
  25. * @var PHPUnit_Extensions_Database_DataSet_ITable
  26. */
  27. protected $table;
  28. /**
  29. * @var array
  30. */
  31. protected $fullReplacements;
  32. /**
  33. * @var array
  34. */
  35. protected $subStrReplacements;
  36. /**
  37. * Creates a new replacement table
  38. *
  39. * @param PHPUnit_Extensions_Database_DataSet_ITable $table
  40. * @param array $fullReplacements
  41. * @param array $subStrReplacements
  42. */
  43. public function __construct(PHPUnit_Extensions_Database_DataSet_ITable $table, Array $fullReplacements = array(), Array $subStrReplacements = array())
  44. {
  45. $this->table = $table;
  46. $this->fullReplacements = $fullReplacements;
  47. $this->subStrReplacements = $subStrReplacements;
  48. }
  49. /**
  50. * Adds a new full replacement
  51. *
  52. * Full replacements will only replace values if the FULL value is a match
  53. *
  54. * @param string $value
  55. * @param string $replacement
  56. */
  57. public function addFullReplacement($value, $replacement)
  58. {
  59. $this->fullReplacements[$value] = $replacement;
  60. }
  61. /**
  62. * Adds a new substr replacement
  63. *
  64. * Substr replacements will replace all occurances of the substr in every column
  65. *
  66. * @param string $value
  67. * @param string $replacement
  68. */
  69. public function addSubStrReplacement($value, $replacement)
  70. {
  71. $this->subStrReplacements[$value] = $replacement;
  72. }
  73. /**
  74. * Returns the table's meta data.
  75. *
  76. * @return PHPUnit_Extensions_Database_DataSet_ITableMetaData
  77. */
  78. public function getTableMetaData()
  79. {
  80. return $this->table->getTableMetaData();
  81. }
  82. /**
  83. * Returns the number of rows in this table.
  84. *
  85. * @return int
  86. */
  87. public function getRowCount()
  88. {
  89. return $this->table->getRowCount();
  90. }
  91. /**
  92. * Returns the value for the given column on the given row.
  93. *
  94. * @param int $row
  95. * @param int $column
  96. */
  97. public function getValue($row, $column)
  98. {
  99. return $this->getReplacedValue($this->table->getValue($row, $column));
  100. }
  101. /**
  102. * Returns the an associative array keyed by columns for the given row.
  103. *
  104. * @param int $row
  105. * @return array
  106. */
  107. public function getRow($row)
  108. {
  109. $row = $this->table->getRow($row);
  110. return array_map(array($this, 'getReplacedValue'), $row);
  111. }
  112. /**
  113. * Asserts that the given table matches this table.
  114. *
  115. * @param PHPUnit_Extensions_Database_DataSet_ITable $other
  116. */
  117. public function matches(PHPUnit_Extensions_Database_DataSet_ITable $other)
  118. {
  119. $thisMetaData = $this->getTableMetaData();
  120. $otherMetaData = $other->getTableMetaData();
  121. if (!$thisMetaData->matches($otherMetaData) ||
  122. $this->getRowCount() != $other->getRowCount()) {
  123. return FALSE;
  124. }
  125. $columns = $thisMetaData->getColumns();
  126. $rowCount = $this->getRowCount();
  127. for ($i = 0; $i < $rowCount; $i++) {
  128. foreach ($columns as $columnName) {
  129. $thisValue = $this->getValue($i, $columnName);
  130. $otherValue = $other->getValue($i, $columnName);
  131. if (is_numeric($thisValue) && is_numeric($otherValue)) {
  132. if ($thisValue != $otherValue) {
  133. return FALSE;
  134. }
  135. } elseif ($thisValue !== $otherValue) {
  136. return FALSE;
  137. }
  138. }
  139. }
  140. return TRUE;
  141. }
  142. public function __toString()
  143. {
  144. $columns = $this->getTableMetaData()->getColumns();
  145. $lineSeperator = str_repeat('+----------------------', count($columns)) . "+\n";
  146. $lineLength = strlen($lineSeperator) - 1;
  147. $tableString = $lineSeperator;
  148. $tableString .= '| ' . str_pad($this->getTableMetaData()->getTableName(), $lineLength - 4, ' ', STR_PAD_RIGHT) . " |\n";
  149. $tableString .= $lineSeperator;
  150. $tableString .= $this->rowToString($columns);
  151. $tableString .= $lineSeperator;
  152. $rowCount = $this->getRowCount();
  153. for ($i = 0; $i < $rowCount; $i++) {
  154. $values = array();
  155. foreach ($columns as $columnName) {
  156. $values[] = $this->getValue($i, $columnName);
  157. }
  158. $tableString .= $this->rowToString($values);
  159. $tableString .= $lineSeperator;
  160. }
  161. return "\n" . $tableString . "\n";
  162. }
  163. protected function rowToString(Array $row)
  164. {
  165. $rowString = '';
  166. foreach ($row as $value) {
  167. if (is_null($value)) {
  168. $value = 'NULL';
  169. }
  170. $rowString .= '| ' . str_pad(substr($value, 0, 20), 20, ' ', STR_PAD_BOTH) . ' ';
  171. }
  172. return $rowString . "|\n";
  173. }
  174. protected function getReplacedValue($value)
  175. {
  176. if (is_scalar($value) && array_key_exists((string)$value, $this->fullReplacements)) {
  177. return $this->fullReplacements[$value];
  178. }
  179. else if (count($this->subStrReplacements) && isset($value)) {
  180. return str_replace(array_keys($this->subStrReplacements), array_values($this->subStrReplacements), $value);
  181. }
  182. else {
  183. return $value;
  184. }
  185. }
  186. }