๐Ÿ“ฆ jclapp23 / setfive_apply

๐Ÿ“„ databaseTest.php ยท 149 lines
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
147
148
149<?php 

require_once "DB.class.php";
require_once "config.class.php";

class DatabaseTest extends PHPUnit_Framework_TestCase
{
	
	public function testGetConnection(){
		$db = new DB(array('dsn'=>Config::$dsn,'username'=>Config::$username,'password'=>Config::$password));
		$con = $db->getConnection();
		$this->assertInstanceOf( 'PDO', $con, "getConnection did not return a PDO object." );
	}
	
	/**
	 * @depends testGetConnection
	 */
	public function testInsertRow(){		
		$pdo = $this->getPDO();
		$pdo->query("TRUNCATE user");
		
		$db = new DB(array('dsn'=>Config::$dsn,'username'=>Config::$username,'password'=>Config::$password));
		$db = $db->insertRow( "user", array("first_name" => "Patrick", "last_name" => "Bateman", "id" => "5") );
		$this->assertInstanceOf('DB',$db,'insertRow did not return DB Object');

		$row = $pdo->query("SELECT COUNT(*) FROM user WHERE id = 5")->fetchAll();
		
		$this->assertCount( 1, $row, "Expected 1 row in user. Got " . count($row) );
	}
	
	/** 
	 * @depends testInsertRow
	 */	
	public function testLastInsertId(){

		$pdo = $this->getPDO();
		$pdo->query("TRUNCATE user");
		
		$db = new DB(array('dsn'=>Config::$dsn,'username'=>Config::$username,'password'=>Config::$password));
		$rowId = $db->insertRow( "user", 
						array("first_name" => "Patrick", "last_name" => "Bateman", "id" => "5") )
					->getLastInsertId();
		
		$row = $pdo->query("SELECT * FROM user WHERE id = 5")->fetchAll();		
		
		$this->assertEquals( $row[0]["id"], $rowId, "Last insert id does not match MySQL value." );
	}
	
	/**
	 * @depends testInsertRow
	 */
	public function testBulkInsert(){
		$pdo = $this->getPDO();
		$pdo->query("TRUNCATE user");
		
		$db = new DB(array('dsn'=>Config::$dsn,'username'=>Config::$username,'password'=>Config::$password));
		$db->bulkInsertRows( "user",
				 		array(array("first_name" => "Patrick", "last_name" => "Bateman", "id" => "5"),
				 			  array("first_name" => "Patrick", "last_name" => "Bateman", "id" => "7"),
				 			  array("first_name" => "Patrick", "last_name" => "Bateman", "id" => "9")));
		
		$row = $pdo->query("SELECT COUNT(*) AS c FROM user")->fetchAll();
		
		$this->assertEquals( $row[0]["c"], 3, "Number of MySQL results does not match expected." );
	}
	
	/**
	 * @depends testBulkInsert
	 */
	public function testFindById(){
		$pdo = $this->getPDO();
		$pdo->query("TRUNCATE user");
		
		$db = new DB(array('dsn'=>Config::$dsn,'username'=>Config::$username,'password'=>Config::$password));
		$rowId = $db->bulkInsertRows( "user",
				array(array("first_name" => "Patrick", "last_name" => "Bateman", "id" => "5"),
						array("first_name" => "Patrick", "last_name" => "Bateman", "id" => "7"),
						array("first_name" => "Patrick", "last_name" => "Bateman", "id" => "9")));
		
		$pdoRows = $pdo->query("SELECT * FROM user")->fetchAll();
		$dbRow = $db->findById("user", $pdoRows[0]["id"]);
		
		$this->assertEquals( $pdoRows[0]["first_name"], $dbRow["first_name"], "Returned row does not match expected." );
	}
	
	/**
	 * @depends testBulkInsert
	 */
	public function testDelete(){

		$pdo = $this->getPDO();
		$pdo->query("TRUNCATE user");
		
		$db = new DB(array('dsn'=>Config::$dsn,'username'=>Config::$username,'password'=>Config::$password));
		$rowId = $db->bulkInsertRows( "user",
				array(array("first_name" => "Patrick", "last_name" => "Bateman", "id" => "5"),
						array("first_name" => "Patrick", "last_name" => "Bateman", "id" => "7"),
						array("first_name" => "Patrick", "last_name" => "Bateman", "id" => "9")));
		
		$pdoRows = $pdo->query("SELECT * FROM user")->fetchAll();
		$db->deleteById("user", $pdoRows[0]["id"]);
		
		$countRow = $pdo->query("SELECT COUNT(*) AS c FROM user")->fetchAll();
		$this->assertEquals( $countRow[0]["c"] , 2, "MySQL row count does not match expected." );
	}
	
	/**
	 * @depends testBulkInsert
	 */	
	public function testSelectWhere(){

		$pdo = $this->getPDO();
		$pdo->query("TRUNCATE user");
		
		$db = new DB(array('dsn'=>Config::$dsn,'username'=>Config::$username,'password'=>Config::$password));
		$rowId = $db->bulkInsertRows( "user",
				array(array("first_name" => "Larry", "last_name" => "Bateman", "id" => "5"),
						array("first_name" => "Moe", "last_name" => "Bateman", "id" => "7"),
						array("first_name" => "Curly", "last_name" => "Bateman", "id" => "9")));
		
		$rows = $db->selectWhere("user", array("first_name" => "Moe", "id" => "7"));				
		$this->assertCount( 1, $rows, "Number of results does not match expected." );
		
		$rows = $db->selectWhere("user", array("first_name" => "Moe", "id" => "17"));		
		$this->assertCount( 0, $rows, "Number of results does not match expected." );
				
	}
	
	/**
	 * @depends testInsertRow
	 */	
	public function testUpdate(){

		$pdo = $this->getPDO();
		$pdo->query("TRUNCATE user");
		
		$db = new DB(array('dsn'=>Config::$dsn,'username'=>Config::$username,'password'=>Config::$password));
		
		$db->insertRow( "user", array("first_name" => "Patrick", "last_name" => "Bateman", "id" => "5") );		
		$db->updateRow( "user", array("id" => "5"), array("first_name" => "Moe") );
		
		$row = $pdo->query("SELECT * FROM user WHERE id = 5")->fetchAll();
		$this->assertEquals( $row[0]["first_name"], "Moe", "First name did not reflect updated value." );
	}
	
	public function getPDO(){
		return new PDO(Config::$dsn, Config::$username, Config::$password);
	}
}