Skip to content

Commit

Permalink
add test for dismiss notice
Browse files Browse the repository at this point in the history
  • Loading branch information
remyperona committed Oct 31, 2024
1 parent d395948 commit 806bc03
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
38 changes: 38 additions & 0 deletions tests/Fixtures/src/Admin/Notices/Notices/dismissNotice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

return [
'testShouldSendErrorWhenNoCap' => [
'config' => [
'current_user_can' => false,
'notice_id' => 'update_notice',
'user_meta' => [],
],
'expected' => 'You do not have permissions to perform this action.',
],
'testShouldSendErrorWhenNoNoticeId' => [
'config' => [
'current_user_can' => true,
'notice_id' => '',
'user_meta' => [],
],
'expected' => 'The notice ID is missing',
],
'testShouldSendErrorWhenDismissed' => [
'config' => [
'current_user_can' => true,
'notice_id' => 'update_notice',
'user_meta' => [
'update_notice',
],
],
'expected' => 'The notice is already dismissed',
],
'testShouldSendSuccess' => [
'config' => [
'current_user_can' => true,
'notice_id' => 'update_notice',
'user_meta' => [],
],
'expected' => 'Notice dismissed',
],
];
69 changes: 69 additions & 0 deletions tests/Unit/src/Admin/Notices/Notices/dismissNotice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
declare(strict_types=1);

namespace RocketCDN\Tests\Unit\src\Admin\Notices\Notices;

use Brain\Monkey\Functions;
use Mockery;
use RocketCDN\Admin\Notices\Notices;
use RocketCDN\API\Client;
use RocketCDN\Dependencies\LaunchpadOptions\Options;
use RocketCDN\Tests\Unit\TestCase;

/**
* @covers \RocketCDN\Admin\Notices\Notices::dismiss_notice
*
* @group Admin
* @group Notices
*/
class TestDismissNotice extends TestCase {
protected $options;
protected $client;
protected $notices;

protected function setUp(): void {
parent::setUp();

$this->options = Mockery::mock( Options::class );
$this->client = Mockery::mock( Client::class );
$this->notices = new Notices( $this->client );
$this->notices->set_options( $this->options );

$this->stubTranslationFunctions();
}

/**
* @dataProvider configTestData
*/
public function testShouldDoExpected( $config, $expected ) {
Functions\when( 'check_ajax_referer' )->justReturn( true );

Functions\when( 'current_user_can' )->justReturn( $config['current_user_can'] );

Functions\when( 'sanitize_key' )->returnArg();

$_POST['notice_id'] = $config['notice_id'];

Functions\when( 'get_current_user_id' )->justReturn( 1 );
Functions\when( 'get_user_meta' )->justReturn( $config['user_meta'] );

Functions\expect( 'update_user_meta' )
->atMost()
->once()
->with( 1, 'dismissed_notices', [ $config['notice_id'] ] );

Functions\when( 'wp_send_json_error' )
->alias( function() use ( $expected ) {
throw new \Exception( $expected );
} );
Functions\when( 'wp_send_json_success' )
->alias( function() use ( $expected ) {
throw new \Exception( $expected );
} );

$this->expectException( \Exception::class );
$this->expectExceptionMessage( $expected );

$this->notices->dismiss_notice();
}
}

0 comments on commit 806bc03

Please sign in to comment.