objQuery = new SC_Query(); $this->objQuery->begin(); } function tearDown() { // MySQL では CREATE TABLE がロールバックされないので DROP TABLE を行う $this->dropTestTable(); $this->objQuery->rollback(); $this->objQuery = null; } function verify() { $this->assertEquals($this->expected, $this->actual); } /** * インスタンスを取得するテストケース. */ function testGetInstance() { $this->expected = true; $this->actual = is_object($this->objQuery); $this->verify(); } /** * SC_Query::query() を使用して, CREATE TABLE を実行するテストケース. */ function testCreateTable() { $result = $this->createTestTable(); $this->expected = false; $this->actual = PEAR::isError($result); $this->verify(); } /** * SC_Query::getAll() のテストケース. */ function testGetAll() { $result = $this->createTestTable(); $result = $this->setTestData(1, '2', 'f'); $this->expected = array(array('id' => '1', 'column1' => '1', 'column2' => '2', 'column3' => 'f')); $this->actual = $this->objQuery->getAll("SELECT * FROM test_table WHERE id = ?", array(1)); $this->verify(); } /** * SC_Query::select() のテストケース. */ function testSelect() { $this->createTestTable(); $result = $this->setTestData(1, '2', 'f'); $this->expected = array(array('id' => '1', 'column1' => '1', 'column2' => '2', 'column3' => 'f')); $this->actual = $this->objQuery->setWhere("id = ?") ->setOrder('id') ->select("*", 'test_table', "", array(1)); $this->verify(); } /** * SC_Query::getOne() のテストケース. */ function testGetOne() { $this->createTestTable(); $this->setTestData(1, '2', 'f'); $this->setTestData(1, '2', 'f'); $this->setTestData(1, '2', 'f'); $this->expected = 3; $this->actual = $this->objQuery->getOne("SELECT COUNT(*) FROM test_table"); $this->verify(); } /** * SC_Query::getRow() のテストケース. */ function testGetRow() { $this->createTestTable(); $this->setTestData(1, '1', 'f'); $this->setTestData(2, '2', 'f'); $this->setTestData(3, '3', 'f'); $this->expected = array('column1' => 1, 'column2' => 1); $this->actual = $this->objQuery->getRow("column1, column2", 'test_table', "id = ?", array(1)); $this->verify(); } /** * SC_Query::getCol() のテストケース. */ function testGetCol() { $this->createTestTable(); $this->setTestData(1, '1', 'f'); $this->setTestData(2, '2', 'f'); $this->setTestData(3, '3', 'f'); $this->expected = array(1, 2); $this->actual = $this->objQuery->getCol('column1', 'test_table', "id < ?", array(3)); $this->verify(); } /** * SC_Query::query() で INSERT を実行するテストケース. */ function testQuery1() { $this->createTestTable(); $sql = "INSERT INTO test_table VALUES (?, ?, ?, ?)"; $data = array('1', '1', '1', 'f'); $this->objQuery->query($sql, $data); $this->expected = array(array('id' => '1', 'column1' => '1', 'column2' => '1', 'column3' => 'f')); $this->actual = $this->objQuery->getAll("SELECT * FROM test_table"); $this->verify(); } function testInsert() { $this->createTestTable(); $this->objQuery->insert('test_table', array('id' => '1', 'column1' => '1', 'column2' => '1', 'column3' => 'f')); $this->expected = array(array('id' => '1', 'column1' => '1', 'column2' => '1', 'column3' => 'f')); $this->actual = $this->objQuery->getAll("SELECT * FROM test_table"); $this->verify(); } /** * SC_Query::query() で UPDATE を実行するテストケース. */ function testQuery2() { $this->createTestTable(); $this->setTestData(1, '2', 'f'); $sql = "UPDATE test_table SET column1 = ?, column2 = ? WHERE id = ?"; $data = array('2', '2', '1'); $this->objQuery->query($sql, $data); $this->expected = array(array('id' => '1', 'column1' => '2', 'column2' => '2', 'column3' => 'f')); $this->actual = $this->objQuery->getAll("SELECT * FROM test_table"); $this->verify(); } function testUpdate() { $this->createTestTable(); $this->setTestData(1, '2', 'f'); $this->objQuery->update('test_table', array('id' => '1', 'column1' => '2', 'column2' => '2', 'column3' => 'f'), "id = ?", array(1)); $this->expected = array(array('id' => '1', 'column1' => '2', 'column2' => '2', 'column3' => 'f')); $this->actual = $this->objQuery->getAll("SELECT * FROM test_table"); $this->verify(); } function testListTables() { $tables = $this->objQuery->listTables(); $this->assertTrue(in_array('mtb_zip', $tables)); } function testListSequences() { $sequences = $this->objQuery->listSequences(); $this->assertTrue(in_array('dtb_products_product_id', $sequences)); } function testListTableFields() { $this->expected = array('id', 'name', 'rank', 'remarks'); $this->actual = $this->objQuery->listTableFields('mtb_constants'); $this->verify(); } function testListTableIndexes() { $indexes = $this->objQuery->listTableIndexes('dtb_mobile_kara_mail'); $this->assertTrue(in_array('dtb_mobile_kara_mail_create_date_key', $indexes)); } function createTestTable() { $sql = "CREATE TABLE test_table (" . "id SERIAL PRIMARY KEY," . "column1 numeric(9)," . "column2 varchar(20)," . "column3 char(1)" . ")"; return $this->objQuery->query($sql); } function dropTestTable() { $tables = $this->objQuery->listTables(); if (in_array('test_table', $tables)) { $this->objQuery->query("DROP TABLE test_table"); } return; } function setTestData($column1, $column2, $column3) { $fields_values = array($column1, $column2, $column3); $sql = "INSERT INTO test_table (column1, column2, column3) VALUES (?, ?, ?)"; $result = $this->objQuery->query($sql, $fields_values); if (PEAR::isError($result)) { var_dump($result); } return $result; } } ?>