]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/Tests/Extensions/Database/DataSet/QueryTableTest.php
Added unit tests.
[Github/sugarcrm.git] / tests / PHPUnit / Tests / Extensions / Database / DataSet / QueryTableTest.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.2.0
45  */
46
47 require_once 'PHPUnit/Framework/TestCase.php';
48 require_once 'PHPUnit/Extensions/Database/DataSet/QueryTable.php';
49 require_once 'PHPUnit/Extensions/Database/DataSet/DefaultTable.php';
50 require_once 'PHPUnit/Extensions/Database/DB/DefaultDatabaseConnection.php';
51
52 /**
53  * @category   Testing
54  * @package    PHPUnit
55  * @author     Mike Lively <m@digitalsandwich.com>
56  * @copyright  2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
57  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
58
59  * @link       http://www.phpunit.de/
60  * @since      File available since Release 3.2.0
61  */
62 class Extensions_Database_DataSet_QueryTableTest extends PHPUnit_Framework_TestCase
63 {
64     /**
65      * @var PHPUnit_Extensions_Database_DataSet_QueryTable
66      */
67     protected $table;
68
69     public function setUp()
70     {
71         $query = "
72             SELECT
73                 'value1' as col1,
74                 'value2' as col2,
75                 'value3' as col3
76             UNION SELECT
77                 'value4' as col1,
78                 'value5' as col2,
79                 'value6' as col3
80         ";
81         $this->table = new PHPUnit_Extensions_Database_DataSet_QueryTable(
82             'table1',
83             $query,
84             new PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection(new PDO('sqlite::memory:'), 'test')
85         );
86     }
87
88     public static function providerTestGetValue()
89     {
90         return array(
91             array(0, 'col1', 'value1'),
92             array(0, 'col2', 'value2'),
93             array(0, 'col3', 'value3'),
94             array(1, 'col1', 'value4'),
95             array(1, 'col2', 'value5'),
96             array(1, 'col3', 'value6'),
97         );
98     }
99
100     public function testGetTableMetaData()
101     {
102         $metaData = new PHPUnit_Extensions_Database_DataSet_DefaultTableMetaData('table1', array('col1', 'col2', 'col3'));
103
104         $this->assertEquals($metaData, $this->table->getTableMetaData());
105     }
106
107     public function testGetRowCount()
108     {
109         $this->assertEquals(2, $this->table->getRowCount());
110     }
111
112     /**
113      * @dataProvider providerTestGetValue
114      */
115     public function testGetValue($row, $column, $value)
116     {
117         $this->assertEquals($value, $this->table->getValue($row, $column));
118     }
119
120     public function testGetRow()
121     {
122         $this->assertEquals(array('col1' => 'value1', 'col2' => 'value2', 'col3' => 'value3'), $this->table->getRow(0));
123     }
124
125     public function testAssertEquals()
126     {
127         $expected_table = new PHPUnit_Extensions_Database_DataSet_DefaultTable(new PHPUnit_Extensions_Database_DataSet_DefaultTableMetaData('table1', array('col1', 'col2', 'col3')));
128         $expected_table->addRow(array('col1' => 'value1', 'col2' => 'value2', 'col3' => 'value3'));
129         $expected_table->addRow(array('col1' => 'value4', 'col2' => 'value5', 'col3' => 'value6'));
130         $this->assertTrue($this->table->assertEquals($expected_table));
131     }
132
133     public function testAssertEqualsFails()
134     {
135         $this->setExpectedException('Exception', 'Expected row count of 2, has a row count of 3');
136
137         $expected_table = new PHPUnit_Extensions_Database_DataSet_DefaultTable(new PHPUnit_Extensions_Database_DataSet_DefaultTableMetaData('table1', array('col1', 'col2', 'col3')));
138         $expected_table->addRow(array('col1' => 'value1', 'col2' => 'value2', 'col3' => 'value3'));
139         $expected_table->addRow(array('col1' => 'value4', 'col2' => 'value5', 'col3' => 'value6'));
140         $expected_table->addRow(array('col1' => 'value7', 'col2' => 'value8', 'col3' => 'value9'));
141         $this->table->assertEquals($expected_table);
142     }
143 }
144 ?>