Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
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.2.0
44
 */
45
 
46
require_once 'PHPUnit/Framework.php';
47
require_once 'PHPUnit/Util/Filter.php';
48
 
49
require_once 'PHPUnit/Extensions/Database/DataSet/ITable.php';
50
 
51
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
52
 
53
/**
54
 * Provides a basic functionality for dbunit tables
55
 *
56
 * @category   Testing
57
 * @package    PHPUnit
58
 * @author     Mike Lively <m@digitalsandwich.com>
59
 * @copyright  2010 Mike Lively <m@digitalsandwich.com>
60
 * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
61
 * @version    Release: 3.4.15
62
 * @link       http://www.phpunit.de/
63
 * @since      Class available since Release 3.2.0
64
 */
65
class PHPUnit_Extensions_Database_DataSet_AbstractTable implements PHPUnit_Extensions_Database_DataSet_ITable
66
{
67
 
68
    /**
69
     * @var PHPUnit_Extensions_Database_DataSet_ITableMetaData
70
     */
71
    protected $tableMetaData;
72
 
73
    /**
74
     * A 2-dimensional array containing the data for this table.
75
     *
76
     * @var array
77
     */
78
    protected $data;
79
 
80
    /**
81
     * Sets the metadata for this table.
82
     *
83
     * @param PHPUnit_Extensions_Database_DataSet_ITableMetaData $tableMetaData
84
     * @deprecated
85
     */
86
    protected function setTableMetaData(PHPUnit_Extensions_Database_DataSet_ITableMetaData $tableMetaData)
87
    {
88
        $this->tableMetaData = $tableMetaData;
89
    }
90
 
91
    /**
92
     * Returns the table's meta data.
93
     *
94
     * @return PHPUnit_Extensions_Database_DataSet_ITableMetaData
95
     */
96
    public function getTableMetaData()
97
    {
98
        return $this->tableMetaData;
99
    }
100
 
101
    /**
102
     * Returns the number of rows in this table.
103
     *
104
     * @return int
105
     */
106
    public function getRowCount()
107
    {
108
        return count($this->data);
109
    }
110
 
111
    /**
112
     * Returns the value for the given column on the given row.
113
     *
114
     * @param int $row
115
     * @param int $column
116
     * @todo reorganize this function to throw the exception first.
117
     */
118
    public function getValue($row, $column)
119
    {
120
        if (isset($this->data[$row][$column])) {
121
            return (string)$this->data[$row][$column];
122
        } else {
123
            if (!in_array($column, $this->getTableMetaData()->getColumns()) || $this->getRowCount() <= $row) {
124
                throw new InvalidArgumentException("The given row ({$row}) and column ({$column}) do not exist in table {$this->getTableMetaData()->getTableName()}");
125
            } else {
126
                return NULL;
127
            }
128
        }
129
    }
130
 
131
    /**
132
     * Returns the an associative array keyed by columns for the given row.
133
     *
134
     * @param int $row
135
     * @return array
136
     */
137
    public function getRow($row)
138
    {
139
        if (isset($this->data[$row])) {
140
            return $this->data[$row];
141
        } else {
142
            if ($this->getRowCount() <= $row) {
143
                throw new InvalidArgumentException("The given row ({$row}) does not exist in table {$this->getTableMetaData()->getTableName()}");
144
            } else {
145
                return NULL;
146
            }
147
        }
148
    }
149
 
150
    /**
151
     * Asserts that the given table matches this table.
152
     *
153
     * @param PHPUnit_Extensions_Database_DataSet_ITable $other
154
     */
155
    public function assertEquals(PHPUnit_Extensions_Database_DataSet_ITable $other)
156
    {
157
        $thisMetaData = $this->getTableMetaData();
158
        $otherMetaData = $other->getTableMetaData();
159
 
160
        $thisMetaData->assertEquals($otherMetaData);
161
 
162
        if ($this->getRowCount() != $other->getRowCount()) {
163
            throw new Exception("Expected row count of {$this->getRowCount()}, has a row count of {$other->getRowCount()}");
164
        }
165
 
166
        $columns = $thisMetaData->getColumns();
167
        for ($i = 0; $i < $this->getRowCount(); $i++) {
168
            foreach ($columns as $columnName) {
169
                if ($this->getValue($i, $columnName) != $other->getValue($i, $columnName)) {
170
                    throw new Exception("Expected value of {$this->getValue($i, $columnName)} for row {$i} column {$columnName}, has a value of {$other->getValue($i, $columnName)}");
171
                }
172
            }
173
        }
174
 
175
        return TRUE;
176
    }
177
 
178
    public function __toString()
179
    {
180
        $columns = $this->getTableMetaData()->getColumns();
181
 
182
        $lineSeperator = str_repeat('+----------------------', count($columns)) . "+\n";
183
        $lineLength = strlen($lineSeperator) - 1;
184
 
185
        $tableString = $lineSeperator;
186
        $tableString .= '| ' . str_pad($this->getTableMetaData()->getTableName(), $lineLength - 4, ' ', STR_PAD_RIGHT) . " |\n";
187
        $tableString .= $lineSeperator;
188
        $tableString .= $this->rowToString($columns);
189
        $tableString .= $lineSeperator;
190
 
191
        for ($i = 0; $i < $this->getRowCount(); $i++) {
192
            $values = array();
193
            foreach ($columns as $columnName) {
194
                $values[] = $this->getValue($i, $columnName);
195
            }
196
 
197
            $tableString .= $this->rowToString($values);
198
            $tableString .= $lineSeperator;
199
        }
200
 
201
        return "\n" . $tableString . "\n";
202
    }
203
 
204
    protected function rowToString(Array $row)
205
    {
206
        $rowString = '';
207
        foreach ($row as $value) {
208
            if (is_null($value)) {
209
                $value = 'NULL';
210
            }
211
            $rowString .= '| ' . str_pad(substr($value, 0, 20), 20, ' ', STR_PAD_BOTH) . ' ';
212
        }
213
 
214
        return $rowString . "|\n";
215
    }
216
}
217
?>