1<?php
2/*
3 * Created on Jul 01, 2008
4 *
5 * by Stefan Klug ( stefan.klug (at) gmail.com )
6 */
7
8class Doctrine_Ticket_1195_TestCase extends Doctrine_UnitTestCase
9{
10	public function prepareTables()
11    {
12    	$this->tables = array();
13    	$this->tables[] = 'T1195_Item';
14    	$this->tables[] = 'T1195_Ref';
15
16    	parent :: prepareTables();
17    }
18
19    public function prepareData()
20    {
21        $item = new T1195_Item();
22        $item->col1 = "a";
23        $item->col2 = "a";
24        $item->save();
25
26        $item = new T1195_Item();
27        $item->col1 = "a";
28        $item->col2 = "b";
29        $item->save();
30
31        $item = new T1195_Item();
32        $item->col1 = "b";
33        $item->col2 = "a";
34        $item->save();
35
36        $item = new T1195_Item();
37        $item->col1 = "b";
38        $item->col2 = "b";
39        $item->save();
40
41        $ref = new T1195_Ref();
42        $ref->Item = $item;
43        $ref->save();
44
45        $ref = new T1195_Ref();
46        $ref->Item = $item;
47        $ref->save();
48
49    }
50
51    public function testRawSQLaddWhere()
52    {
53    	//this checks for an error in parseDqlQueryPart
54
55    	$query = new Doctrine_RawSql();
56        $q = $query->select('{i.*}')
57				->addComponent('i', 'T1195_Item i')
58				->from('items i')
59				->addWhere('i.col1 = ?','a')
60				->addWhere('i.col2 = ?','a');
61
62		$res = $q->execute();
63
64        $this->assertEqual($res->count(), 1);
65    }
66
67    public function testRawSQLDistinct()
68    {
69    	$q = new Doctrine_RawSql();
70        $q = $q->select('{i.*}')
71				->addComponent('i', 'T1195_Item i')
72				->from('ref r')
73				->leftJoin('items i ON r.item_id=i.id');
74
75
76		$res = $q->execute(array(),Doctrine_Core::HYDRATE_ARRAY);
77		$this->assertEqual(sizeof($res), 2);
78
79		$q->distinct();
80		$res = $q->execute(array(),Doctrine_Core::HYDRATE_ARRAY);
81		$this->assertEqual(sizeof($res), 1);
82    }
83
84	public function testRawSQLCount()
85    {
86    	$q = new Doctrine_RawSql();
87        $q = $q->select('{i.*}')
88				->addComponent('i', 'T1195_Item i')
89				->from('items i');
90
91		if ( !method_exists( $q, 'count' ))
92		{
93			$this->fail("The query doesn't have a count() method");
94			return;
95		}
96
97		$res = $q->count();
98		$this->assertEqual($res, 4);
99
100    }
101}
102
103class T1195_Item extends Doctrine_Record
104{
105    public function setTableDefinition()
106    {
107       	$this->setTableName('items');
108        $this->hasColumn('id', 'integer', null, array('autoincrement' => true, 'primary' => true, 'notnull' => true));
109        $this->hasColumn('col1', 'string', 10);
110        $this->hasColumn('col2', 'string', 10);
111    }
112}
113
114class T1195_Ref extends Doctrine_Record
115{
116    public function setTableDefinition()
117    {
118       	$this->setTableName('ref');
119        $this->hasColumn('id', 'integer', null, array('autoincrement' => true, 'primary' => true, 'notnull' => true));
120        $this->hasColumn('item_id', 'integer', null);
121    }
122
123    public function setUp()
124    {
125    	$this->hasOne('T1195_Item as Item', array('local' => 'item_id', 'foreign' => 'id'));
126    }
127}
128