DatabaseExceptionWrapperTest.php in Zircon Profile 8        
                          
                  
                        
  
  
  
File
  core/modules/system/src/Tests/Database/DatabaseExceptionWrapperTest.php
  
    View source  
  <?php
namespace Drupal\system\Tests\Database;
use Drupal\Core\Database\DatabaseExceptionWrapper;
use Drupal\Core\Database\Database;
use Drupal\simpletest\KernelTestBase;
class DatabaseExceptionWrapperTest extends KernelTestBase {
  
  public function testPreparedStatement() {
    $connection = Database::getConnection();
    try {
      
      $query = $connection
        ->prepare('bananas');
      
      $connection
        ->query($query);
      $this
        ->fail('Expected PDOException or DatabaseExceptionWrapper, none was thrown.');
    } catch (\PDOException $e) {
      $this
        ->pass('Expected PDOException was thrown.');
    } catch (DatabaseExceptionWrapper $e) {
      $this
        ->pass('Expected DatabaseExceptionWrapper was thrown.');
    } catch (\Exception $e) {
      $this
        ->fail("Thrown exception is not a PDOException:\n" . (string) $e);
    }
  }
  
  public function testQueryThrowsDatabaseExceptionWrapperException() {
    $connection = Database::getConnection();
    try {
      $connection
        ->query('SELECT * FROM {does_not_exist}');
      $this
        ->fail('Expected PDOException, none was thrown.');
    } catch (DatabaseExceptionWrapper $e) {
      $this
        ->pass('Expected DatabaseExceptionWrapper was thrown.');
    } catch (\Exception $e) {
      $this
        ->fail("Thrown exception is not a DatabaseExceptionWrapper:\n" . (string) $e);
    }
  }
}