Skip to content

Commit

Permalink
Update test for get_rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Swanand01 committed May 10, 2024
1 parent e85b4dc commit 8ac8d38
Showing 1 changed file with 13 additions and 103 deletions.
116 changes: 13 additions & 103 deletions tests/phpunit/integration/tests/Speculation_Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,23 @@

namespace Google\Web_Stories\Tests\Integration;

use Google\Web_Stories\Admin\Dashboard;
use Google\Web_Stories\Context;
use Google\Web_Stories\Story_Post_Type;
use PHPUnit\Framework\MockObject\MockObject;

/**
* @coversDefaultClass \Google\Web_Stories\Speculation_Rules
*/
class Speculation_Rules extends DependencyInjectedTestCase {
/**
* @var Context & MockObject
*/
private $context;

private Story_Post_Type $story_post_type;

/**
* @var Dashboard & MockObject
*/
private $dashboard;

protected \Google\Web_Stories\Speculation_Rules $instance;

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

$this->dashboard = $this->createMock( Dashboard::class );
$this->story_post_type = $this->injector->make( Story_Post_Type::class );
$this->context = $this->createMock( Context::class );

$this->instance = new \Google\Web_Stories\Speculation_Rules(
$this->context,
$this->story_post_type,
$this->dashboard,
);
}

Expand All @@ -62,111 +45,38 @@ public function set_up(): void {
*/
public function test_register(): void {
$this->instance->register();
$this->assertSame( 10, has_action( 'admin_enqueue_scripts', [ $this->instance, 'load_rules' ] ) );
$this->assertSame( 10, has_action( 'admin_footer', [ $this->instance, 'print_rules' ] ) );
$this->assertSame( 10, has_action( 'wp_footer', [ $this->instance, 'print_rules' ] ) );
}

/**
* @covers ::load_rules
* @covers ::get_rules
*/
public function test_load_rules_dashboard(): void {
$this->dashboard->method( 'get_hook_suffix' )->willReturn( 'web-story_page_stories-dashboard' );

public function test_get_rules(): void {
$prerendering_class = $this->getMockBuilder( \Google\Web_Stories\Speculation_Rules::class )
->onlyMethods( [ 'get_rules', 'print_rules' ] )
->setConstructorArgs( [ $this->context, $this->story_post_type, $this->dashboard ] )
->onlyMethods( [ 'get_rules' ] )
->setConstructorArgs( [ $this->story_post_type ] )
->getMock();

$prerendering_class->expects( $this->once() )
->method( 'get_rules' )
->with( 'dashboard' );

$prerendering_class->expects( $this->once() )
->method( 'print_rules' );

$prerendering_class->load_rules( 'web-story_page_stories-dashboard' );
}

/**
* @covers ::load_rules
*/
public function test_load_rules_all_stories(): void {
$this->context->method( 'get_screen_post_type' )->willReturn( 'web-story' );
$this->context->method( 'get_screen_base' )->willReturn( 'edit' );

$prerendering_class = $this->getMockBuilder( \Google\Web_Stories\Speculation_Rules::class )
->onlyMethods( [ 'get_rules', 'print_rules' ] )
->setConstructorArgs( [ $this->context, $this->story_post_type, $this->dashboard ] )
->getMock();

$prerendering_class->expects( $this->once() )
->method( 'get_rules' )
->with( 'all_stories' );

$prerendering_class->expects( $this->once() )
->method( 'print_rules' );

$prerendering_class->load_rules( 'edit' );
}

/**
* @covers ::get_rules
*/
public function test_get_rules_for_dashboard(): void {
$new_story_url = sprintf(
'post-new.php?post_type=%s',
$this->story_post_type->get_slug()
);
$edit_story_url = 'post.php?post=*&action=edit';

$expected = [
$expected_rules = [
'prerender' => [
[
'source' => 'document',
'where' => [
'and' => [
[
'href_matches' => [ $edit_story_url, $new_story_url ],
],
[ 'href_matches' => [ Story_Post_Type::REWRITE_SLUG, sprintf( '/%s/*', $this->story_post_type::REWRITE_SLUG ) ] ],
],
],
'eagerness' => 'moderate',
],
],
];

$this->assertEquals( $expected, $this->instance->get_rules( 'dashboard' ) );
}

/**
* @covers ::get_rules
*/
public function test_get_rules_for_all_stories(): void {
$new_story_url = sprintf(
'post-new.php?post_type=%s',
$this->story_post_type->get_slug()
);
$edit_story_url = 'post.php?post=*&action=edit';
$view_story_url = sprintf(
'/%s/*',
$this->story_post_type::REWRITE_SLUG
);

$expected = [
'prerender' => [
[
'source' => 'document',
'where' => [
'and' => [
[
'href_matches' => [ $edit_story_url, $new_story_url, $view_story_url ],
],
],
],
'eagerness' => 'moderate',
],
],
];
$prerendering_class->expects( $this->once() )
->method( 'get_rules' )
->willReturn( $expected_rules );

$this->assertEquals( $expected, $this->instance->get_rules( 'all_stories' ) );
$actual_rules = $prerendering_class->get_rules();
$this->assertEquals( $expected_rules, $actual_rules );
}
}

0 comments on commit 8ac8d38

Please sign in to comment.