Completed
Push — add/php-test-for-business-hour... ( 687d29...87d7ff )
by
unknown
08:04
created

Business_Hours_Block_Test   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 98
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A set_up() 0 4 1
A tear_down() 0 5 2
A test_block_can_be_registered() 0 4 1
A test_server_side_rendering_based_on_serialized_fixtures() 0 52 4
1
<?php
2
/**
3
 * Business Hours Block tests
4
 *
5
 * @package automattic/jetpack
6
 */
7
8
/**
9
 * Include the file containing the block's registration and render functions.
10
 */
11
require_once JETPACK__PLUGIN_DIR . 'extensions/blocks/business-hours/business-hours.php';
12
13
const BLOCK_NAME = 'jetpack/business-hours';
14
15
/**
16
 * Business Hours Block tests.
17
 *
18
 * These tests primarily check that server rendered markup is not changing unexpectedly
19
 * when serialized fixtures are updated via the block's JS-based save behaviour.
20
 *
21
 * The goal is to catch when changes to serialized markup affects server rendering of the block.
22
 */
23
class Business_Hours_Block_Test extends \WP_UnitTestCase {
24
	/**
25
	 * A variable to track whether or not the block was already registered before the test was run.
26
	 *
27
	 * @access private
28
	 *
29
	 * @var boolean
30
	 */
31
	private $was_registered = false;
32
33
	/**
34
	 * Setup and ensure the block is registered before running the tests.
35
	 *
36
	 * @before
37
	 */
38
	public function set_up() {
39
		$this->was_registered = \Automattic\Jetpack\Blocks::is_registered( BLOCK_NAME );
40
		\Automattic\Jetpack\Extensions\Business_Hours\register_block();
41
	}
42
43
	/**
44
	 * Teardown and unregister the block if it wasn't registered before running these tests.
45
	 *
46
	 * @after
47
	 */
48
	public function tear_down() {
49
		if ( ! $this->was_registered ) {
50
			unregister_block_type( 'jetpack/business-hours' );
51
		}
52
	}
53
54
	/**
55
	 * Test that the block is registered, which means that it can be registered.
56
	 */
57
	public function test_block_can_be_registered() {
58
		$is_registered = \Automattic\Jetpack\Blocks::is_registered( BLOCK_NAME );
59
		$this->assertTrue( $is_registered );
60
	}
61
62
	/**
63
	 * This test iterates over the block's serialized fixtures, and tests that the generated
64
	 * markup matches a fixture for the server rendered markup for the block.
65
	 *
66
	 * If no server rendered fixture can be found, then one is created.
67
	 */
68
	public function test_server_side_rendering_based_on_serialized_fixtures() {
69
		// phpcs:disable WordPress.WP.AlternativeFunctions
70
		$fixtures_path = 'extensions/blocks/business-hours/test/fixtures/';
71
		$file_pattern  = '*.serialized.html';
72
		$files         = glob( JETPACK__PLUGIN_DIR . $fixtures_path . $file_pattern );
73
74
		$fail_messages = array();
75
76
		foreach ( $files as $file ) {
77
			$block_markup = trim( file_get_contents( $file ) );
78
79
			$parsed_blocks   = parse_blocks( $block_markup );
80
			$rendered_output = render_block( $parsed_blocks[0] );
81
82
			$target_markup_filename = str_replace( '.serialized.html', '.server-rendered.html', $file );
83
84
			// Create a server rendered fixture if one does not exist.
85
			if ( ! file_exists( $target_markup_filename ) ) {
86
				file_put_contents( $target_markup_filename, $rendered_output );
87
				$fail_messages[] =
88
					sprintf(
89
						"No server rendered fixture could be found for the %s block's %s fixture\n" .
90
						"A fixture file has been created in: %s\n",
91
						BLOCK_NAME,
92
						basename( $file ),
93
						$fixtures_path . basename( $target_markup_filename )
94
					);
95
			}
96
97
			$server_rendered_fixture = file_get_contents( $target_markup_filename );
98
			$this->assertEquals(
99
				$rendered_output,
100
				trim( $server_rendered_fixture ),
101
				sprintf(
102
					'The results of render_block for %s called with serialized markup from %s do not match ' .
103
					"the server-rendered fixture: %s\n",
104
					BLOCK_NAME,
105
					basename( $file ),
106
					basename( $target_markup_filename )
107
				)
108
			);
109
		}
110
111
		// Fail the test if any fixtures were missing, and report that fixtures have been generated.
112
		if ( ! empty( $fail_messages ) ) {
113
			$this->fail(
114
				implode( "\n", $fail_messages ) .
115
				"\nTry running this test again. Be sure to commit generated fixture files with any code changes."
116
			);
117
		}
118
		// phpcs:enable WordPress.WP.AlternativeFunctions
119
	}
120
}
121