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 |
||
| 8 | class Test_Jetpack_Capabilities extends \WP_UnitTestCase { |
||
| 9 | var $builder; |
||
| 10 | var $current_product_slug; |
||
| 11 | |||
| 12 | public function setUp() { |
||
| 16 | |||
| 17 | public function tearDown() { |
||
| 20 | |||
| 21 | View Code Duplication | public function test_get() { |
|
| 22 | |||
| 23 | $capability = $this->builder |
||
| 24 | ->create( 'jetpack.backup.restore' ) |
||
| 25 | ->require_wp_role( 'administrator' ) |
||
| 26 | ->require_wp_capability( 'administrator' ) |
||
| 27 | ->get(); |
||
| 28 | |||
| 29 | // no admin privilege |
||
| 30 | $this->assertFalse( $capability->check()->granted() ); |
||
| 31 | |||
| 32 | $this->setUserRole( 'administrator' ); |
||
| 33 | |||
| 34 | // has admin privilege |
||
| 35 | $this->assertTrue( $capability->check()->granted() ); |
||
| 36 | } |
||
| 37 | |||
| 38 | public function test_capability_has_details() { |
||
| 48 | |||
| 49 | View Code Duplication | public function test_jetpack_plan_rule() { |
|
| 65 | |||
| 66 | public function test_builder_registers_capability() { |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Utility functions |
||
| 77 | */ |
||
| 78 | private function setUserRole( $role ) { |
||
| 82 | |||
| 83 | private function mockJetpackPlan( $product_slug ) { |
||
| 95 | } |
||
| 96 |