Completed
Push — update/add-csstidy-config-sett... ( 5d5261...a816cd )
by
unknown
548:09 queued 537:58
created

Jetpack_Block_Fixture_TestCase   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 68
Duplicated Lines 16.18 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 11
loc 68
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B generate_server_side_rendering_based_on_serialized_fixtures() 11 56 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Jetpack Block Fixture Test Case.
4
 *
5
 * @package automattic/jetpack
6
 */
7
8
/**
9
 * Jetpack Block Fixture Test Case with method for testing against the block's
10
 * serialized HTML test fixtures, generated by the block's `save` method in JavaScript.
11
 */
12
abstract class Jetpack_Block_Fixture_TestCase extends WP_UnitTestCase {
13
	/**
14
	 * This test iterates over the block's serialized fixtures, and tests that the generated
15
	 * markup matches a fixture for the server rendered markup for the block.
16
	 *
17
	 * If no server rendered fixture can be found, then one is created.
18
	 *
19
	 * @param string $block_name The name used to register the block (e.g. `jetpack/repeat-visitor`).
20
	 * @param string $block_slug The slug of the directory containing the code for the block (e.g. `repeat-visitor`).
21
	 * @param string $target_extension The extension for the target markup of the rendered block.
22
	 */
23
	public function generate_server_side_rendering_based_on_serialized_fixtures(
24
		$block_name,
25
		$block_slug,
26
		$target_extension = '.server-rendered.html'
27
	) {
28
		// phpcs:disable WordPress.WP.AlternativeFunctions
29
		$fixtures_path = "extensions/blocks/{$block_slug}/test/fixtures/";
30
		$file_pattern  = '*.serialized.html';
31
		$files         = glob( JETPACK__PLUGIN_DIR . $fixtures_path . $file_pattern );
32
33
		$fail_messages = array();
34
35
		foreach ( $files as $file ) {
36
			$block_markup = trim( file_get_contents( $file ) );
37
38
			$parsed_blocks   = parse_blocks( $block_markup );
39
			$rendered_output = trim( render_block( $parsed_blocks[0] ) );
40
41
			$target_markup_filename = str_replace( '.serialized.html', $target_extension, $file );
42
43
			// Create a server rendered fixture if one does not exist.
44 View Code Duplication
			if ( ! file_exists( $target_markup_filename ) ) {
45
				file_put_contents( $target_markup_filename, $rendered_output );
46
				$fail_messages[] =
47
					sprintf(
48
						"No server rendered fixture could be found for the %s block's %s fixture\n" .
49
						"A fixture file has been created in: %s\n",
50
						$block_name,
51
						basename( $file ),
52
						$fixtures_path . basename( $target_markup_filename )
53
					);
54
			}
55
56
			$server_rendered_fixture = file_get_contents( $target_markup_filename );
57
			$this->assertEquals(
58
				$rendered_output,
59
				trim( $server_rendered_fixture ),
60
				sprintf(
61
					'The results of render_block for %s called with serialized markup from %s do not match ' .
62
					"the server-rendered fixture: %s\n",
63
					$block_name,
64
					basename( $file ),
65
					basename( $target_markup_filename )
66
				)
67
			);
68
		}
69
70
		// Fail the test if any fixtures were missing, and report that fixtures have been generated.
71
		if ( ! empty( $fail_messages ) ) {
72
			$this->fail(
73
				implode( "\n", $fail_messages ) .
74
				"\nTry running this test again. Be sure to commit generated fixture files with any code changes."
75
			);
76
		}
77
		// phpcs:enable WordPress.WP.AlternativeFunctions
78
	}
79
}
80