Skip to content

Instantly share code, notes, and snippets.

@PeterBooker
Last active October 6, 2018 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PeterBooker/d7b687118ae0b393feaa2277d44ed43c to your computer and use it in GitHub Desktop.
Save PeterBooker/d7b687118ae0b393feaa2277d44ed43c to your computer and use it in GitHub Desktop.
First PHP Unit Test
<?php
class Health_Check_Tests_Prof_Test extends WP_UnitTestCase {
private $health_check_site_status;
private $tests_list;
public function setUp() {
parent::setUp();
$this->$health_check_site_status = new Health_Check_Site_Status();
$this->$tests_list = Health_Check_Site_Status::get_tests();
}
private function runTest( $func ) {
$this->assertTrue(
method_exists( $this->$health_check_site_status, $func ) && is_callable( array( $this->$health_check_site_status, $func ) )
);
$start_time = microtime( true );
ob_start();
call_user_func( array( $this->$health_check_site_status, $func ) );
ob_end_clean();
return round( ( microtime( true ) - $start_time ) * 1000 );
}
public function testDirectTestsProf() {
$tests = $this->$tests_list['direct'];
foreach ( $tests as $test ) {
$test_function = sprintf(
'test_%s',
$test['test']
);
$result = $this->runTest( $test_function );
/**
* Result should be <= 100 miliseconds.
*/
$this->assertLessThanOrEqual(
100,
$result
);
}
}
public function testAsyncTestsProf() {
$tests = $this->$tests_list['async'];
foreach ( $tests as $test ) {
$test_function = sprintf(
'test_%s',
$test['test']
);
$result = $this->runTest( $test_function );
/**
* Result should be >= 100 miliseconds.
*/
$this->assertGreaterThanOrEqual(
100,
$result
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment