Completed
Push — update/package-logo-add-gray ( d6f2c9...023660 )
by
unknown
66:10 queued 58:37
created

Test_Logo   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 79
Duplicated Lines 32.91 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A tearDown() 0 7 1
A test_constructor_default_logo() 0 4 1
A test_constructor_custom_logo() 0 5 1
A test_render_img_tag() 0 16 1
A mock_plugins_url() 15 15 1
A mock_with_identity() 11 11 1
A identity() 0 3 1

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
namespace Automattic\Jetpack\Assets;
3
4
use PHPUnit\Framework\TestCase;
5
use phpmock\MockBuilder;
6
7
class Test_Logo extends TestCase {
8
	protected $logo_url = 'https://yourjetpack.blog/wp-content/plugins/jetpack/packages/logo/assets/logo.svg';
9
10
	public function setUp() {
11
		parent::setUp();
12
13
		$this->esc_url_mock = $this->mock_with_identity( 'esc_url' );
14
		$this->esc_attr_mock = $this->mock_with_identity( 'esc_attr' );
15
		$this->plugins_url_mock = $this->mock_plugins_url();
16
	}
17
18
	public function tearDown() {
19
		$this->esc_url_mock->disable();
20
		$this->esc_attr_mock->disable();
21
		$this->plugins_url_mock->disable();
22
23
		parent::tearDown();
24
	}
25
26
	public function test_constructor_default_logo() {
27
		$logo = new Logo();
28
		$this->assertContains( $this->logo_url, $logo->render() );
29
	}
30
31
	public function test_constructor_custom_logo() {
32
		$example_logo = 'https://wordpress.com/logo.png';
33
		$logo = new Logo( $example_logo );
0 ignored issues
show
Unused Code introduced by
The call to Logo::__construct() has too many arguments starting with $example_logo.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
34
		$this->assertContains( $example_logo, $logo->render() );
35
	}
36
37
	public function test_render_img_tag() {
38
		$logo = new Logo();
39
		$output = $logo->render();
40
41
		// Contains only a valid img tag.
42
		$this->assertRegExp( '/^<img.*\/>$/', $output );
43
44
		// Contains the expected src attribute.
45
		$this->assertRegExp( '/.+src="' . preg_quote( $this->logo_url, '/' ) . '".+/', $output );
46
47
		// Contains the expected class attribute.
48
		$this->assertRegExp( '/.+class="jetpack-logo".+/', $output );
49
50
		// Contains an alt attribute.
51
		$this->assertRegExp( '/.+alt="[^"]+".+/', $output );
52
	}
53
54 View Code Duplication
	protected function mock_plugins_url() {
55
		$builder = new MockBuilder();
56
		$builder->setNamespace( __NAMESPACE__ )
57
			->setName( 'plugins_url' )
58
			->setFunction(
59
				function () {
60
					return $this->logo_url;
61
				}
62
			);
63
64
		$mock = $builder->build();
65
		$mock->enable();
66
67
		return $mock;
68
	}
69
70 View Code Duplication
	protected function mock_with_identity( $function_name ) {
71
		$builder = new MockBuilder();
72
		$builder->setNamespace( __NAMESPACE__ )
73
			->setName( $function_name )
74
			->setFunction( array( $this, 'identity' ) );
75
76
		$mock = $builder->build();
77
		$mock->enable();
78
79
		return $mock;
80
	}
81
82
	public function identity( $value ) {
83
		return $value;
84
	}
85
}
86