Skip to content

Commit

Permalink
add more tests to increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
wordpressfan committed Jan 23, 2025
1 parent c023ef9 commit 47f2f83
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

return [
'test_data' => [
'testIsNotInPostPage' => [
'config' => [
'is_singular' => false,
],
'can_cache' => true,
],
'testValidPostPage' => [
'config' => [
'is_singular' => true,
'current_post_id' => 1,
'current_post_link' => 'http://example.com/test1',
'current_page_url' => 'http://example.com/test1',
],
'can_cache' => true,
],
'testValidPostPageWithPagination' => [
'config' => [
'is_singular' => true,
'current_post_id' => 1,
'current_post_link' => 'http://example.com/test1',
'current_page_url' => 'http://example.com/test1/page/2',
'page' => 2,
],
'can_cache' => true,
],
'testEmptyPostId' => [
'config' => [
'is_singular' => true,
'current_post_id' => 0,
'current_post_link' => '',
'current_post_url' => 'http://example.com/test1',
],
'can_cache' => true,
],
'testNotValidPostPage' => [
'config' => [
'is_singular' => true,
'current_post_id' => 1,
'current_post_link' => 'http://example.com/test1',
'current_post_url' => 'http://example.com/additional-query/test1',
],
'can_cache' => false,
],
'testNotValidPostPageWithPagination' => [
'config' => [
'is_singular' => true,
'current_post_id' => 1,
'current_post_link' => 'http://example.com/test1',
'current_page_url' => 'http://example.com/additional-query/test1/page/2',
'page' => 2,
],
'can_cache' => false,
],
'testValidPostPageWithNonLatinCharactersInUrl' => [
'config' => [
'is_singular' => true,
'current_post_id' => 1,
'current_post_link' => 'http://example.com/%D0%BF%D1%80%D0%BE%D0%B4%D1%83%D0%BA%D1%82%D0%BE%D0%B2%D0%B0-%D0%BA%D0%B0%D1%82%D0%B5%D0%B3%D0%BE%D1%80%D0%B8%D1%8F/',
'current_page_url' => 'http://example.com/продуктова-категория/',
],
'can_cache' => true,
],
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace WP_Rocket\Tests\Unit\inc\Engine\Cache\PostSubscriber;

use Brain\Monkey\Functions;
use WP_Rocket\Engine\Cache\PostSubscriber;
use WP_Rocket\Tests\Unit\TestCase;

/**
* Test class covering PostSubscriber::disable_cache_on_not_valid_pages
* @covers PostSubscriber::disable_cache_on_not_valid_pages
*
* @uses PostSubscriber::is_not_valid_page
*
* @group Cache
*/
class Test_DisableCacheOnNotValidPages extends TestCase {

Check failure on line 17 in tests/Unit/inc/Engine/Cache/PostSubscriber/disableCacheOnNotValidPages.php

View workflow job for this annotation

GitHub Actions / WPRocket lint with PHP Stan. PHP 8.2 on ubuntu-latest.

@Covers value PostSubscriber::disable_cache_on_not_valid_pages references an invalid method.
private $subscriber;

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

$this->subscriber = new PostSubscriber();
}


/**
* @dataProvider configTestData
*/
public function testShouldReturnExpected( $config, $can_cache ) {
Functions\expect( 'is_singular' )->once()->andReturn( ! empty( $config['is_singular'] ) );
Functions\when( 'get_queried_object_id' )->justReturn( $config['current_post_id'] ?? 0 );

Functions\when( 'get_permalink' )->justReturn( $config['current_post_link'] ?? '' );
Functions\when( 'add_query_arg' )->justReturn( '' );
Functions\when( 'home_url' )->justReturn( $config['current_page_url'] ?? '' );
Functions\when( 'is_paged' )->justReturn( ! empty( $config['page'] ) );
Functions\when( 'get_query_var' )->justReturn( $config['page'] ?? 0 );

$this->assertSame( $can_cache, $this->subscriber->disable_cache_on_not_valid_pages( true ) );
}
}

0 comments on commit 47f2f83

Please sign in to comment.