Completed
Push — add/refactor-nav-unification ( ba4aaf...622254 )
by
unknown
140:28 queued 130:34
created

Test_Base_Admin_Menu   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 104
Duplicated Lines 9.62 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 10
loc 104
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A wpSetUpBeforeClass() 0 5 1
A setUp() 0 8 1
A test_get_instance() 10 10 1
A test_add_admin_menu_separator() 0 19 1
A get_concrete_menu_admin() 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
/**
3
 * Tests for Base_Admin_Menu class.
4
 *
5
 * @package automattic/jetpack
6
 */
7
8
require_jetpack_file( 'modules/masterbar/admin-menu/class-base-admin-menu.php' );
9
10
use \Automattic\Jetpack\Dashboard_Customizations\Base_Admin_Menu;
11
12
/**
13
 * Class Test_Base_Admin_Menu
14
 *
15
 * @coversDefaultClass Automattic\Jetpack\Dashboard_Customizations\Base_Admin_Menu
16
 */
17
class Test_Base_Admin_Menu extends WP_UnitTestCase {
18
19
	/**
20
	 * Admin menu instance.
21
	 *
22
	 * @var Base_Admin_Menu
23
	 */
24
	public static $admin_menu;
25
26
	/**
27
	 * Mock user ID.
28
	 *
29
	 * @var int
30
	 */
31
	private static $user_id = 0;
32
33
	/**
34
	 * Menu data fixture.
35
	 *
36
	 * @var array
37
	 */
38
	public static $menu_data;
39
40
	/**
41
	 * Submenu data fixture.
42
	 *
43
	 * @var array
44
	 */
45
	public static $submenu_data;
46
47
	/**
48
	 * Create shared fixtures.
49
	 *
50
	 * @param WP_UnitTest_Factory $factory Fixture factory.
51
	 */
52
	public static function wpSetUpBeforeClass( $factory ) {
53
		static::$user_id      = $factory->user->create( array( 'role' => 'administrator' ) );
0 ignored issues
show
Bug introduced by
Since $user_id is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $user_id to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
54
		static::$menu_data    = get_menu_fixture();
55
		static::$submenu_data = get_submenu_fixture();
56
	}
57
58
	/**
59
	 * Set up data.
60
	 */
61
	public function setUp() {
62
		parent::setUp();
63
64
		// Initialize in setUp so it registers hooks for every test.
65
		static::$admin_menu = $this->get_concrete_menu_admin()::get_instance();
66
67
		wp_set_current_user( static::$user_id );
0 ignored issues
show
Bug introduced by
Since $user_id is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $user_id to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
68
	}
69
70
	/**
71
	 * Test get_instance.
72
	 *
73
	 * @covers ::get_instance
74
	 * @covers ::__construct
75
	 */
76 View Code Duplication
	public function test_get_instance() {
77
78
		$instance = $this->get_concrete_menu_admin()::get_instance();
79
80
		$this->assertInstanceOf( Base_Admin_Menu::class, $instance );
81
		$this->assertSame( $instance, static::$admin_menu );
82
83
		$this->assertSame( 99999, has_action( 'admin_menu', array( $instance, 'reregister_menu_items' ) ) );
84
		$this->assertSame( 10, has_action( 'admin_enqueue_scripts', array( $instance, 'enqueue_scripts' ) ) );
85
	}
86
87
	/**
88
	 * Tests add_admin_menu_separator
89
	 *
90
	 * @covers ::add_admin_menu_separator
91
	 */
92
	public function test_add_admin_menu_separator() {
93
		global $menu;
94
95
		// Start with a clean slate.
96
		$temp_menu = $menu;
97
		$menu      = array();
98
99
		static::$admin_menu->add_admin_menu_separator( 15 );
100
		static::$admin_menu->add_admin_menu_separator( 10, 'manage_options' );
101
102
		$this->assertSame( array( 10, 15 ), array_keys( $menu ), 'Menu should be ordered by position parameter.' );
103
		$this->assertSame( 'manage_options', $menu[10][1] );
104
		$this->assertContains( 'separator-custom-', $menu[10][2] );
105
		$this->assertSame( 'read', $menu[15][1] );
106
		$this->assertContains( 'separator-custom-', $menu[15][2] );
107
108
		// Restore filtered $menu.
109
		$menu = $temp_menu;
110
	}
111
112
	/**
113
	 * Get an object of Base_Admin_Menu
114
	 *
115
	 * @return Base_Admin_Menu
116
	 */
117
	private function get_concrete_menu_admin() {
118
		return $this->getMockBuilder( Base_Admin_Menu::class )->disableOriginalConstructor()->getMockForAbstractClass();
119
	}
120
}
121