Skip to content

Commit

Permalink
wp_get_webfont_providers() plus tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hellofromtonya committed Aug 19, 2022
1 parent 0740239 commit 549908c
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 12 deletions.
22 changes: 11 additions & 11 deletions lib/experimental/class-wp-webfonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ public function __construct() {
do_action_ref_array( 'wp_default_webfonts', array( &$this ) );
}

/**
* Get the list of registered providers.
*
* @since X.X.X
*
* @return WP_Webfonts_Provider[] All registered providers, each keyed by their unique ID.
*/
public function get_providers() {
return $this->providers;
}

/**
* Get the list of all registered font families and their variations.
*
Expand Down Expand Up @@ -355,17 +366,6 @@ public function register_provider( $provider, $class ) {
return true;
}

/**
* Get the list of providers.
*
* @since 6.0.0
*
* @return WP_Webfonts_Provider[] All registered providers, each keyed by their unique ID.
*/
public function get_providers() {
return $this->providers;
}

/**
* Retrieves a list of enqueued web font variations for a provider.
*
Expand Down
2 changes: 1 addition & 1 deletion lib/experimental/webfonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ function wp_register_webfont_provider( $name, $classname ) {
* process its specific font service (i.e. local or remote)
* and how to generate the `@font-face` styles for its service.
*
* @since 6.0.0
* @since X.X.X
*
* @return WP_Webfonts_Provider[] All registered providers, each keyed by their unique ID.
*/
Expand Down
42 changes: 42 additions & 0 deletions phpunit/webfonts/wp-webfonts-tests-dataset.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,48 @@ public function data_font_family_handles() {
);
}

/**
* Data provider.
*
* @return array
*/
public function data_get_providers() {
$local = array(
'local' => array(
'class' => WP_Webfonts_Provider_Local::class,
'fonts' => array(),
),
);
$mock = array(
'mock' => array(
'class' => Mock_Provider::class,
'fonts' => array(),
),
);

return array(
'local provider' => array(
'providers' => array(
'local' => WP_Webfonts_Provider_Local::class,
),
'expected' => $local,
),
'mock provider' => array(
'providers' => array(
'mock' => Mock_Provider::class,
),
'expected' => $mock,
),
'local and mock providers' => array(
'providers' => array(
'local' => WP_Webfonts_Provider_Local::class,
'mock' => Mock_Provider::class,
),
'expected' => array_merge( $local, $mock ),
),
);
}

/**
* Data provider.
*
Expand Down
72 changes: 72 additions & 0 deletions phpunit/webfonts/wpGetWebfontProviders-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Unit and integration tests for wp_get_webfont_providers().
*
* @package WordPress
* @subpackage Webfonts
*/

require_once __DIR__ . '/wp-webfonts-testcase.php';
require_once __DIR__ . '/../fixtures/mock-provider.php';

/**
* @group webfonts
* @covers ::wp_get_webfont_providers
*/
class Tests_Webfonts_WpGetWebfontProviders extends WP_Webfonts_TestCase {
private $providers_property;

public function set_up() {
parent::set_up();
$this->providers_property = new ReflectionProperty( WP_Webfonts::class, 'providers' );
$this->providers_property->setAccessible( true );
}

/**
* Unit test.
*
* @dataProvider data_get_providers
*
* @param array $providers Not used.
* @param array $expected Expected results.
*/
public function test_should_return_mocked_providers( array $providers, array $expected ) {
$mock = $this->set_up_mock( 'get_providers' );
$mock->expects( $this->once() )
->method( 'get_providers' )
->will( $this->returnValue( $expected ) );

$this->assertSame( $expected, wp_get_webfont_providers() );
}

/**
* Integration test that sets the `WP_Webfonts::providers` property.
*
* @dataProvider data_get_providers
*
* @param array $providers Array of providers to test.
* @param array $expected Expected results.
*/
public function test_should_return_providers( array $providers, array $expected ) {
$this->setup_providers( $providers );
$this->assertSame( $expected, wp_get_webfont_providers() );
}

/**
* Sets up the given providers and stores them in the `WP_Webfonts::providers` property.
*
* @param array $providers Array of providers to set up.
*/
private function setup_providers( array $providers ) {
$data = array();

foreach ( $providers as $provider_id => $class ) {
$data[ $provider_id ] = array(
'class' => $class,
'fonts' => array(),
);
}

$this->providers_property->setValue( wp_webfonts(), $data );
}
}
62 changes: 62 additions & 0 deletions phpunit/webfonts/wpWebfonts/getProviders-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* WP_Webfonts::get_providers() tests.
*
* @package WordPress
* @subpackage Webfonts
*/

require_once __DIR__ . '/../wp-webfonts-testcase.php';
require_once __DIR__ . '/../../fixtures/mock-provider.php';

/**
* @group webfonts
* @covers WP_Webfonts::get_providers
*/
class Tests_Webfonts_WpWebfonts_GetProviders extends WP_Webfonts_TestCase {
private $wp_webfonts;
private $providers_property;

public function set_up() {
parent::set_up();
$this->wp_webfonts = new WP_Webfonts();

$this->providers_property = new ReflectionProperty( WP_Webfonts::class, 'providers' );
$this->providers_property->setAccessible( true );
}

public function test_should_be_empty() {
$actual = $this->wp_webfonts->get_providers();
$this->assertIsArray( $actual, 'Should return an empty array' );
$this->assertEmpty( $actual, 'Should return an empty array when no providers are registered' );
}

/**
* @dataProvider data_get_providers
*
* @param array $providers Array of providers to test.
* @param array $expected Expected results.
*/
public function test_get_providers( array $providers, array $expected ) {
$this->setup_providers( $providers );
$this->assertSame( $expected, $this->wp_webfonts->get_providers() );
}

/**
* Sets up the given providers and stores them in the `WP_Webfonts::providers` property.
*
* @param array $providers Array of providers to set up.
*/
private function setup_providers( array $providers ) {
$data = array();

foreach ( $providers as $provider_id => $class ) {
$data[ $provider_id ] = array(
'class' => $class,
'fonts' => array(),
);
}

$this->providers_property->setValue( $this->wp_webfonts, $data );
}
}

0 comments on commit 549908c

Please sign in to comment.