|
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 ); |
|
|
|
|
|
|
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
|
|
|
|
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
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.