what is this PHPUnit error?

Discussion of testing theory and practice, including methodologies (such as TDD, BDD, DDD, Agile, XP) and software - anything to do with testing goes here. (Formerly "The Testing Side of Development")

Moderator: General Moderators

Post Reply
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

what is this PHPUnit error?

Post by raghavan20 »

I am writing a unit test to check a function. Let us say I work with formatted EDI files; I need to keep record of all files that are processed by the system. This means I have to create an entry for each file processed. So this function, will add a new entry if already a file entry does not exist and returns new inserted file id; if file is already registered, then returns the file id.

the test case does not throw any error(not failure) when a file entry already exists and returns file id but throws error when it adds a new file entry. But an actual new file entry is created in the database table even though PHPUnit reports an error.

Error

Code: Select all

[root@mail helpersTest]# php FileIndexHelperTest.php
PHPUnit 3.1.2 by Sebastian Bergmann.

3410E <----this 3410 is the newly inserted row id returned/echoed

Time: 0 seconds

There was 1 error:

1) testAddFileEntry(FileIndexHelperTest)
InvalidArgumentException:
/var/www/html/CDF/backend/testing/helpersTest/FileIndexHelperTest.php:23
/var/www/html/CDF/backend/testing/helpersTest/FileIndexHelperTest.php:37

FAILURES!
Tests: 1, Errors: 1.
unit test code

Code: Select all

<?php

//initialize
require_once( "../include.php" );
setIncludePath( '/var/www/html/CDF/backend/modules/' );


require_once( "../../helpers/FileIndexHelper.php" );
require_once( "PHPUnit/Framework/TestCase.php" );

class FileIndexHelperTest extends PHPUnit_Framework_TestCase{
	
	public function __construct( $name ){
		parent::__construct( $name );
	}
	
	//should add a new file entry and return the file id
	//if file id already present, it returns
	public function testAddFileEntry(){
		
		$result = FileIndexHelper::addFileEntry( 'ingrams', 'acknowledgement', 'reports/acknowledgement/ING/00999999.XPR' );
		echo $result;
		$this->assertRegExp( '#^[0-9]+$#', $result );
		debug_print_backtrace();
		
	}
	
	
}


require_once( "PHPUnit/Framework/TestSuite.php" );
require_once( "PHPUnit/TextUI/TestRunner.php" );

$suite = new PHPUnit_Framework_TestSuite();
$suite->addTestSuite( 'FileIndexHelperTest' );
PHPUnit_TextUI_TestRunner::run( $suite );



?>
library code

Code: Select all

class FileIndexHelper{
	
	public static function addFileEntry( $source, $fileType = "temporary", $fullPath ){
		
		$dbManager = new DBManager();
		$conn = $dbManager->getConnection();
		$processResult = FALSE;
	
		$fileType = OtherHelper::getFileTypeCode( $fileType );
		$sourceCode = SupplierHelper::getSupplierCode( $source ); 
		$fileId = NULL;

		
		$query = "
		
			SELECT 
				`id`
			FROM
				`fileindex`
			WHERE
				`filePath` = '$fullPath'
			AND
				`fileType` = '$fileType'
			AND
				`source` = '$sourceCode'
		
		";
		
		$result = $conn->query ( $query );
		
		if ( $result ){
			if ( $result->num_rows > 0 ){
				$row = $result->fetch_assoc();
				$fileId = $row['id'];
				$processResult = TRUE;
			}
		}
		
		//2. if file id is null, insert an entry
		if ( $fileId == NULL ){
			$query = "
			
				INSERT INTO 
					`fileindex`
				(
					`id`, `filePath`, `fileType`, `source`, `who`
				)
				VALUES
				(
					NULL, '$fullPath', '$fileType', '$sourceCode' , NULL
				)
			
			";
			
			##echo "<br>insert file index entry query: ".$query;
			$conn->query ( $query );
			##echo "<br>insert file index entry query error: ".$conn->error;
			
			
			try{
				if ( $conn->affected_rows == 1 ){
					$processResult = TRUE;
					$fileId = $conn->insert_id;					
				}else{
					throw new ExceptionHandler( 
						"Unable to insert a row in fileentry",
						"Processing file with file path, $fullPath"
					 );
				}
			}catch ( ExceptionHandler $e ){
				echo $e->getExceptionSummary();
				$e->reportErrorByMail();
			}
	
		}

		##echo "file id -" . $fileId;
		if ( get_class ( $conn )  ==  "mysqli" ) $conn->close();	
		return $fileId;
		
	}
}
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

assertRegExp() will throw an exception if the second argument is not a string. Try casting your value into a string. I have seen certain versions of php though that is_string() return false on a string if it is an integer so YMMV.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

Begby wrote:assertRegExp() will throw an exception if the second argument is not a string. Try casting your value into a string. I have seen certain versions of php though that is_string() return false on a string if it is an integer so YMMV.
oh that worked. i did not expect that php would throw error on this. thanks very much. by the way, what is YMMV?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Your Mileage May Vary.
Post Reply