Skip to content

Commit

Permalink
Reset wp_webfonts instance before each test is run
Browse files Browse the repository at this point in the history
  • Loading branch information
zaguiini committed Mar 10, 2022
1 parent b261365 commit 9078b01
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
18 changes: 0 additions & 18 deletions lib/compat/wordpress-6.0/class-wp-webfonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* Class WP_Webfonts
*/
class WP_Webfonts {
private static $instance = null;
/**
* An array of registered webfonts.
*
Expand All @@ -33,27 +32,10 @@ class WP_Webfonts {
*/
private $stylesheet_handle = '';

/**
* Return an instance of the WP_Webfonts class, or create one if none exist yet.
*
* @since 6.0.0
*
* @return WP_Webfonts|null
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new WP_Webfonts();
self::$instance->init();
}

return self::$instance;
}

/**
* Init.
*/
public function init() {

// Register default providers.
$this->register_provider( 'local', 'WP_Webfonts_Provider_Local' );

Expand Down
15 changes: 14 additions & 1 deletion lib/compat/wordpress-6.0/webfonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@
* @subpackage Webfonts
*/

class _WP_Webfonts_Singleton {
private static $instance = null;

public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new WP_Webfonts();
self::$instance->init();
}

return self::$instance;
}
}

/**
* Instantiates the webfonts controller, if not already set, and returns it.
*
Expand All @@ -16,7 +29,7 @@
* @return WP_Webfonts Instance of the controller.
*/
function wp_webfonts() {
return WP_Webfonts::get_instance();
return _WP_Webfonts_Singleton::get_instance();
}

/**
Expand Down
22 changes: 21 additions & 1 deletion phpunit/class-wp-webfonts-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
* @covers WP_Webfonts_Test
*/
class WP_Webfonts_Test extends WP_UnitTestCase {
function setUp() {
$singleton = new _WP_Webfonts_Singleton();
$reflection = new ReflectionClass( $singleton );
$instance = $reflection->getProperty( 'instance' );
$instance->setAccessible( true );
$instance->setValue( null, null );
$instance->setAccessible( false );
}

/**
* @covers wp_register_webfonts
Expand Down Expand Up @@ -106,8 +114,20 @@ public function test_validate_font() {
* @covers WP_Webfonts::generate_styles
*/
public function test_generate_styles() {
$font = array(
'provider' => 'local',
'font-family' => 'Source Serif Pro',
'font-style' => 'normal',
'font-weight' => '200 900',
'font-stretch' => 'normal',
'src' => 'https://example.com/assets/fonts/source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2',
'font-display' => 'fallback',
);

wp_register_webfont( $font );

$this->assertEquals(
'@font-face{font-family:"Source Serif Pro";font-style:normal;font-weight:200 900;font-display:fallback;font-stretch:normal;src:local("Source Serif Pro"), url(\'https://example.com/assets/fonts/source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2\') format(\'woff2\');}@font-face{font-family:"Source Serif Pro";font-style:italic;font-weight:200 900;font-display:fallback;font-stretch:normal;src:local("Source Serif Pro"), url(\'https://example.com/assets/fonts/source-serif-pro/SourceSerif4Variable-Italic.ttf.woff2\') format(\'woff2\');}',
'@font-face{font-family:"Source Serif Pro";font-style:normal;font-weight:200 900;font-display:fallback;font-stretch:normal;src:local("Source Serif Pro"), url(\'https://example.com/assets/fonts/source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2\') format(\'woff2\');}',
wp_webfonts()->generate_styles()
);
}
Expand Down

0 comments on commit 9078b01

Please sign in to comment.