]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/Tests/Extensions/Database/DataSet/QueryDataSetTest.php
Added unit tests.
[Github/sugarcrm.git] / tests / PHPUnit / Tests / Extensions / Database / DataSet / QueryDataSetTest.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/Extensions/Database/TestCase.php';
48 require_once 'PHPUnit/Extensions/Database/DataSet/QueryDataSet.php';
49 require_once dirname(__FILE__). '/../_files/DatabaseTestUtility.php';
50 //require_once 'PHPUnit/Extensions/Database/DataSet/DefaultTable.php';
51 //require_once 'PHPUnit/Extensions/Database/DB/DefaultDatabaseConnection.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.2.0
62  */
63 class Extensions_Database_DataSet_QueryDataSetTest extends PHPUnit_Extensions_Database_TestCase
64 {
65     /**
66      * @var PHPUnit_Extensions_Database_DataSet_QueryDataSet
67      */
68     protected $dataSet;
69
70     protected $pdo;
71
72     /**
73      * @return PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection
74      */
75     protected function getConnection()
76     {
77         return $this->createDefaultDBConnection($this->pdo, 'test');
78     }
79
80     protected function getDataSet()
81     {
82         return $this->createFlatXMLDataSet(dirname(__FILE__).'/../_files/XmlDataSets/QueryDataSetTest.xml');
83     }
84
85     public function setUp()
86     {
87         $this->pdo = DBUnitTestUtility::getSQLiteMemoryDB();
88         parent::setUp();
89         $this->dataSet = new PHPUnit_Extensions_Database_DataSet_QueryDataSet($this->getConnection());
90         $this->dataSet->addTable('table1');
91         $this->dataSet->addTable('query1', '
92             SELECT
93                 t1.column1 tc1, t2.column5 tc2
94             FROM
95                 table1 t1
96                 JOIN table2 t2 ON t1.table1_id = t2.table2_id
97         ');
98     }
99
100     public function testGetTable()
101     {
102         $expectedTable1 = $this->getConnection()->createDataSet(array('table1'))->getTable('table1');
103
104         $expectedTable2 = new PHPUnit_Extensions_Database_DataSet_DefaultTable(
105             new PHPUnit_Extensions_Database_DataSet_DefaultTableMetaData('query1', array('tc1', 'tc2'))
106         );
107
108         $expectedTable2->addRow(array('tc1' => 'bar', 'tc2' => 'blah'));
109
110         $this->assertTablesEqual($expectedTable1, $this->dataSet->getTable('table1'));
111         $this->assertTablesEqual($expectedTable2, $this->dataSet->getTable('query1'));
112     }
113
114     public function testGetTableNames()
115     {
116         $this->assertEquals(array('table1', 'query1'), $this->dataSet->getTableNames());
117     }
118
119     public function testCreateIterator()
120     {
121         $expectedTable1 = $this->getConnection()->createDataSet(array('table1'))->getTable('table1');
122
123         $expectedTable2 = new PHPUnit_Extensions_Database_DataSet_DefaultTable(
124             new PHPUnit_Extensions_Database_DataSet_DefaultTableMetaData('query1', array('tc1', 'tc2'))
125         );
126
127         $expectedTable2->addRow(array('tc1' => 'bar', 'tc2' => 'blah'));
128
129         foreach ($this->dataSet as $i => $table) {
130             /* @var $table PHPUnit_Extensions_Database_DataSet_ITable */
131             switch ($table->getTableMetaData()->getTableName()) {
132                 case 'table1':
133                     $this->assertTablesEqual($expectedTable1, $table);
134                     break;
135                 case 'query1':
136                     $this->assertTablesEqual($expectedTable2, $table);
137                     break;
138                 default:
139                     $this->fail('Proper keys not present from the iterator');
140             }
141         }
142     }
143 }
144 ?>