PHP Classes

File: tests/DBTest.php

Recommend this page to a friend!
  Classes of Cyril Ogana   PHP DBAL Wrapper   tests/DBTest.php   Download  
File: tests/DBTest.php
Role: Unit test script
Content type: text/plain
Description: Unit test script
Class: PHP DBAL Wrapper
Establish database connections with Doctrine DBAL
Author: By
Last change: Add type hints to interited methods of test cases
Date: 4 years ago
Size: 4,279 bytes
 

Contents

Class file image Download
<?php
namespace cymapgt\core\utility\db\tests;

use
cymapgt\core\utility\db\DB;

/**
 * Generated by PHPUnit_SkeletonGenerator 1.2.1 on 2014-04-29 at 15:20:52.
 */
class DBTest extends \PHPUnit\Framework\TestCase {

   
/**
     * @var DB
     */
   
protected $object;

   
/**
     * Sets up the fixture, for example, opens a network connection.
     * This method is called before a test is executed.
     */
   
protected function setUp(): void {
      
$dbParams = array (
           
'driver' => 'pdo_sqlite',
           
'path' => __DIR__ . '/files/db.test.sqlite'
      
);
       
      
$this->object = DB::connectDbNew($dbParams);
       
$sql = "DELETE FROM db_test WHERE 1";
       
$this->object->query($sql);

      
$sql = "INSERT INTO db_test (first_name, last_name) VALUES('Allesandro', 'Del Piero')";
      
$this->object->query($sql);

      
$sql = "INSERT INTO db_test (first_name, last_name) VALUES('Allesandro', 'Nesta')";
      
$this->object->query($sql);

      
$sql = "INSERT INTO db_test (first_name, last_name) VALUES('Andrea', 'Pirlo')";
      
$this->object->query($sql);

      
$sql = "INSERT INTO db_test (first_name, last_name) VALUES('Paolo', 'Maldini')";
      
$this->object->query($sql);

      
$sql = "INSERT INTO db_test (first_name, last_name) VALUES('Roberto', 'Baggio')";
      
$this->object->query($sql);

      
$sql = "INSERT INTO db_test (first_name, last_name) VALUES('Stephan', 'El Shaarawy')";
      
$this->object->query($sql);
    }

   
/**
     * Tears down the fixture, for example, closes a network connection.
     * This method is called after a test is executed.
     */
   
protected function tearDown(): void {

    }

   
/**
     * @covers cymapgt\core\utility\db\DB::connectDb
     * @todo Implement testConnect().
     */
   
public function testConnectDb() {
       
$sql = "SELECT id FROM db_test LIMIT 1";
       
$stmt = $this->object->query($sql);
       
$row = $stmt->fetch();

        if(((int) (
$row['id'])) > 0) {
           
$idIsInt = true;
        } else {
           
$idIsInt = false;
        }
           
       
$this->assertEquals(true, $idIsInt);
    }

   
/**
     * @covers cymapgt\core\utility\db\DB::connectDbNew
     * @todo Implement testConnectNew().
     */
   
public function testConnectDbNew() {
       
$sql = "SELECT id FROM db_test LIMIT 1";
       
$stmt = $this->object->query($sql);
       
$row = $stmt->fetch();

        if(((int) (
$row['id'])) > 0) {
           
$idIsInt = true;
        } else {
           
$idIsInt = false;
        }
           
       
$this->assertEquals(true, $idIsInt);
    }

   
/**
     * @covers cymapgt\core\utility\db\DB::setDbType
     */
   
public function testSetDbType() {
       
DB::setDbType('sqlite');
       
$this->assertEquals('sqlite', DB::getDbType());
    }
   
   
/**
     * @covers cymapgt\core\utility\db\DB::setDbType
     * @expectedException \cymapgt\Exception\DBException
     */
   
public function testSetDbTypeException() {
       
DB::setDbType('unsupporteddb');
    }
   
   
/**
     * @covers cymapgt\core\utility\db\DB::validateDbParameters
     * @expectedException \cymapgt\Exception\DBException
     * @expectedExceptionMessage One or more default parameters for DB connection not set
     */
   
public function testValidateDbParameters() {
       
$dbParams = array();
       
DB::validateDbParameters($dbParams);
    }
       
   
/**
       *@covers cymapgt\core\utility\db\DB::sanitizeDbParameters
       */
   
public function testSanitizeDbParameters() {
      
$dbParams = array (
           
'driver' => 'pdo_sqlite',
           
'path' => __DIR__ . 'files/db.test.sqlite',
           
'user' => ''
      
);
       
       
DB::setDbType('sqlite');
       
$dbParamsSanitized = DB::sanitizeDbParameters($dbParams);
       
$this->assertEquals(false, isset($dbParamsSanitized['user']));
    }
   
   
/**
     * @covers cymapgt\core\utility\db\DB::closeDbConnection
     * @todo Implement testClose().
     */
   
public function testCloseDbConnection() {
       
$this->assertEquals(null, DB::closeDbConnection());
    }
}