Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Miraeld committed Nov 7, 2024
1 parent 359f175 commit 6f044c0
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

return [
'testShouldReturnTrueWhenFeatureEnabled' => [
'config' => [
'local_google_fonts' => false,
'rocket_self_host_fonts' => true,
],
'expected' => true,
],
'testShouldReturnFalseWhenFeatureDisabled' => [
'config' => [
'local_google_fonts' => true,
'rocket_self_host_fonts' => false,
],
'expected' => false,
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

return [
'testShouldReturnLocalUrl' => [
'config' => [
'font_url' => 'https://fonts.googleapis.com/css2?family=Roboto',
],
'expected' => '/wp-content/cache/wp-rocket/fonts/google-fonts/2/b7488900b01c2ffa45c4faf5876bd050.css',
],
'testShouldReturnLocalUrlWithVersion' => [
'config' => [
'font_url' => 'https://fonts.googleapis.com/css2?family=Roboto&ver=1.0',
],
'expected' => '/wp-content/cache/wp-rocket/fonts/google-fonts/2/bd7d7cead547593f5d4c8a439f32e209.css',
],
'testShouldReturnLocalUrlWithSubset' => [
'config' => [
'font_url' => 'https://fonts.googleapis.com/css2?family=Roboto:300,400,500&subset=latin,latin-ext',
],
'expected' => '/wp-content/cache/wp-rocket/fonts/google-fonts/2/b6eed853c9ca4324a5fd627ea3465c48.css',
],
'testShouldReturnLocalUrlWithMultipleFonts' => [
'config' => [
'font_url' => 'https://fonts.googleapis.com/css2?family=Roboto|Open+Sans',
],
'expected' => '/wp-content/cache/wp-rocket/fonts/google-fonts/2/3d7fe3a1467a88318d3fb6e4ced0b127.css',
],
'testShouldReturnLocalUrlWithMultipleFontsAndVersion' => [
'config' => [
'font_url' => 'https://fonts.googleapis.com/css2?family=Roboto|Open+Sans&ver=1.0',
],
'expected' => '/wp-content/cache/wp-rocket/fonts/google-fonts/2/3fbdd05b1bf26933207aeb7d3d910d30.css',
],
];
26 changes: 26 additions & 0 deletions tests/Unit/inc/Engine/Media/Fonts/Context/Context/isAllowed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace WP_Rocket\Tests\Unit\inc\Engine\Media\Fonts\Context\Context;

use WP_Rocket\Tests\Unit\TestCase;
use WP_Rocket\Engine\Media\Fonts\Context\Context;
use Brain\Monkey\Functions;

class Test_IsAllowed extends TestCase
{

/**
* @dataProvider configTestData
*/
public function testShouldDoExpected($config, $expected)
{
$context = new Context();

Functions\expect('get_option')
->once()
->with('local_google_fonts')
->andReturn($config['local_google_fonts']);

$this->assertSame($expected, $context->is_allowed($config));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace WP_Rocket\tests\Unit\inc\Engine\Media\Fonts\Factory\Fonts\GoogleFontV2;

use WP_Rocket\Engine\Media\Fonts\Factory\Fonts\GoogleFontV2;
use WP_Rocket\Tests\Unit\TestCase;
use Brain\Monkey\Functions;

class Test_GetLocalUrl extends TestCase
{

/**
* @dataProvider configTestData
*/
public function testShouldDoExpected($config, $expected)
{
$googleFontV1 = new GoogleFontV2($config['font_url']);

Functions\expect('WP_Rocket\Engine\Media\Fonts\Factory\Fonts\rocket_get_constant')
->once()
->with('WP_ROCKET_CACHE_PATH')
->andReturn('/wp-content/cache/wp-rocket/');

$result = $googleFontV1->get_local_url();
$this->assertEquals($expected, $result);
}
}

0 comments on commit 6f044c0

Please sign in to comment.