GitBucket
4.21.2
Toggle navigation
Snippets
Sign in
Files
Branches
1
Releases
Issues
Pull requests
Labels
Priorities
Milestones
Wiki
Forks
nigel.stanger
/
sqlmarker
Browse code
Added HTML reporter class.
master
1 parent
d472142
commit
96c64ac78e275a635719980f7a349d9a727d0a9d
Nigel Stanger
authored
on 28 Jun 2013
Patch
Showing
4 changed files
Unit_testing/Reporter.php
Unit_testing/Reporter/HTMLReporter.php
Unit_testing/Reporter/TextReporter.php
Unit_testing/test.php
Ignore Space
Show notes
View
Unit_testing/Reporter.php
0 → 100644
<?php abstract class Reporter { // 0 == no output // 1 == brief output (students?) // 2 == verbose output (marking) private $verbosity = 0; const STATUS_PASS = 'PASSED'; const STATUS_SKIPPED = 'SKIPPED'; const STATUS_INCOMPLETE = 'INCOMPLETE'; const STATUS_FAILURE = 'FAILED'; const STATUS_ERROR = 'ERROR'; const STATUS_WARNING = 'WARNING'; const STATUS_NOTE = 'NOTE'; function __construct( $verbosity ) { $this->verbosity = $verbosity; } public static function pluralise( $count, $oneText, $manyText ) { return ( $count > 1 ) ? $manyText : $oneText; } public function setVerbosity( $newVerbosity ) { $this->verbosity = $newVerbosity; } public function getVerbosity() { return $this->verbosity; } /** * $status is one of: PASSED, FAILED, ERROR, INCOMPLETE, SKIPPED, WARNING, NOTE, ...? * $text is a printf-style string (although we actually use vprintf because of the array) * $arguments is an array of arguments to $text */ public function report( $statusText, $reportText, $printfArguments ) { if ( $this->verbosity ) vprintf( $statusText . $reportText, $printfArguments ); } } ?>
Ignore Space
Show notes
View
Unit_testing/Reporter/HTMLReporter.php
0 → 100644
<?php require_once( 'Reporter.php' ); class HTMLReporter extends Reporter { public function report ( $status, $reportText, $printfArguments ) { if ( $this->getVerbosity() ) { $statusText = '<p><span'; switch ( $status ) { case Reporter::STATUS_PASS: $statusText .= ' style="color: green;">✔✔✔ '; break; case Reporter::STATUS_SKIPPED: $statusText .= '>### '; break; case Reporter::STATUS_INCOMPLETE: $statusText .= ' style="background-color: yellow;">%%% '; break; case Reporter::STATUS_FAILURE: $statusText .= ' style="color: red;">✘✘✘ '; break; case Reporter::STATUS_ERROR: $statusText .= ' style="color: red;">☠☠☠ '; break; case Reporter::STATUS_WARNING: $statusText .= ' style="color: orange;">!!! '; break; case Reporter::STATUS_NOTE: $statusText .= ' style="background-color: yellow;">!!! '; break; default: $statusText .= ' style="background-color: yellow;">??? '; break; } if ( $this->getVerbosity() > 1 ) $statusText .= $status . ': '; parent::report( $statusText, $reportText . "</span></p>\n", $printfArguments ); } } } ?>
Ignore Space
Show notes
View
Unit_testing/Reporter/TextReporter.php
<?php require_once( 'Reporter.php' ); class TextReporter extends Reporter { public function report ( $status, $reportText, $printfArguments ) { if ( $this->getVerbosity() ) { $statusText = ''; switch ( $status ) { case Reporter::STATUS_PASS: $statusText .= '+++ '; break; case Reporter::STATUS_SKIPPED: $statusText .= '### '; break; case Reporter::STATUS_INCOMPLETE: $statusText .= '%%% '; break; case Reporter::STATUS_FAILURE: $statusText .= '--- '; break; case Reporter::STATUS_ERROR: $statusText .= 'EEE '; break; case Reporter::STATUS_WARNING: $statusText .= '!!! '; break; case Reporter::STATUS_NOTE: $statusText .= '!!! '; break; default: $statusText .= '??? '; break; } if ( $this->getVerbosity() > 1 ) $statusText .= $status . ': '; parent::report( $statusText, $reportText . "\n", $printfArguments ); } } } ?>
<?php require_once( 'Reporter.php' ); class TextReporter extends Reporter { public function report ( $status, $reportText, $printfArguments ) { if ( $this->getVerbosity() ) { $statusText = ''; switch ( $status ) { case Reporter::STATUS_PASS: $statusText = '+++ '; break; case Reporter::STATUS_SKIPPED: $statusText = '### '; break; case Reporter::STATUS_INCOMPLETE: $statusText = '%%% '; break; case Reporter::STATUS_FAILURE: $statusText = '--- '; break; case Reporter::STATUS_ERROR: $statusText = 'EEE '; break; case Reporter::STATUS_WARNING: $statusText = '!!! '; break; case Reporter::STATUS_NOTE: $statusText = '!!! '; break; default: $statusText = '??? '; break; } if ( $this->getVerbosity() > 1 ) $statusText = $statusText . $status . ': '; parent::report( $statusText, $reportText . "\n", $printfArguments ); } } } ?>
Ignore Space
Show notes
View
Unit_testing/test.php
<?php require_once "PHPUnit/Autoload.php"; require_once 'SimpleTestListener.php'; require_once 'Reporter/TextReporter.php'; require_once 'Reporter/HTMLReporter.php'; require_once "Staff/BDL_Test_Staff_structure.php"; require_once "Staff/BDL_Test_Staff_data.php"; $result = new PHPUnit_Framework_TestResult; $listener = new SimpleTestListener; $result->addListener( $listener ); $mode = 'TEXT'; $verbosity = 2; switch ( $mode ) { case 'HTML': $reporter = new HTMLReporter( $verbosity ); break; case 'TEXT': $reporter = new TextReporter( $verbosity ); break; } $suite = new PHPUnit_Framework_TestSuite( 'BDL_Test_Staff_structure' ); // Critical to data testing. // TODO: is $testResult needed anymore? $testResult = $suite->run( $result, '/testTableExists/' ); $structurePassed = $listener->wasSuccessful( 'testTableExists' ); if ( $structurePassed ) { $reporter->report( Reporter::STATUS_PASS, 'Table exists.', null ); // Critical to data testing. $testResult = $suite->run( $result, '/testColumnExists/' ); $structurePassed = $listener->wasSuccessful( 'BDL_Test_Staff_structure::testColumnExists' ); if ( $structurePassed ) { $reporter->report( Reporter::STATUS_PASS, 'Table contains all the expected columns.', null ); $testResult = $suite->run( $result, '/testColumnDataType/' ); $structurePassed = $listener->wasSuccessful( 'BDL_Test_Staff_structure::testColumnDataType' ); if ( $structurePassed ) { $reporter->report( Reporter::STATUS_PASS, 'All columns have the expected data types.', null ); $testResult = $suite->run( $result, '/testColumnLength/' ); $structurePassed = $listener->wasSuccessful( 'BDL_Test_Staff_structure::testColumnLength' ); if ( $structurePassed ) { $reporter->report( Reporter::STATUS_PASS, 'All columns have the expected lengths.', null ); } else { $reporter->report( Reporter::STATUS_FAILURE, '%d of the %d columns %s an unexpected column length.', array( $listener->countNonPasses( 'BDL_Test_Staff_structure::testColumnLength' ), $listener->countPasses( 'BDL_Test_Staff_structure::testColumnLength' ), Reporter::pluralise( 'BDL_Test_Staff_structure::testColumnLength' ) ) ) ; } } else { $reporter->report( Reporter::STATUS_FAILURE, '%d of the %d columns %s unexpected data types.', array( $listener->countNonPasses( 'BDL_Test_Staff_structure::testColumnDataType' ), $listener->countPasses( 'BDL_Test_Staff_structure::testColumnDataType' ), Reporter::pluralise( $listener->countNonPasses( 'BDL_Test_Staff_structure::testColumnDataType' ), "has", "have" ) ) ) ; $reporter->report( Reporter::STATUS_SKIPPED, 'column length tests, as the data types do not match what was expected.', null ); } $testResult = $suite->run( $result, '/testColumnNullability/' ); if ( $listener->wasSuccessful( 'BDL_Test_Staff_structure::testColumnNullability' ) ) { $reporter->report( Reporter::STATUS_PASS, 'All columns have the expected nullability.', null ); } else { $reporter->report( Reporter::STATUS_FAILURE, '%d of the %d columns %s unexpected nullability.', array( $listener->countNonPasses( 'BDL_Test_Staff_structure::testColumnNullability' ), $listener->countPasses( 'BDL_Test_Staff_structure::testColumnNullability' ), Reporter::pluralise( $listener->countNonPasses( 'BDL_Test_Staff_structure::testColumnNullability' ), "has", "have" ) ) ) ; } } else { $reporter->report( Reporter::STATUS_FAILURE, '%d of the %d expected columns %s either missing or misnamed.', array( $listener->countNonPasses( 'BDL_Test_Staff_structure::testColumnExists' ), $listener->countPasses( 'BDL_Test_Staff_structure::testColumnExists' ), Reporter::pluralise( $listener->countNonPasses( 'BDL_Test_Staff_structure::testColumnExists' ), 'is', 'are' ) ) ) ; $reporter->report( Reporter::STATUS_SKIPPED, 'data type, length and nullability tests as they will include spurious errors.', null ); } // Not critical to data testing. Need to run both tests in one pass as the columns test depends on the existence test. $testResult = $suite->run( $result, '/testPK.*/' ); if ( $listener->wasSuccessful( 'testPKExists' ) ) { $reporter->report( Reporter::STATUS_PASS, 'Primary key exists.', null ); } else { $reporter->report( Reporter::STATUS_FAILURE, 'Primary key missing.', null ); } if ( $listener->wasSuccessful( 'testPKColumns' ) ) { $reporter->report( Reporter::STATUS_PASS, 'Primary key includes (only) the expected columns.', null ); } else { $reporter->report( Reporter::STATUS_FAILURE, 'Primary key does not include (only) the expected columns.', null ); } // Not critical to data testing. $testResult = $suite->run( $result, '/testConstraintsNamed/' ); if ( $listener->wasSuccessful( 'BDL_Test_Staff_structure::testConstraintsNamed' ) ) { $reporter->report( Reporter::STATUS_PASS, 'All constraints that should be are explicitly named.', null ); } else { $reporter->report( Reporter::STATUS_FAILURE, 'Some constraints are not explicitly named that should be.', null ); } } /* If the table or required columns are missing or misnamed, we need to skip the data testing entirely, as the INSERTs will just error out. We can't incorporate this into the if above, because we're using a completely different test suite. */ if ( $structurePassed ) { $suite = new PHPUnit_Framework_TestSuite( 'BDL_Test_Staff_data' ); $testResult = $suite->run( $result, '/testColumnLegalValue/' ); if ( $listener->wasSuccessful( 'BDL_Test_Staff_data::testColumnLegalValue' ) ) { $reporter->report( Reporter::STATUS_PASS, 'All %d legal values tested were accepted.', array( $listener->countTests( 'BDL_Test_Staff_data::testColumnLegalValue' ) ) ); } $testResult = $suite->run( $result, '/testColumnIllegalValueExplicit/' ); if ( $listener->wasSuccessful( 'BDL_Test_Staff_data::testColumnIllegalValueExplicit' ) ) { $reporter->report( Reporter::STATUS_PASS, 'All %d illegal values tested were rejected by a CHECK constraint.', array( $listener->countTests( 'BDL_Test_Staff_data::testColumnIllegalValueExplicit' ) ) ); } else { $checkFails = $listener->countFails( 'BDL_Test_Staff_data::testColumnIllegalValueExplicit' ); $reporter->report( Reporter::STATUS_ALERT, '%d of %d illegal values tested %s not rejected by a CHECK constraint.', array( $checkFails, $listener->countTests( 'BDL_Test_Staff_data::testColumnIllegalValueExplicit' ), Reporter::pluralise( $checkFails, 'was', 'were' ) ) ) ; $reporter->report( Reporter::STATUS_ALERT, 'Checking values against column length...', null ); /* Unfortunately, we can't test just the columns that failed the CHECK test. The failed TestCases are in $testResult->failures(), but we need the column name, which is hidden away in the private $data member of TestCase. We therefore have to test all the illegal values again to see if they're larger than the column. We then make the big assumption that if all the values that failed the CHECK test did so because they exceeded the column length. If this is correct, then the number of CHECK fails will equal the number of column length passes. If not, then something more serious has probably gone wrong! */ $testResult = $suite->run( $result, '/testColumnIllegalValueImplicit/' ); $implicitPasses = $listener->countPasses( 'BDL_Test_Staff_data::testColumnIllegalValueImplicit' ); $reporter->report( Reporter::STATUS_PASSED, '%d of %d illegal values tested %s rejected by exceeding the column length.', array( $implicitPasses, $listener->countTests( 'BDL_Test_Staff_data::testColumnIllegalValueImplicit' ), Reporter::pluralise( $implicitPasses, 'was', 'were' ) ) ) ; // Any leftovers? if ( $implicitPasses != $checkFails ) { /* $checkFails must by definition be >= $implicitPasses, as a "length exceeded" will /always/ fail the CHECK test, and a "check constraint" will /always/ fail the column length test. The two values will only differ when there are other exceptions in the mix, which will fail both tests. For example, suppose that two values fail the CHECK test with "length exceeded", one fails with "foo exception" and the remaining two pass. The first two will pass the column length test, and the remaining three will fail. */ $reporter->report( Reporter::STATUS_FAILURE, '%d illegal values %s rejected in both tests—check for something unusual.', array( $checkFails - $implicitPasses, Reporter::pluralise( $checkFails - $implicitPasses, 'was', 'were' ) ) ) ; } else { $reporter->report( Reporter::STATUS_NOTE, 'This is OK, but not necessarily safe, as the column length may change in future.', null ); } } } else { $reporter->report( Reporter::STATUS_SKIPPED, 'data tests, as failures in the structure testing mean that they may not work.', null ); } ?>
<?php require_once "PHPUnit/Autoload.php"; require_once 'SimpleTestListener.php'; require_once 'Reporter/TextReporter.php'; require_once "Staff/BDL_Test_Staff_structure.php"; require_once "Staff/BDL_Test_Staff_data.php"; $result = new PHPUnit_Framework_TestResult; $listener = new SimpleTestListener; $result->addListener( $listener ); $mode = 'TEXT'; $verbosity = 2; switch ( $mode ) { case 'TEXT': $reporter = new TextReporter( $verbosity ); break; } $suite = new PHPUnit_Framework_TestSuite( 'BDL_Test_Staff_structure' ); // Critical to data testing. // TODO: is $testResult needed anymore? $testResult = $suite->run( $result, '/testTableExists/' ); $structurePassed = $listener->wasSuccessful( 'testTableExists' ); if ( $structurePassed ) { $reporter->report( Reporter::STATUS_PASS, 'Table exists.', null ); // Critical to data testing. $testResult = $suite->run( $result, '/testColumnExists/' ); $structurePassed = $listener->wasSuccessful( 'BDL_Test_Staff_structure::testColumnExists' ); if ( $structurePassed ) { $reporter->report( Reporter::STATUS_PASS, 'Table contains all the expected columns.', null ); $testResult = $suite->run( $result, '/testColumnDataType/' ); $structurePassed = $listener->wasSuccessful( 'BDL_Test_Staff_structure::testColumnDataType' ); if ( $structurePassed ) { $reporter->report( Reporter::STATUS_PASS, 'All columns have the expected data types.', null ); $testResult = $suite->run( $result, '/testColumnLength/' ); $structurePassed = $listener->wasSuccessful( 'BDL_Test_Staff_structure::testColumnLength' ); if ( $structurePassed ) { $reporter->report( Reporter::STATUS_PASS, 'All columns have the expected lengths.', null ); } else { $reporter->report( Reporter::STATUS_FAILURE, '%d of the %d columns %s an unexpected column length.', array( $listener->countNonPasses( 'BDL_Test_Staff_structure::testColumnLength' ), $listener->countPasses( 'BDL_Test_Staff_structure::testColumnLength' ), Reporter::pluralise( 'BDL_Test_Staff_structure::testColumnLength' ) ) ) ; } } else { $reporter->report( Reporter::STATUS_FAILURE, '%d of the %d columns %s unexpected data types.', array( $listener->countNonPasses( 'BDL_Test_Staff_structure::testColumnDataType' ), $listener->countPasses( 'BDL_Test_Staff_structure::testColumnDataType' ), Reporter::pluralise( $listener->countNonPasses( 'BDL_Test_Staff_structure::testColumnDataType' ), "has", "have" ) ) ) ; $reporter->report( Reporter::STATUS_SKIPPED, 'column length tests, as the data types do not match what was expected.', null ); } $testResult = $suite->run( $result, '/testColumnNullability/' ); if ( $listener->wasSuccessful( 'BDL_Test_Staff_structure::testColumnNullability' ) ) { $reporter->report( Reporter::STATUS_PASS, 'All columns have the expected nullability.', null ); } else { $reporter->report( Reporter::STATUS_FAILURE, '%d of the %d columns %s unexpected nullability.', array( $listener->countNonPasses( 'BDL_Test_Staff_structure::testColumnNullability' ), $listener->countPasses( 'BDL_Test_Staff_structure::testColumnNullability' ), Reporter::pluralise( $listener->countNonPasses( 'BDL_Test_Staff_structure::testColumnNullability' ), "has", "have" ) ) ) ; } } else { $reporter->report( Reporter::STATUS_FAILURE, '%d of the %d expected columns %s either missing or misnamed.', array( $listener->countNonPasses( 'BDL_Test_Staff_structure::testColumnExists' ), $listener->countPasses( 'BDL_Test_Staff_structure::testColumnExists' ), Reporter::pluralise( $listener->countNonPasses( 'BDL_Test_Staff_structure::testColumnExists' ), 'is', 'are' ) ) ) ; $reporter->report( Reporter::STATUS_SKIPPED, 'data type, length and nullability tests as they will include spurious errors.', null ); } // Not critical to data testing. Need to run both tests in one pass as the columns test depends on the existence test. $testResult = $suite->run( $result, '/testPK.*/' ); if ( $listener->wasSuccessful( 'testPKExists' ) ) { $reporter->report( Reporter::STATUS_PASS, 'Primary key exists.', null ); } else { $reporter->report( Reporter::STATUS_FAILURE, 'Primary key missing.', null ); } if ( $listener->wasSuccessful( 'testPKColumns' ) ) { $reporter->report( Reporter::STATUS_PASS, 'Primary key includes (only) the expected columns.', null ); } else { $reporter->report( Reporter::STATUS_FAILURE, 'Primary key does not include (only) the expected columns.', null ); } // Not critical to data testing. $testResult = $suite->run( $result, '/testConstraintsNamed/' ); if ( $listener->wasSuccessful( 'BDL_Test_Staff_structure::testConstraintsNamed' ) ) { $reporter->report( Reporter::STATUS_PASS, 'All constraints that should be are explicitly named.', null ); } else { $reporter->report( Reporter::STATUS_FAILURE, 'Some constraints are not explicitly named that should be.', null ); } } /* If the table or required columns are missing or misnamed, we need to skip the data testing entirely, as the INSERTs will just error out. We can't incorporate this into the if above, because we're using a completely different test suite. */ if ( $structurePassed ) { $suite = new PHPUnit_Framework_TestSuite( 'BDL_Test_Staff_data' ); $testResult = $suite->run( $result, '/testColumnLegalValue/' ); if ( $listener->wasSuccessful( 'BDL_Test_Staff_data::testColumnLegalValue' ) ) { $reporter->report( Reporter::STATUS_PASS, 'All %d legal values tested were accepted.', array( $listener->countTests( 'BDL_Test_Staff_data::testColumnLegalValue' ) ) ); } $testResult = $suite->run( $result, '/testColumnIllegalValueExplicit/' ); if ( $listener->wasSuccessful( 'BDL_Test_Staff_data::testColumnIllegalValueExplicit' ) ) { $reporter->report( Reporter::STATUS_PASS, 'All %d illegal values tested were rejected by a CHECK constraint.', array( $listener->countTests( 'BDL_Test_Staff_data::testColumnIllegalValueExplicit' ) ) ); } else { $checkFails = $listener->countFails( 'BDL_Test_Staff_data::testColumnIllegalValueExplicit' ); $reporter->report( Reporter::STATUS_ALERT, '%d of %d illegal values tested %s not rejected by a CHECK constraint.', array( $checkFails, $listener->countTests( 'BDL_Test_Staff_data::testColumnIllegalValueExplicit' ), Reporter::pluralise( $checkFails, 'was', 'were' ) ) ) ; $reporter->report( Reporter::STATUS_ALERT, 'Checking values against column length...', null ); /* Unfortunately, we can't test just the columns that failed the CHECK test. The failed TestCases are in $testResult->failures(), but we need the column name, which is hidden away in the private $data member of TestCase. We therefore have to test all the illegal values again to see if they're larger than the column. We then make the big assumption that if all the values that failed the CHECK test did so because they exceeded the column length. If this is correct, then the number of CHECK fails will equal the number of column length passes. If not, then something more serious has probably gone wrong! */ $testResult = $suite->run( $result, '/testColumnIllegalValueImplicit/' ); $implicitPasses = $listener->countPasses( 'BDL_Test_Staff_data::testColumnIllegalValueImplicit' ); $reporter->report( Reporter::STATUS_PASSED, '%d of %d illegal values tested %s rejected by exceeding the column length.', array( $implicitPasses, $listener->countTests( 'BDL_Test_Staff_data::testColumnIllegalValueImplicit' ), Reporter::pluralise( $implicitPasses, 'was', 'were' ) ) ) ; // Any leftovers? if ( $implicitPasses != $checkFails ) { /* $checkFails must by definition be >= $implicitPasses, as a "length exceeded" will /always/ fail the CHECK test, and a "check constraint" will /always/ fail the column length test. The two values will only differ when there are other exceptions in the mix, which will fail both tests. For example, suppose that two values fail the CHECK test with "length exceeded", one fails with "foo exception" and the remaining two pass. The first two will pass the column length test, and the remaining three will fail. */ $reporter->report( Reporter::STATUS_FAILURE, '%d illegal values %s rejected in both tests—check for something unusual.', array( $checkFails - $implicitPasses, Reporter::pluralise( $checkFails - $implicitPasses, 'was', 'were' ) ) ) ; } else { $reporter->report( Reporter::STATUS_NOTE, 'This is OK, but not necessarily safe, as the column length may change in future.', null ); } } } else { $reporter->report( Reporter::STATUS_SKIPPED, 'data tests, as failures in the structure testing mean that they may not work.', null ); } ?>
Show line notes below