what is this PHPUnit error?
Posted: Fri Jul 20, 2007 8:06 am
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
unit test code
library code
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.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 );
?>
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;
}
}