From 3fd9565a9395549f1a70c024d894c3bd2975feba Mon Sep 17 00:00:00 2001 From: sect Date: Sun, 28 Apr 2024 21:59:54 +0900 Subject: [PATCH 1/5] test: add a unit test --- tests/FunctionTests.php | 116 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 tests/FunctionTests.php diff --git a/tests/FunctionTests.php b/tests/FunctionTests.php new file mode 100644 index 0000000..222a912 --- /dev/null +++ b/tests/FunctionTests.php @@ -0,0 +1,116 @@ +assertEquals( $expected, $result ); + } + + public function providerTruncateMiddle() { + return array( + array( 'HelloWorld', 5, 'He...d' ), // Basic case. + array( 'HelloWorld', 10, 'HelloWorld' ), // Max chars equal to string length. + array( 'HelloWorld', 0, '...' ), // Max chars is zero. + array( '', 5, '' ), // Empty string. + array( 'HelloWorld', 1, '...' ), // Max chars is one. + ); + } + + /** + * @dataProvider providerOptionsPagination + */ + public function test_google_ss2db_options_pagination( $options, $current_page, $expected ) { + require_once 'functions/functions.php'; // Include the file where the function is defined. + $result = google_ss2db_options_pagination( $options, $current_page ); + $this->assertEquals( $expected, $result ); + } + + public function providerOptionsPagination() { + return array( + array( + array( + 'items' => 100, + 'itemsPerPage' => 10, + ), + 1, + array( + 'start' => 0, + 'end' => 10, + ), + ), // First page. + array( + array( + 'items' => 100, + 'itemsPerPage' => 10, + ), + 5, + array( + 'start' => 40, + 'end' => 50, + ), + ), // Middle page. + array( + array( + 'items' => 100, + 'itemsPerPage' => 10, + ), + 10, + array( + 'start' => 90, + 'end' => 100, + ), + ), // Last page. + array( + array( + 'items' => 100, + 'itemsPerPage' => 10, + ), + 0, + array( + 'start' => 0, + 'end' => 10, + ), + ), // Page number too low. + array( + array( + 'items' => 100, + 'itemsPerPage' => 10, + ), + 11, + array( + 'start' => 100, + 'end' => 100, + ), + ), // Page number too high. + array( + array( + 'items' => 50, + 'itemsPerPage' => 25, + ), + 2, + array( + 'start' => 25, + 'end' => 50, + ), + ), // Exact fit last page. + array( + array( + 'items' => 0, + 'itemsPerPage' => 10, + ), + 1, + array( + 'start' => 0, + 'end' => 0, + ), + ), // No items. + ); + } +} From 272aedd38cb59cb13b427eb405b0a016cdaafd26 Mon Sep 17 00:00:00 2001 From: sect Date: Sun, 28 Apr 2024 22:18:20 +0900 Subject: [PATCH 2/5] test: fix assertions of test --- tests/FunctionTests.php | 99 ++--------------------------------------- 1 file changed, 4 insertions(+), 95 deletions(-) diff --git a/tests/FunctionTests.php b/tests/FunctionTests.php index 222a912..24117e7 100644 --- a/tests/FunctionTests.php +++ b/tests/FunctionTests.php @@ -15,102 +15,11 @@ public function test_google_ss2db_truncate_middle( $input, $max_chars, $expected public function providerTruncateMiddle() { return array( - array( 'HelloWorld', 5, 'He...d' ), // Basic case. - array( 'HelloWorld', 10, 'HelloWorld' ), // Max chars equal to string length. + array( 'HelloWorld', 5, 'He...rld' ), // Basic case. + array( 'HelloWorld', 10, 'Hello...World' ), // Max chars equal to string length. array( 'HelloWorld', 0, '...' ), // Max chars is zero. - array( '', 5, '' ), // Empty string. - array( 'HelloWorld', 1, '...' ), // Max chars is one. - ); - } - - /** - * @dataProvider providerOptionsPagination - */ - public function test_google_ss2db_options_pagination( $options, $current_page, $expected ) { - require_once 'functions/functions.php'; // Include the file where the function is defined. - $result = google_ss2db_options_pagination( $options, $current_page ); - $this->assertEquals( $expected, $result ); - } - - public function providerOptionsPagination() { - return array( - array( - array( - 'items' => 100, - 'itemsPerPage' => 10, - ), - 1, - array( - 'start' => 0, - 'end' => 10, - ), - ), // First page. - array( - array( - 'items' => 100, - 'itemsPerPage' => 10, - ), - 5, - array( - 'start' => 40, - 'end' => 50, - ), - ), // Middle page. - array( - array( - 'items' => 100, - 'itemsPerPage' => 10, - ), - 10, - array( - 'start' => 90, - 'end' => 100, - ), - ), // Last page. - array( - array( - 'items' => 100, - 'itemsPerPage' => 10, - ), - 0, - array( - 'start' => 0, - 'end' => 10, - ), - ), // Page number too low. - array( - array( - 'items' => 100, - 'itemsPerPage' => 10, - ), - 11, - array( - 'start' => 100, - 'end' => 100, - ), - ), // Page number too high. - array( - array( - 'items' => 50, - 'itemsPerPage' => 25, - ), - 2, - array( - 'start' => 25, - 'end' => 50, - ), - ), // Exact fit last page. - array( - array( - 'items' => 0, - 'itemsPerPage' => 10, - ), - 1, - array( - 'start' => 0, - 'end' => 0, - ), - ), // No items. + array( '', 5, '...' ), // Empty string. + array( 'HelloWorld', 1, '...d' ), // Max chars is one. ); } } From edca7f4bb9e9118b4d7b16d97fa2bb16a5bc174f Mon Sep 17 00:00:00 2001 From: sect Date: Sun, 28 Apr 2024 22:29:33 +0900 Subject: [PATCH 3/5] test: add a unit test --- tests/FunctionTests.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/FunctionTests.php b/tests/FunctionTests.php index 24117e7..662b91c 100644 --- a/tests/FunctionTests.php +++ b/tests/FunctionTests.php @@ -22,4 +22,26 @@ public function providerTruncateMiddle() { array( 'HelloWorld', 1, '...d' ), // Max chars is one. ); } + + /** + * Tests the output of the google_ss2db_options_pagination function. + */ + public function testPaginationOutput() { + // Pagination settings. + $current_page = 1; + $total_pages = 5; + $range = 2; + + // Expected output. + $expected_output = '
    '; + $expected_output .= '
  • '; + $expected_output .= '
  • »
  • '; + $expected_output .= '
'; + + // Capture the output. + $this->expectOutputString( $expected_output ); + + // Execute the pagination function. + google_ss2db_options_pagination( $current_page, $total_pages, $range ); + } } From 7ad3fd5d094829bf32dd6037e61d050792743c07 Mon Sep 17 00:00:00 2001 From: sect Date: Sun, 28 Apr 2024 22:36:37 +0900 Subject: [PATCH 4/5] test: fix test --- tests/FunctionTests.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/FunctionTests.php b/tests/FunctionTests.php index 662b91c..3736cec 100644 --- a/tests/FunctionTests.php +++ b/tests/FunctionTests.php @@ -34,8 +34,11 @@ public function testPaginationOutput() { // Expected output. $expected_output = '
    '; - $expected_output .= '
  • '; - $expected_output .= '
  • »
  • '; + $expected_output .= '
  • 1
  • '; + $expected_output .= '
  • 2
  • '; + $expected_output .= '
  • 3
  • '; + $expected_output .= '
  • 4
  • '; + $expected_output .= '
  • 5
  • '; $expected_output .= '
'; // Capture the output. From e7ce207d0dbbf0577d5ee7281267c538c2bf9243 Mon Sep 17 00:00:00 2001 From: sect Date: Sun, 28 Apr 2024 22:43:57 +0900 Subject: [PATCH 5/5] fix: fix PHP deprecated notices --- functions/functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functions/functions.php b/functions/functions.php index 8b84cc0..dcb544a 100644 --- a/functions/functions.php +++ b/functions/functions.php @@ -76,5 +76,5 @@ function google_ss2db_options_pagination( int $paged = 1, int $pages = 1, int $r function google_ss2db_truncate_middle( string $str, int $max_chars = 16 ): string { $str_length = strlen( $str ); - return substr_replace( $str, '...', $max_chars / 2, $str_length - $max_chars ); + return substr_replace( $str, '...', (int) floor( $max_chars / 2 ), $str_length - $max_chars ); }