Visa ett inlägg
Oläst 2013-06-15, 13:59 #10
Conny Westh Conny Westh är inte uppkopplad
Klarade millennium-buggen
 
Reg.datum: Aug 2005
Inlägg: 5 166
Conny Westh Conny Westh är inte uppkopplad
Klarade millennium-buggen
 
Reg.datum: Aug 2005
Inlägg: 5 166
Jag använder en autoloader som ligger i underkatalogen 'lib':

Kod:
<?php
// File: autoload.php
// Modified by Conny Westh 2012-09-13
function __autoload($class_name) 
{
    if(file_exists($class_name . '.php')) 
    {
        require_once($class_name . '.php');    
    } 
    else 
    {
        throw new Exception("Kan inte ladda klass: $class_name.");
    }
}
?>
Här är de Testklasser jag använder....

Kod:
<?php
/* 
	PlayersTest.PHP
*/
require_once('/lib/autoload.php');
// spl_autoload_extensions(".inc, .php, .lib, .class.php, .lib.php "); // comma-separated list
// spl_autoload_register();
class PlayersTest
{
	static function RunTestDeleteAllPlayers() 
	{ 
	    try
	    { 
			print("--------------------------------------------------------------\n");
			print("Begin: DELETE All Players:\n");
			Players::deleteAllPlayers();
			print("End: DELETE All Players:\n");
			
			print("--------------------------------------------------------------\n");
			print("Begin: Fetch Players::getCountOfAllPlayers():\n");
			$result1 = Players::getCountOfAllPlayers();
			foreach($result1 as $row) 
			{
			    print_r($row);
			}
			print("End: Fetch Players::getCountOfAllPlayers():\n");
			print("--------------------------------------------------------------\n");
		}
		catch (PDOException $e) 
		{
	    	throw new Exception("Error: " . $e->getMessage() . "",0,$e); 
		    die();
			return null; 
		}
		catch (Exception $e)
		{
	    	throw new Exception('Error: Can not connect to database.',0,$e); 
		}
		return null; 
	}

	static function RunTestInsertAllPlayers() 
	{ 
	    try
	    { 
			print("--------------------------------------------------------------\n");
			print("Begin: INSERT INFO Players:\n");
			Players::insertPlayer("Robert","McIntosh");
			Players::insertPlayer("Tim","Brody");
			Players::insertPlayer("Rebecca","Jefferson");
			Players::insertPlayer("Sandy","Jonson");
			Players::insertPlayer("John","Archimedes");
			Players::insertPlayer("Elisabet","Futterkiste");
			Players::insertPlayer("Anna","Oscarson");
			Players::insertPlayer("Olivia","Runesson");
			print("End: INSERT INFO Players::\n");
		}
		catch (PDOException $e) 
		{
	    	throw new Exception("Error: " . $e->getMessage() . "",0,$e); 
		    die();
			return null; 
		}
		catch (Exception $e)
		{
	    	throw new Exception('Error: Can not connect to database.',0,$e); 
		}
		return null; 
	}

	static function RunTestGetAllPlayersWithQuery()
	{ 
	    try
	    { 
			print("--------------------------------------------------------------\n");
			print("Begin: Fetch all TestPlayers:\n");
			$result0 = Players::getAllPlayersWithQuery();
			foreach($result0 as $row) 
			{
			    print_r($row);
			}
			print("End: Fetch all TestPlayers:\n");
		}
		catch (PDOException $e) 
		{
	    	throw new Exception("Error: " . $e->getMessage() . "",0,$e); 
		    die();
			return null; 
		}
		catch (Exception $e)
		{
	    	throw new Exception('Error: Can not connect to database.',0,$e); 
		}
		return null; 
	}

	static function RunTestGetAllPlayers()
	{ 
	    try
	    { 
			print("--------------------------------------------------------------\n");
			/* Fetch all of the remaining rows in the result set */
			print("Begin: Fetch Players::getAllPlayers():\n");
			$result1 = Players::getAllPlayers();
			foreach($result1 as $row) 
			{
			    print_r($row);
			}
			print("End: Fetch Players::getAllPlayers():\n");
		}
		catch (PDOException $e) 
		{
	    	throw new Exception("Error: " . $e->getMessage() . "",0,$e); 
		    die();
			return null; 
		}
		catch (Exception $e)
		{
	    	throw new Exception('Error: Can not connect to database.',0,$e); 
		}
		return null; 
	}

	static function RunTestGetPlayerById()
	{ 
	    try
	    { 
			print("--------------------------------------------------------------\n");		
			$id = 1;
			print("Begin: Fetch Players::getPlayerById($id):\n");
			$result2 = Players::getPlayerById($id);
			foreach($result2 as $row) 
			{
			    print_r($row);
			}
			print("End: Fetch Players::getPlayerById($id):\n");
		}
		catch (PDOException $e) 
		{
	    	throw new Exception("Error: " . $e->getMessage() . "",0,$e); 
		    die();
			return null; 
		}
		catch (Exception $e)
		{
	    	throw new Exception('Error: Can not connect to database.',0,$e); 
		}
		return null; 
	}

	static function RunTestGetCountOfAllPlayers()
	{ 
	    try
	    { 
			print("--------------------------------------------------------------\n");
			print("Begin: Fetch Players::getCountOfAllPlayers():\n");
			$result1 = Players::getCountOfAllPlayers();
			foreach($result1 as $row) 
			{
			    print_r($row);
			}
			print("End: Fetch Players::getCountOfAllPlayers():\n");
		}
		catch (PDOException $e) 
		{
	    	throw new Exception("Error: " . $e->getMessage() . "",0,$e); 
		    die();
			return null; 
		}
		catch (Exception $e)
		{
	    	throw new Exception('Error: Can not connect to database.',0,$e); 
		}
		return null; 
	}

	static function RunTest() 
	{ 
	    try
	    { 
			self::RunTestDeleteAllPlayers();
			self::RunTestInsertAllPlayers();
			self::RunTestGetAllPlayersWithQuery();
			self::RunTestGetAllPlayers();
			self::RunTestGetPlayerById();
			self::RunTestGetCountOfAllPlayers();
			print("--------------------------------------------------------------\n");
			print("                    All test are Done.                        \n");
			print("--------------------------------------------------------------\n");
		}
		catch (PDOException $e) 
		{
	    	throw new Exception("Error: " . $e->getMessage() . "",0,$e); 
		    die();
			return null; 
		}
		catch (Exception $e)
		{
	    	throw new Exception('Error: Can not connect to database.',0,$e); 
		}
		return null; 
	}
} 

PlayersTest::RunTest();

?>
Conny Westh är inte uppkopplad   Svara med citatSvara med citat