Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
remyperona committed Nov 13, 2024
1 parent 22e9f61 commit 85b6385
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

return [
'shouldReturnOptimizedTagWhenSingleTagGiven' => [
'given' =>
'config' => [
'swap' => false,
'disable_preload' => false,
],
'html' =>
'<!doctype html>
<html>
<head>
Expand All @@ -29,7 +33,11 @@
</html>'
],
'shouldUseFilteredDisplayValue' => [
'given' =>
'config' => [
'swap' => 'optional',
'disable_preload' => false,
],
'html' =>
'<!doctype html>
<html>
<head>
Expand All @@ -55,10 +63,13 @@
</body>
</html>'
,
'filtered' => 'optional',
],
'shouldNotCombineMultipleTagsWithTextParam' => [
'given' =>
'config' => [
'swap' => false,
'disable_preload' => false,
],
'html' =>
'<!doctype html>
<html>
<head>
Expand Down Expand Up @@ -87,7 +98,11 @@
</html>'
],
'shouldCombineMultipleTags' => [
'given' =>
'config' => [
'swap' => false,
'disable_preload' => false,
],
'html' =>
'<!doctype html>
<html>
<head>
Expand Down Expand Up @@ -115,7 +130,11 @@
</html>'
],
'shouldCombineMultipleTagsWithMultipleFamiliesInTag' => [
'given' =>
'config' => [
'swap' => false,
'disable_preload' => false,
],
'html' =>
'<!doctype html>
<html>
<head>
Expand Down Expand Up @@ -145,7 +164,11 @@
</html>'
],
'shouldRemovePreconnectWhenNoGoogleFontsPresentOnPage' => [
'given' =>
'config' => [
'swap' => false,
'disable_preload' => false,
],
'html' =>
'<!doctype html>
<html>
<head>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,59 +13,66 @@
* @group GoogleFonts
*/
class Test_Optimize extends TestCase {

private $filter_value;
private $display;
private $disable_preload;

public function set_up() {
parent::set_up();

$GLOBALS['wp'] = (object) [
'query_vars' => [],
'request' => 'http://example.org',
'public_query_vars' => [
'embed',
],
];

$this->unregisterAllCallbacksExcept('rocket_buffer', 'process', 17 );
}

public function tear_down() {
remove_filter( 'pre_get_rocket_option_minify_google_fonts', [ $this, 'return_true' ] );
remove_filter( 'rocket_combined_google_fonts_display', [ $this, 'set_display_value' ] );
remove_filter( 'rocket_disable_google_fonts_preload', [ $this, 'set_disable_preload' ] );

unset( $this->filter_value );
$this->restoreWpHook('rocket_buffer');

parent::tear_down();
}

/**
* @dataProvider addDataProviderV1
*/
public function testShouldCombineGoogleFontsV1( $original, $combined, $filtered = false ) {
$this->doTest( $original, $combined, $filtered );
public function testShouldCombineGoogleFontsV1( $config, $original, $combined ) {
$this->doTest( $config, $original, $combined );
}

/**
* @dataProvider addDataProviderV2
*/
public function testShouldCombineGoogleFontsV2( $original, $combined, $filtered = false ) {
$this->doTest( $original, $combined, $filtered );
public function testShouldCombineGoogleFontsV2( $config, $original, $combined ) {
$this->doTest( $config, $original, $combined );
}

/**
* @dataProvider addDataProviderV1V2
*/
public function testShouldCombineGoogleFontsV1V2( $original, $combined, $filtered = false ) {
$this->doTest( $original, $combined, $filtered );
public function testShouldCombineGoogleFontsV1V2( $config, $original, $combined ) {
$this->doTest( $config, $original, $combined );
}

private function doTest( $original, $expected, $filtered ) {
private function doTest( $config, $original, $expected ) {
$this->display = $config['swap'];
$this->disable_preload = $config['disable_preload'];

add_filter( 'pre_get_rocket_option_minify_google_fonts', [ $this, 'return_true' ] );

if ( $filtered ) {
$this->filter_value = $filtered;
if ( false !== $config['swap'] ) {
add_filter( 'rocket_combined_google_fonts_display', [ $this, 'set_display_value' ] );
}

add_filter( 'rocket_disable_google_fonts_preload', [ $this, 'set_disable_preload' ] );

$actual = apply_filters( 'rocket_buffer', $original );

$this->assertSame(
Expand All @@ -86,7 +93,11 @@ public function addDataProviderV1V2() {
return $this->getTestData( __DIR__ . 'V1V2', 'optimize' );
}

public function set_display_value( $filtered ) {
return $this->filter_value;
public function set_display_value() {
return $this->display;
}

public function set_disable_preload() {
return $this->disable_preload;
}
}

0 comments on commit 85b6385

Please sign in to comment.