]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/Tests/Extensions/Database/DataSet/ReplacementTableTest.php
Added unit tests.
[Github/sugarcrm.git] / tests / PHPUnit / Tests / Extensions / Database / DataSet / ReplacementTableTest.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/TestCase.php';
48 require_once 'PHPUnit/Extensions/Database/DataSet/ReplacementTable.php';
49 require_once 'PHPUnit/Extensions/Database/DataSet/DefaultTable.php';
50 require_once 'PHPUnit/Extensions/Database/DataSet/DefaultTableMetaData.php';
51 require_once 'PHPUnit/Extensions/Database/TestCase.php';
52
53 /**
54  * @category   Testing
55  * @package    PHPUnit
56  * @author     Mike Lively <m@digitalsandwich.com>
57  * @copyright  2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
58  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
59
60  * @link       http://www.phpunit.de/
61  * @since      File available since Release 3.3.0
62  */
63 class Extensions_Database_DataSet_ReplacementTableTest extends PHPUnit_Framework_TestCase
64 {
65     /**
66      * @var PHPUnit_Extensions_Database_DataSet_DefaultTable
67      */
68     protected $startingTable;
69
70     public function setUp()
71     {
72         $tableMetaData = new PHPUnit_Extensions_Database_DataSet_DefaultTableMetaData(
73             'table1', array('table1_id', 'column1', 'column2', 'column3', 'column4')
74         );
75
76         $table = new PHPUnit_Extensions_Database_DataSet_DefaultTable($tableMetaData);
77
78         $table->addRow(array(
79             'table1_id' => 1,
80             'column1' => 'My name is %%%name%%%',
81             'column2' => 200,
82             'column3' => 34.64,
83             'column4' => 'yghkf;a  hahfg8ja h;'
84         ));
85         $table->addRow(array(
86             'table1_id' => 2,
87             'column1' => 'hk;afg',
88             'column2' => 654,
89             'column3' => 46.54,
90             'column4' => '24rwehhads'
91         ));
92         $table->addRow(array(
93             'table1_id' => 3,
94             'column1' => 'ha;gyt',
95             'column2' => 462,
96             'column3' => '[NULL] not really',
97             'column4' => '[NULL]'
98         ));
99
100         $this->startingTable = $table;
101     }
102
103     public function testNoReplacement()
104     {
105         PHPUnit_Extensions_Database_TestCase::assertTablesEqual(
106             $this->startingTable,
107             new PHPUnit_Extensions_Database_DataSet_ReplacementTable($this->startingTable)
108         );
109     }
110
111     public function testFullReplacement()
112     {
113         $tableMetaData = new PHPUnit_Extensions_Database_DataSet_DefaultTableMetaData(
114             'table1', array('table1_id', 'column1', 'column2', 'column3', 'column4')
115         );
116
117         $table = new PHPUnit_Extensions_Database_DataSet_DefaultTable($tableMetaData);
118
119         $table->addRow(array(
120             'table1_id' => 1,
121             'column1' => 'My name is %%%name%%%',
122             'column2' => 200,
123             'column3' => 34.64,
124             'column4' => 'yghkf;a  hahfg8ja h;'
125         ));
126         $table->addRow(array(
127             'table1_id' => 2,
128             'column1' => 'hk;afg',
129             'column2' => 654,
130             'column3' => 46.54,
131             'column4' => '24rwehhads'
132         ));
133         $table->addRow(array(
134             'table1_id' => 3,
135             'column1' => 'ha;gyt',
136             'column2' => 462,
137             'column3' => '[NULL] not really',
138             'column4' => NULL
139         ));
140
141         $actual = new PHPUnit_Extensions_Database_DataSet_ReplacementTable($this->startingTable);
142         $actual->addFullReplacement('[NULL]', NULL);
143
144         PHPUnit_Extensions_Database_TestCase::assertTablesEqual($table, $actual);
145     }
146
147     public function testSubStrReplacement()
148     {
149         $tableMetaData = new PHPUnit_Extensions_Database_DataSet_DefaultTableMetaData(
150             'table1', array('table1_id', 'column1', 'column2', 'column3', 'column4')
151         );
152
153         $table = new PHPUnit_Extensions_Database_DataSet_DefaultTable($tableMetaData);
154
155         $table->addRow(array(
156             'table1_id' => 1,
157             'column1' => 'My name is Mike Lively',
158             'column2' => 200,
159             'column3' => 34.64,
160             'column4' => 'yghkf;a  hahfg8ja h;'
161         ));
162         $table->addRow(array(
163             'table1_id' => 2,
164             'column1' => 'hk;afg',
165             'column2' => 654,
166             'column3' => 46.54,
167             'column4' => '24rwehhads'
168         ));
169         $table->addRow(array(
170             'table1_id' => 3,
171             'column1' => 'ha;gyt',
172             'column2' => 462,
173             'column3' => '[NULL] not really',
174             'column4' => '[NULL]'
175         ));
176
177         $actual = new PHPUnit_Extensions_Database_DataSet_ReplacementTable($this->startingTable);
178         $actual->addSubStrReplacement('%%%name%%%', 'Mike Lively');
179
180         PHPUnit_Extensions_Database_TestCase::assertTablesEqual($table, $actual);
181     }
182
183     public function testConstructorReplacements()
184     {
185         $tableMetaData = new PHPUnit_Extensions_Database_DataSet_DefaultTableMetaData(
186             'table1', array('table1_id', 'column1', 'column2', 'column3', 'column4')
187         );
188
189         $table = new PHPUnit_Extensions_Database_DataSet_DefaultTable($tableMetaData);
190
191         $table->addRow(array(
192             'table1_id' => 1,
193             'column1' => 'My name is Mike Lively',
194             'column2' => 200,
195             'column3' => 34.64,
196             'column4' => 'yghkf;a  hahfg8ja h;'
197         ));
198         $table->addRow(array(
199             'table1_id' => 2,
200             'column1' => 'hk;afg',
201             'column2' => 654,
202             'column3' => 46.54,
203             'column4' => '24rwehhads'
204         ));
205         $table->addRow(array(
206             'table1_id' => 3,
207             'column1' => 'ha;gyt',
208             'column2' => 462,
209             'column3' => '[NULL] not really',
210             'column4' => NULL
211         ));
212
213         $actual = new PHPUnit_Extensions_Database_DataSet_ReplacementTable(
214             $this->startingTable,
215             array('[NULL]' => NULL),
216             array('%%%name%%%' => 'Mike Lively')
217         );
218
219         PHPUnit_Extensions_Database_TestCase::assertTablesEqual($table, $actual);
220     }
221
222     public function testGetRow()
223     {
224         $actual = new PHPUnit_Extensions_Database_DataSet_ReplacementTable(
225             $this->startingTable,
226             array('[NULL]' => NULL),
227             array('%%%name%%%' => 'Mike Lively')
228         );
229
230         $this->assertEquals(
231             array(
232                 'table1_id' => 1,
233                 'column1' => 'My name is Mike Lively',
234                 'column2' => 200,
235                 'column3' => 34.64,
236                 'column4' => 'yghkf;a  hahfg8ja h;'
237             ),
238             $actual->getRow(0)
239         );
240
241         $this->assertEquals(
242             array(
243                 'table1_id' => 3,
244                 'column1' => 'ha;gyt',
245                 'column2' => 462,
246                 'column3' => '[NULL] not really',
247                 'column4' => NULL
248             ),
249             $actual->getRow(2)
250         );
251     }
252
253     public function testGetValue()
254     {
255         $actual = new PHPUnit_Extensions_Database_DataSet_ReplacementTable(
256             $this->startingTable,
257             array('[NULL]' => NULL),
258             array('%%%name%%%' => 'Mike Lively')
259         );
260
261         $this->assertNull($actual->getValue(2, 'column4'));
262         $this->assertEquals('My name is Mike Lively', $actual->getValue(0, 'column1'));
263     }
264 }
265 ?>