Completed
Push — add/packages-readme ( 6aaf4b...09fa4a )
by
unknown
07:53
created

Test_Logo::mock_plugins_url()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 15
loc 15
rs 9.7666
c 0
b 0
f 0
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