Skip to content

Commit

Permalink
[Fonts API] Remove global from wp_print_fonts() (#50225)
Browse files Browse the repository at this point in the history
  • Loading branch information
hellofromtonya authored and bph committed May 9, 2023
1 parent 2ef9c34 commit c922336
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
16 changes: 8 additions & 8 deletions lib/experimental/fonts-api/fonts-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,21 +183,21 @@ function wp_register_font_provider( $name, $classname ) {
* An empty array if none were processed.
*/
function wp_print_fonts( $handles = false ) {
global $wp_fonts;
$wp_fonts = wp_fonts();
$registered = $wp_fonts->get_registered_font_families();

// Nothing to print, as no fonts are registered.
if ( empty( $registered ) ) {
return array();
}

if ( empty( $handles ) ) {
$handles = false;
}

_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );

if ( ! ( $wp_fonts instanceof WP_Fonts ) ) {
if ( ! $handles ) {
return array(); // No need to instantiate if nothing is there.
}
}

return wp_fonts()->do_items( $handles );
return $wp_fonts->do_items( $handles );
}
}

Expand Down
35 changes: 17 additions & 18 deletions phpunit/fonts-api/wpPrintFonts-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,29 @@
*/
class Tests_Fonts_WpPrintFonts extends WP_Fonts_TestCase {

public function test_should_return_empty_array_when_global_not_instance() {
global $wp_fonts;
wp_fonts();
$wp_fonts = null;

public function test_should_return_empty_array_when_no_fonts_registered() {
$this->assertSame( array(), wp_print_fonts() );
$this->assertNotInstanceOf( WP_Webfonts::class, $wp_fonts );
}

/**
* Unit test to mock WP_Webfonts::do_items().
* Unit test which mocks WP_Fonts methods.
*
* @dataProvider data_mocked_handles
*
* @param string|string[]|false $handles Handles to test.
* @param array|string[] $expected Expected array of processed handles.
* @param string|string[] $handles Handles to test.
*/
public function test_should_return_mocked_handles( $handles, $expected ) {
$mock = $this->set_up_mock( 'do_items' );
public function test_should_return_mocked_handles( $handles ) {
$mock = $this->set_up_mock( array( 'get_registered_font_families', 'do_items' ) );
$mock->expects( $this->once() )
->method( 'get_registered_font_families' )
->will( $this->returnValue( $handles ) );

$mock->expects( $this->once() )
->method( 'do_items' )
->with(
$this->identicalTo( $handles )
)
->will( $this->returnValue( $expected ) );
->will( $this->returnValue( $handles ) );

wp_print_fonts( $handles );
}
Expand All @@ -51,13 +49,14 @@ public function test_should_return_mocked_handles( $handles, $expected ) {
*/
public function data_mocked_handles() {
return array(
'no handles' => array(
'handles' => false,
'expected' => array(),
'font family' => array(
array( 'my-custom-font' ),
),
'font family handles' => array(
'handles' => array( 'my-custom-font' ),
'expected' => array( 'my-custom-font' ),
'multiple font families' => array(
array(
'font1',
'font2',
),
),
);
}
Expand Down

0 comments on commit c922336

Please sign in to comment.