Skip to content

Commit

Permalink
Added unit test for CLI script.
Browse files Browse the repository at this point in the history
  • Loading branch information
mauteri committed Dec 30, 2023
1 parent dd606cd commit 158dc01
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
2 changes: 1 addition & 1 deletion includes/core/classes/commands/class-cli-event.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function rsvp( array $args = array(), array $assoc_args = array() ): void

$response = $event->rsvp->save( $user_id, $status );

WP_CLI::success(
static::success(
sprintf(
/* translators: %1$d: event ID, %2$s: attendance status, %3$d: user ID. */
__( 'The RSVP status for Event ID "%1$d" has been successfully set to "%2$s" for User ID "%3$d".', 'gatherpress' ),
Expand Down
4 changes: 3 additions & 1 deletion includes/core/classes/commands/class-cli-general.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class Cli_General extends WP_CLI {
* $ wp gatherpress generate_credits --version=1.0.0
* Success: New latest.php file has been generated.
*
* @codeCoverageIgnore Command is for internal purposes only.
*
* @since 1.0.0
*
* @param array $args Positional arguments for the script.
Expand Down Expand Up @@ -81,6 +83,6 @@ public function generate_credits( array $args = array(), array $assoc_args = arr
fwrite( $file, '<?php return ' . var_export( $data, true ) . ';' ); //phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fwrite,WordPress.PHP.DevelopmentFunctions.error_log_var_export
fclose( $file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fclose

WP_CLI::success( 'New latest.php file has been generated.' );
static::success( 'New latest.php file has been generated.' );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* Class handles unit tests for GatherPress\Core\Commands\Cli_Event.
*
* @package GatherPress
* @subpackage Core
* @since 1.0.0
*/

namespace GatherPress\Tests\Core\Commands;

use GatherPress\Core\Commands\Cli_Event;
use GatherPress\Core\Event;
use PMC\Unit_Test\Base;
use PMC\Unit_Test\Utility;

/**
* Class Test_Block.
*
* @coversDefaultClass \GatherPress\Core\Commands\Cli_Event
*/
class Test_Cli_Event extends Base {
/**
* Coverage for rsvp.
*
* @covers ::rsvp
*
* @return void
*/
public function test_rsvp(): void {
$cli_event = new Cli_Event();
$event = $this->mock->post( array( 'post_type' => Event::POST_TYPE ) )->get();
$user = $this->mock->user()->get();
$status = 'not_attending';
$assoc_args = array(
'event_id' => $event->ID,
'user_id' => $user->ID,
'status' => $status,
);

$output = Utility::buffer_and_return( array( $cli_event, 'rsvp' ), array( array(), $assoc_args ) );
$expects = sprintf(
'Success: The RSVP status for Event ID "%1$d" has been successfully set to "%2$s" for User ID "%3$d".',
$event->ID,
$status,
$user->ID,
);

$this->assertSame( $expects, $output, 'Failed to assert output matches.' );
}
}

0 comments on commit 158dc01

Please sign in to comment.