summaryrefslogtreecommitdiff
path: root/tests/unit/SQLMap/InheritanceTest.php
blob: 24e9f9875bd386988e8b51f2258de5b3806210ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php

require_once(dirname(__FILE__).'/BaseTest.php');

/**
 * @package System.DataAccess.SQLMap
 */
class InheritanceTest extends BaseTest
{
	function __construct()
	{
		parent::__construct();

		$this->initSqlMap();
		$this->initScript('documents-init.sql');
	}

	/// Test All document with no formula
	function testGetAllDocument() 
	{
		$list = $this->sqlmap->queryForList("GetAllDocument");

		$this->assertEquals(6, count($list));
		$book = $list[0];
		$this->assertBook($book, 1, "The World of Null-A", 55);

		$book = $list[1];
		$this->assertBook($book, 3, "Lord of the Rings", 3587);

		$document = $list[2];
		$this->assertDocument($document, 5, "Le Monde");

		$document = $list[3];
		$this->assertDocument($document, 6, "Foundation");

		$news = $list[4];
		$this->assertNewspaper($news, 2, "Le Progres de Lyon", "Lyon");

		$document = $list[5];
		$this->assertDocument($document, 4, "Le Canard enchaine");
	}

	/// Test All document in a typed collection
	function testGetTypedCollection() 
	{
		$list = $this->sqlmap->queryForList("GetTypedCollection");

		$this->assertEquals(6, $list->getCount());

		$book = $list[0];
		$this->assertBook($book, 1, "The World of Null-A", 55);

		$book = $list[1];
		$this->assertBook($book, 3, "Lord of the Rings", 3587);

		$document = $list[2];
		$this->assertDocument($document, 5, "Le Monde");

		$document = $list[3];
		$this->assertDocument($document, 6, "Foundation");

		$news = $list[4];
		$this->assertNewspaper($news, 2, "Le Progres de Lyon", "Lyon");

		$document = $list[5];
		$this->assertDocument($document, 4, "Le Canard enchaine");
	}

	/// Test All document with Custom Type Handler
	function testGetAllDocumentWithCustomTypeHandler() 
	{

		//register the custom inheritance type handler
		$this->sqlmap->getTypeHandlerFactory()->register(
			'CustomInheritance', new CustomInheritance);

		$list = $this->sqlmap->queryForList("GetAllDocumentWithCustomTypeHandler");

		$this->assertEquals(6, count($list));
		$book = $list[0];
		$this->assertBook($book, 1, "The World of Null-A", 55);

		$book = $list[1];
		$this->assertBook($book, 3, "Lord of the Rings", 3587);

		$news = $list[2];
		$this->assertNewspaper($news, 5, "Le Monde", "Paris");

		$book = $list[3];
		$this->assertBook($book, 6, "Foundation", 557);

		$news = $list[4];
		$this->assertNewspaper($news, 2, "Le Progres de Lyon", "Lyon");

		$news = $list[5];
		$this->assertNewspaper($news, 4, "Le Canard enchaine", "Paris");
	}

	function AssertDocument(Document $document, $id, $title)
	{
		$this->assertEquals($id, $document->getID());
		$this->assertEquals($title, $document->getTitle());
	}

	function AssertBook(Book $book, $id, $title, $pageNumber)
	{
		$this->assertEquals($id, $book->getId());
		$this->assertEquals($title, $book->getTitle());
		$this->assertEquals($pageNumber, (int)$book->getPageNumber());
	}

	function AssertNewspaper(Newspaper $news, $id, $title, $city)
	{
		$this->assertEquals($id, $news->getId());
		$this->assertEquals($title, $news->getTitle());
		$this->assertEquals($city, $news->getCity());
	}
}


class CustomInheritance implements ITypeHandlerCallback
{
	public function getResult($type)
	{
		switch ($type)
		{
			case 'Monograph': case 'Book': 
				return 'Book';
			case 'Tabloid': case 'Broadsheet': case 'Newspaper':
				return 'Newspaper';
			default: 
				return 'Document';
		}
	}

	public function getParameter($parameter)
	{
		throw new TDataMapperException('not implemented');
	}

	public function createNewInstance()
	{
		throw new TDataMapperException('can not create');
	}
}
?>