Skip to content

Commit

Permalink
Tests: Code QOL improvements
Browse files Browse the repository at this point in the history
- Replace nested dirname() calls
- Remove unnecessary local variables
- Replace qualifiers with an import
- Unwrap unnecessary typecast
- Use strpos() instead of substr()
- Use instanceof instead of is_a()
- Update DocBlocks
- Use decrement operator
- Replace misused foreach() with array_sum()
- Declare closure static
- Declare access modifiers
- Declare return types where possible
- Add parameter and property types
- Use more appropriate assertions
- Improve regular expression
- Remove unnecessary time() argument from strotime() calls
- Add more entropy to uniqid() calls
- Remove unused code
  • Loading branch information
GaryJones committed Feb 7, 2024
1 parent 145ed78 commit 3d156c9
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 165 deletions.
26 changes: 15 additions & 11 deletions tests/CPTTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ class CPTTest extends TestCase {
/**
* Custom Post Type
*
* @var str
* @var string
*/
const TEST_CPT = 'msm_stest';
public const TEST_CPT = 'msm_stest';

/**
* Initialize Test CPT
*/
static function setupBeforeClass(): void {
public static function setupBeforeClass(): void {
register_post_type(
self::TEST_CPT,
array(
Expand All @@ -50,8 +50,8 @@ static function setupBeforeClass(): void {
/**
* Initialize MSM_SiteMap_Test
*/
function setup(): void {
// Create Multiple Posts acorss various Dates.
public function setup(): void {
// Create Multiple Posts across various Dates.
$date = time();

// 3 for Today, 1 in "draft" status
Expand All @@ -68,7 +68,7 @@ function setup(): void {
}

// 1 CPT draft for a month ago
$date = strtotime( '-1 month', time() );
$date = strtotime( '-1 month' );
$cur_day = date( 'Y', $date ) . '-' . date( 'm', $date ) . '-' . date( 'd', $date ) . ' 00:00:00';
$this->create_dummy_post( $cur_day, 'draft', self::TEST_CPT );

Expand All @@ -80,7 +80,7 @@ function setup(): void {
/**
* Remove the sample posts and the sitemap posts
*/
function teardown(): void {
public function teardown(): void {
$this->posts = array();
$sitemaps = get_posts( array(
'post_type' => Metro_Sitemap::SITEMAP_CPT,
Expand All @@ -94,7 +94,8 @@ function teardown(): void {
/**
* Verify Custom Post Types don't have Sitemaps generated by default
*/
function test_cpt_ignored_by_default() {
public function test_cpt_ignored_by_default(): void
{

$this->build_sitemaps();
$sitemaps = get_posts( array(
Expand All @@ -111,7 +112,8 @@ function test_cpt_ignored_by_default() {
* Verify Custom Post Types included when "msm_sitemap_entry_post_type"
* filter applied
*/
function test_cpt_included_by_filter() {
public function test_cpt_included_by_filter(): void
{

add_filter( 'msm_sitemap_entry_post_type', array( $this, 'add_cpt_to_msm_sitemap' ) );

Expand All @@ -130,9 +132,11 @@ function test_cpt_included_by_filter() {
* Filter hook to add TEST_CPT to Metro_Sitemap supported post types
*
* @param array $cpts Array of Post Types.
* @return array(str) Array of Post Types.
*
* @return array<string> Array of Post Types.
*/
public function add_cpt_to_msm_sitemap( $cpts ) {
public function add_cpt_to_msm_sitemap( array $cpts ): array
{
$cpts[] = self::TEST_CPT;
return $cpts;
}
Expand Down
18 changes: 11 additions & 7 deletions tests/CreationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Metro_Sitemap;
use MSM_Sitemap_Builder_Cron;
use WP_Post;

/**
* Unit Tests to confirm Sitemaps are generated.
Expand All @@ -23,12 +24,12 @@ class CreationTest extends TestCase {
*
* @var Integer
*/
private $num_days = 4;
private int $num_days = 4;

/**
* Generate posts and build the sitemap
*/
function setup(): void {
public function setup(): void {
// Create posts for the last num_days days.
$dates = array();
$date = time();
Expand All @@ -45,7 +46,7 @@ function setup(): void {
/**
* Remove the sample posts and the sitemap posts
*/
function teardown(): void {
public function teardown(): void {
$this->posts = array();
$sitemaps = get_posts( array(
'post_type' => Metro_Sitemap::SITEMAP_CPT,
Expand All @@ -60,7 +61,8 @@ function teardown(): void {
/**
* Validate the XML stored in the database after sitemap generation
*/
function test_sitemap_posts_were_created() {
public function test_sitemap_posts_were_created(): void
{
global $post;

$sitemaps = get_posts( array(
Expand Down Expand Up @@ -96,7 +98,8 @@ function test_sitemap_posts_were_created() {
/**
* Validate that get_sitemap_post_id function returns the expected Sitemap
*/
function test_get_sitemap_post_id() {
public function test_get_sitemap_post_id(): void
{

// Get yesterday's sitemap post.
$date = strtotime( '-1 day' );
Expand All @@ -108,7 +111,7 @@ function test_get_sitemap_post_id() {
$sitemap_post_id = Metro_Sitemap::get_sitemap_post_id( $sitemap_year, $sitemap_month, $sitemap_day );
$sitemap_post = get_post( $sitemap_post_id );

$this->assertTrue( is_a( $sitemap_post, 'WP_Post' ), 'get_sitemap_post_id returned non-WP_Post value' );
$this->assertInstanceOf( WP_Post::class, $sitemap_post, 'get_sitemap_post_id returned non-WP_Post value' );
$this->assertEquals( $sitemap_ymd, $sitemap_post->post_title );

}
Expand All @@ -117,7 +120,8 @@ function test_get_sitemap_post_id() {
* Validate that Metro Sitemap CPTs are deleted when all posts are removed
* for a date
*/
function test_delete_empty_sitemap() {
public function test_delete_empty_sitemap(): void
{
global $wpdb;

list( $sitemap ) = get_posts( array(
Expand Down
24 changes: 7 additions & 17 deletions tests/CronTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ class CronTest extends TestCase {
*
* @var Integer
*/
private $num_years_data = 2;
private int $num_years_data = 2;

/**
* Generate posts and build the sitemap
*/
function setup(): void {
public function setup(): void {
if ( ! class_exists( 'MSM_Sitemap_Builder_Cron' ) ) {
require dirname( dirname( __FILE__ ) ) . '/includes/msm-sitemap-builder-cron.php';
require dirname( __FILE__, 2 ) . '/includes/msm-sitemap-builder-cron.php';
MSM_Sitemap_Builder_Cron::setup();
}

Expand All @@ -49,7 +49,7 @@ function setup(): void {
/**
* Remove the sample posts and the sitemap posts
*/
function teardown(): void {
public function teardown(): void {
$this->posts = array();
$sitemaps = get_posts( array(
'post_type' => Metro_Sitemap::SITEMAP_CPT,
Expand All @@ -63,16 +63,15 @@ function teardown(): void {
/**
* Validate that Cron Jobs are scheduled as expected.
*/
function test_cron_jobs_scheduling() {
public function test_cron_jobs_scheduling(): void
{

// Reset Cron SitemapBuilder.
MSM_Sitemap_Builder_Cron::reset_sitemap_data();
delete_option( 'msm_stop_processing' );
MSM_Sitemap_Builder_Cron::generate_full_sitemap();
update_option( 'msm_sitemap_create_in_progress', true );

$days_being_processed = (array) get_option( 'msm_days_to_process', array() );
$months_being_processed = (array) get_option( 'msm_months_to_process', array() );
$years_being_processed = (array) get_option( 'msm_years_to_process', array() );

// Validate initial Options is set to years for Posts.
Expand All @@ -87,19 +86,16 @@ function test_cron_jobs_scheduling() {
// fake_cron.
$this->fake_cron();

$days_being_processed = (array) get_option( 'msm_days_to_process', array() );
$months_being_processed = (array) get_option( 'msm_months_to_process', array() );
$years_being_processed = (array) get_option( 'msm_years_to_process', array() );

// Validate Current Month is added to months_to_process.
$month = (int) date( 'n', time() );
$month = (int) date( 'n' );
$this->assertContains( $month, $months_being_processed, 'Initial Year Processing should use Current Month if same year' );

// fake_cron.
$this->fake_cron();

$days_being_processed = (array) get_option( 'msm_days_to_process', array() );
$months_being_processed = (array) get_option( 'msm_months_to_process', array() );
$years_being_processed = (array) get_option( 'msm_years_to_process', array() );

$expected_days = range( 1, date( 'j' ) );
Expand All @@ -114,8 +110,6 @@ function test_cron_jobs_scheduling() {
}

// Check New Year.
$days_being_processed = (array) get_option( 'msm_days_to_process', array() );
$months_being_processed = (array) get_option( 'msm_months_to_process', array() );
$years_being_processed = (array) get_option( 'msm_years_to_process', array() );

// Validate initial Options is set to years for Posts.
Expand All @@ -129,9 +123,7 @@ function test_cron_jobs_scheduling() {
// fake_cron.
$this->fake_cron();

$days_being_processed = (array) get_option( 'msm_days_to_process', array() );
$months_being_processed = (array) get_option( 'msm_months_to_process', array() );
$years_being_processed = (array) get_option( 'msm_years_to_process', array() );

// Validate Current Month is added to months_to_process.
$month = 12;
Expand All @@ -141,8 +133,6 @@ function test_cron_jobs_scheduling() {
$this->fake_cron();

$days_being_processed = (array) get_option( 'msm_days_to_process', array() );
$months_being_processed = (array) get_option( 'msm_months_to_process', array() );
$years_being_processed = (array) get_option( 'msm_years_to_process', array() );

$this->assertGreaterThanOrEqual( 27, count( $days_being_processed ), 'New Month Processing should star at end of Month' );

Expand Down
11 changes: 7 additions & 4 deletions tests/FiltersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ class FiltersTest extends TestCase {
/**
* Verify that request for sitemap url doesn't cause Main Query to hit db.
*/
function test_bypass_main_query() {
public function test_bypass_main_query(): void
{
global $wp_query;

// Verify post_pre_query on sitemap queryvar returns empty array
// Verify post_pre_query on sitemap query_var returns empty array
set_query_var( 'sitemap', 'true' );
$posts = apply_filters_ref_array( 'posts_pre_query', array( null, $wp_query ) );
$this->assertIsArray( $posts );
Expand All @@ -34,7 +35,8 @@ function test_bypass_main_query() {
/**
* Verify that secondary query is not get modified if sitemap var is set.
*/
function test_secondary_query_not_bypassed() {
public function test_secondary_query_not_bypassed(): void
{

// Verify post_pre_query filter returns null by default
$exp_result = array(1);
Expand All @@ -51,7 +53,8 @@ function test_secondary_query_not_bypassed() {
/**
* Verify that msm_sitemap_index filter runs when build_root_sitemap_xml is called.
*/
function test_msm_sitemap_index_filter_ran() {
public function test_msm_sitemap_index_filter_ran(): void
{
$ran = false;

add_filter( 'msm_sitemap_index', function() use ( &$ran ) { $ran = true; return []; } );
Expand Down
Loading

0 comments on commit 3d156c9

Please sign in to comment.