Beispiel #1
0
 public function testResultSet()
 {
     $connection = \Dao\DbFactory::initDatabase($this->_config);
     //$this->prepareUnitTestTable();
     $names = array('jay' => 'zeng', 'john' => 'smith', 'david' => 'steve', 'awesome' => 'oops');
     // Hold down all the newly insert idsksdjfhaskdjfajksdfas
     $ids = array();
     // Insert a batch of rows
     foreach ($names as $firstName => $lastName) {
         $ids[] = $connection->insert(sprintf('INSERT INTO dbUnitTest.Employee VALUES (null, %s, %s);', $connection->quote($firstName), $connection->quote($lastName)));
     }
     $results = array();
     $dbResult = $connection->select('SELECT * FROM dbUnitTest.Employee');
     // $key is not being used, but we need to specify it in order to hit the
     // \Iterator::key()
     foreach ($dbResult as $key => $result) {
         $results[] = $result;
     }
     // Do a very explicit equality check
     $this->assertEquals(array(array('employee_id' => 1, 'first_name' => 'jay', 'last_name' => 'zeng'), array('employee_id' => 2, 'first_name' => 'john', 'last_name' => 'smith'), array('employee_id' => 3, 'first_name' => 'david', 'last_name' => 'steve'), array('employee_id' => 4, 'first_name' => 'awesome', 'last_name' => 'oops')), $results);
     // Test the explicit getNumberOfRows()
     $this->assertEquals(4, $dbResult->getNumberOfRows());
     // Result implemented countable
     $this->assertEquals(4, count($dbResult));
     //Delete a row
     $connection->delete('DELETE FROM dbUnitTest.Employee WHERE employee_id = 4');
     // Now we should only have 3 rows
     $rows = $connection->select('SELECT * FROM dbUnitTest.Employee');
     $this->assertCount(3, $connection->select('SELECT * FROM dbUnitTest.Employee'));
     // Fetch the 1st row of the result set
     $this->assertEquals(array('employee_id' => 1, 'first_name' => 'jay', 'last_name' => 'zeng'), $connection->selectRow('SELECT * FROM dbUnitTest.Employee'));
     // Now, move on to hit various exceptions
     try {
         $connection->select('SELECT NOSUCHFIELD FROM dbUnitTest.Employee;');
     } catch (\Dao\Exception\ReadException $e) {
         $errorMessage = $e->getDetails();
         $this->assertContains('Error message: Unknown column', $errorMessage);
         $this->assertContains('Error Code', $errorMessage);
         $this->assertContains('Sql Query', $errorMessage);
     }
     try {
         $connection->delete('DELETE FROM dbUnitTest.Employee WHERE lala = 100');
     } catch (\Dao\Exception\DeleteException $e) {
         $errorMessage = $e->getDetails();
         $this->assertContains('Error message: Unknown column', $errorMessage);
         $this->assertContains('Error Code', $errorMessage);
         $this->assertContains('Sql Query', $errorMessage);
     }
     try {
         $connection->insert('INSERT INTO dbUnitTest.Employee VALUES (null, null, null);');
     } catch (\Dao\Exception\WriteException $e) {
         $this->assertContains('Error message: Column \'first_name\' cannot be null', $e->getDetails());
     }
     $this->assertEquals(array('errorMessage' => 'Column \'first_name\' cannot be null', 'errorNumber' => 1048), $connection->getError());
     // update last row
     $connection->update(sprintf('UPDATE dbUnitTest.Employee SET first_name = "%s" WHERE employee_id = 1;', 'superMan'));
     $row = $connection->selectRow('SELECT first_name FROM dbUnitTest.Employee');
     $this->assertSame('superMan', $row['first_name']);
     try {
         $connection->update('UPDATE dbUnitTest.Employee SET oops = "haha";');
     } catch (\Dao\Exception\WriteException $e) {
         $this->assertContains('Unknown column \'oops\' in \'field list\'', $e->getDetails());
     }
 }
Beispiel #2
0
 public function setUp()
 {
     $config = array('host' => DB_HOST, 'username' => DB_USERNAME, 'password' => DB_PASSWORD, 'dbname' => DB_DBNAME, 'port' => DB_PORT, 'socket' => DB_SOCKET);
     $connection = \Dao\DbFactory::initDatabase($config);
     $this->_schema = new \Dao\MetaData\InformationSchema($connection);
 }