|
| 1 | +<?php |
| 2 | + |
| 3 | +require_once dirname( __DIR__, 2 ) . '/includes/class-wp-phpunit-timing-metrics.php'; |
| 4 | + |
| 5 | +/** |
| 6 | + * @group core-vitals-tooling |
| 7 | + * |
| 8 | + * @ticket 65887 |
| 9 | + */ |
| 10 | +class Tests_Includes_JUnit_Timing_Metrics extends WP_UnitTestCase { |
| 11 | + |
| 12 | + /** |
| 13 | + * Temporary files created by a test. |
| 14 | + * |
| 15 | + * @var string[] |
| 16 | + */ |
| 17 | + private $temporary_files = array(); |
| 18 | + |
| 19 | + public function tear_down() { |
| 20 | + foreach ( $this->temporary_files as $file ) { |
| 21 | + unlink( $file ); |
| 22 | + } |
| 23 | + |
| 24 | + parent::tear_down(); |
| 25 | + } |
| 26 | + |
| 27 | + public function test_extracts_aggregate_timing_metrics() { |
| 28 | + $times = array_map( |
| 29 | + static fn ( $millisecond ) => $millisecond / 1000, |
| 30 | + range( 1, 100 ) |
| 31 | + ); |
| 32 | + $file = $this->create_junit_file( $times, 5.05 ); |
| 33 | + |
| 34 | + $this->assertSame( |
| 35 | + array( |
| 36 | + 'phpunit-suite-time' => 5.05, |
| 37 | + 'phpunit-p95-test-time' => 95.0, |
| 38 | + 'phpunit-p99-test-time' => 99.0, |
| 39 | + 'phpunit-max-test-time' => 100.0, |
| 40 | + 'phpunit-tests-over-500ms' => 0, |
| 41 | + 'phpunit-tests-over-1s' => 0, |
| 42 | + ), |
| 43 | + WP_PHPUnit_Timing_Metrics::from_file( $file ) |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + public function test_counts_only_tests_above_slow_test_thresholds() { |
| 48 | + $file = $this->create_junit_file( array( 0.5, 0.500001, 1.0, 1.000001 ), 3.000002 ); |
| 49 | + |
| 50 | + $metrics = WP_PHPUnit_Timing_Metrics::from_file( $file ); |
| 51 | + |
| 52 | + $this->assertSame( 3, $metrics['phpunit-tests-over-500ms'] ); |
| 53 | + $this->assertSame( 1, $metrics['phpunit-tests-over-1s'] ); |
| 54 | + } |
| 55 | + |
| 56 | + public function test_uses_testcase_time_when_suite_time_is_missing() { |
| 57 | + $file = $this->create_junit_file( array( 0.1, 0.2, 0.3 ) ); |
| 58 | + |
| 59 | + $metrics = WP_PHPUnit_Timing_Metrics::from_file( $file ); |
| 60 | + |
| 61 | + $this->assertSame( 0.6, $metrics['phpunit-suite-time'] ); |
| 62 | + } |
| 63 | + |
| 64 | + public function test_rejects_report_without_testcases() { |
| 65 | + $file = $this->create_junit_file( array(), 0.0 ); |
| 66 | + |
| 67 | + $this->expectException( RuntimeException::class ); |
| 68 | + $this->expectExceptionMessage( 'The JUnit timing report contains no testcases.' ); |
| 69 | + |
| 70 | + WP_PHPUnit_Timing_Metrics::from_file( $file ); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Creates a JUnit XML file for a test. |
| 75 | + * |
| 76 | + * @param float[] $times Testcase times in seconds. |
| 77 | + * @param float|int $suite_time Optional testsuite time in seconds. |
| 78 | + * @return string Path to the temporary file. |
| 79 | + */ |
| 80 | + private function create_junit_file( $times, $suite_time = null ) { |
| 81 | + $file = tempnam( sys_get_temp_dir(), 'junit-timing-' ); |
| 82 | + |
| 83 | + if ( false === $file ) { |
| 84 | + $this->fail( 'Failed to create a temporary JUnit file.' ); |
| 85 | + } |
| 86 | + |
| 87 | + $this->temporary_files[] = $file; |
| 88 | + $suite_time_attribute = null === $suite_time ? '' : sprintf( ' time="%s"', $suite_time ); |
| 89 | + $testcases = ''; |
| 90 | + |
| 91 | + foreach ( $times as $index => $time ) { |
| 92 | + $testcases .= sprintf( '<testcase name="test_%1$d" time="%2$s"/>', $index, $time ); |
| 93 | + } |
| 94 | + |
| 95 | + file_put_contents( |
| 96 | + $file, |
| 97 | + sprintf( '<?xml version="1.0"?><testsuites><testsuite tests="%1$d"%2$s>%3$s</testsuite></testsuites>', count( $times ), $suite_time_attribute, $testcases ) |
| 98 | + ); |
| 99 | + |
| 100 | + return $file; |
| 101 | + } |
| 102 | +} |
0 commit comments