Skip to content

Commit

Permalink
Merge pull request #46 from sectsect/feature/test-formatting
Browse files Browse the repository at this point in the history
test: code formatting with phpcs
  • Loading branch information
sectsect authored Apr 30, 2024
2 parents b9ac288 + a3c6f92 commit 679b22f
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 11 deletions.
1 change: 0 additions & 1 deletion .phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@

<exclude-pattern>*/node_modules/*</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern>
<exclude-pattern>*/tests/*</exclude-pattern>
</ruleset>
22 changes: 22 additions & 0 deletions tests/FunctionTests.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@
<?php
/**
* FunctionsTest class
*
* Tests the functionality of functions in functions.php.
*
* @package YourPackageName
* @subpackage Tests
*/

/**
* Class FunctionsTest
* Tests the functionality of functions in functions.php.
*
* @coversDefaultClass Functions
*/
class FunctionsTest extends WP_UnitTestCase {
/**
* Test the google_ss2db_truncate_middle function.
*
* @dataProvider providerTruncateMiddle
* @covers ::google_ss2db_truncate_middle
*
* @param string $input The input string to truncate.
* @param int $max_chars The maximum number of characters to keep.
* @param string $expected The expected truncated string.
*/
public function test_google_ss2db_truncate_middle( $input, $max_chars, $expected ) {
require_once 'functions/functions.php'; // Include the file where the function is defined.
$result = google_ss2db_truncate_middle( $input, $max_chars );
$this->assertEquals( $expected, $result );
}

/**
* Data provider for test_google_ss2db_truncate_middle.
*
* @return array Test data.
*/
public function providerTruncateMiddle() {
return array(
array( 'HelloWorld', 5, 'He...rld' ), // Basic case.
Expand All @@ -27,6 +48,7 @@ public function providerTruncateMiddle() {

/**
* Tests the output of the google_ss2db_options_pagination function.
*
* @covers ::google_ss2db_options_pagination
*/
public function testPaginationOutput() {
Expand Down
13 changes: 11 additions & 2 deletions tests/GoogleSpreadsheetToDBActivatorTests.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
<?php
/**
* Tests for the Google_Spreadsheet_To_DB_Activator class.
*
* @package Google_Spreadsheet_To_DB
* @subpackage Tests
*/

// Require the class file.
require_once __DIR__ . '/../includes/class-google-spreadsheet-to-db-activator.php';

/**
* Class for testing the Google_Spreadsheet_To_DB_Activator activation functionality.
*
* This class extends WP_UnitTestCase to test the activation method of the Google_Spreadsheet_To_DB_Activator class.
*
* @covers Google_Spreadsheet_To_DB_Activator::activate
*/

class Test_Google_Spreadsheet_To_DB_Activator extends WP_UnitTestCase {

/**
* Test the activate method when no previous version is installed.
*
* @covers Google_Spreadsheet_To_DB_Activator::activate
*/
public function test_activate_no_previous_version() {
Expand All @@ -35,6 +44,7 @@ public function test_activate_no_previous_version() {

/**
* Test the activate method when the installed version is outdated.
*
* @covers Google_Spreadsheet_To_DB_Activator::activate
*/
public function test_activate_outdated_version() {
Expand All @@ -45,7 +55,6 @@ public function test_activate_outdated_version() {

// Call the activate method.
Google_Spreadsheet_To_DB_Activator::activate();

// Check if the version is updated.
$installed_ver = get_option( 'google_ss2db_version' );
$this->assertEquals( '1.2.0', $installed_ver );
Expand Down
33 changes: 28 additions & 5 deletions tests/GoogleSpreadsheetToDBQueryTests.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
<?php
/**
* Tests for the Google_Spreadsheet_To_DB_Query class.
*
* @package Google_Spreadsheet_To_DB_Query
*/

// Require the class file.
require_once __DIR__ . '/../includes/class-google-spreadsheet-to-db-query.php';

/**
* Class Test_Google_Spreadsheet_To_DB_Query
* Tests the Google_Spreadsheet_To_DB_Query class functionality.
*
* @covers Google_Spreadsheet_To_DB_Query
*/
class Google_Spreadsheet_To_DB_Query_Test extends WP_UnitTestCase {
/**
* Mock data for testing.
*
* @var array
*/
protected $mock_data;

/**
* Set up the test environment.
*/
public function setUp(): void {
parent::setUp();
// Mock response from Google Sheets API.
Expand Down Expand Up @@ -54,6 +70,7 @@ public function setUp(): void {

/**
* Test getting all rows.
*
* @covers Google_Spreadsheet_To_DB_Query::getrow
*/
public function test_get_all_rows() {
Expand All @@ -71,6 +88,7 @@ public function test_get_all_rows() {

/**
* Test getting 3 rows starting from the 4th row, ordered by ID in ascending order.
*
* @covers Google_Spreadsheet_To_DB_Query::getrow
*/
public function test_get_3_rows_from_4th_ascending_by_id() {
Expand All @@ -97,6 +115,7 @@ public function test_get_3_rows_from_4th_ascending_by_id() {

/**
* Test getting a row with a specific ID.
*
* @covers Google_Spreadsheet_To_DB_Query::getrow
*/
public function test_get_row_with_specific_id() {
Expand All @@ -119,7 +138,7 @@ public function test_get_row_with_specific_id() {
array_filter(
$this->mock_data,
function ( $row ) {
return $row['id'] == 3;
return 3 === $row['id'];
}
)
);
Expand All @@ -132,6 +151,7 @@ function ( $row ) {

/**
* Test getting 3 rows with a specific worksheet name, ordered by ID.
*
* @covers Google_Spreadsheet_To_DB_Query::getrow
*/
public function test_get_3_rows_with_specific_worksheet_ordered_by_id() {
Expand All @@ -158,7 +178,7 @@ function () {
$filtered = array_filter(
$this->mock_data,
function ( $row ) {
return $row['worksheet_name'] == 'Sheet 1';
return 'Sheet 1' === $row['worksheet_name'];
}
);
usort(
Expand All @@ -183,6 +203,7 @@ function ( $a, $b ) {

/**
* Test getting rows with a specific sheet name.
*
* @covers Google_Spreadsheet_To_DB_Query::getrow
*/
public function test_get_rows_with_specific_sheet_name() {
Expand All @@ -205,7 +226,7 @@ public function test_get_rows_with_specific_sheet_name() {
array_filter(
$this->mock_data,
function ( $row ) {
return $row['sheet_name'] == 'Data 3';
return 'Data 3' === $row['sheet_name'];
}
)
);
Expand All @@ -218,6 +239,7 @@ function ( $row ) {

/**
* Test getting rows with a specific title.
*
* @covers Google_Spreadsheet_To_DB_Query::getrow
*/
public function test_get_rows_with_specific_title() {
Expand All @@ -240,7 +262,7 @@ public function test_get_rows_with_specific_title() {
array_filter(
$this->mock_data,
function ( $row ) {
return $row['title'] == 'Title 4';
return 'Title 4' === $row['title'];
}
)
);
Expand All @@ -253,6 +275,7 @@ function ( $row ) {

/**
* Test getting the 2nd row with a date greater than or equal to '2023-06-03 12:00:00' and a specific worksheet name, ordered by ID in descending order.
*
* @covers Google_Spreadsheet_To_DB_Query::getrow
*/
public function test_get_2nd_row_with_date_gte_and_specific_worksheet_ordered_by_id_desc() {
Expand Down Expand Up @@ -285,7 +308,7 @@ public function test_get_2nd_row_with_date_gte_and_specific_worksheet_ordered_by
array_filter(
$this->mock_data,
function ( $row ) {
return $row['date'] >= '2023-06-03 12:00:00' && $row['worksheet_name'] == 'Sheet 2';
return $row['date'] >= '2023-06-03 12:00:00' && 'Sheet 2' === $row['worksheet_name'];
}
),
1,
Expand Down
11 changes: 8 additions & 3 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
<?php
/**
* PHPUnit bootstrap file.
* PHPUnit bootstrap file for the Google Spreadsheet to DB plugin.
*
* This file is used to set up the testing environment for the plugin.
* It loads the necessary dependencies, configures the test directory,
* and manually loads the plugin being tested.
*
* @package Google_Spreadsheet_to_DB
*/

require_once dirname( dirname( __FILE__ ) ) . '/vendor/autoload.php';
// Load the Composer autoloader.
require_once dirname( __DIR__, 1 ) . '/vendor/autoload.php';

$_tests_dir = getenv( 'WP_TESTS_DIR' );

Expand All @@ -31,7 +36,7 @@
* Manually load the plugin being tested.
*/
function _manually_load_plugin() {
require dirname( dirname( __FILE__ ) ) . '/google-spreadsheet-to-db.php';
require dirname( __DIR__, 1 ) . '/google-spreadsheet-to-db.php';
}

tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );
Expand Down

0 comments on commit 679b22f

Please sign in to comment.