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 |
||
| 9 | class Test_Terms_Of_Service extends TestCase { |
||
| 10 | |||
| 11 | /** |
||
| 12 | * Test setup. |
||
| 13 | */ |
||
| 14 | public function setUp() { |
||
| 15 | $this->terms_of_service = $this->createPartialMock( __NAMESPACE__ .'\\Terms_Of_Service', |
||
| 16 | array( 'get_raw_has_agreed', 'is_development_mode', 'is_active', 'set_agree', 'set_reject' ) |
||
| 17 | ) |
||
| 18 | } |
||
|
|
|||
| 19 | |||
| 20 | /** |
||
| 21 | * Test teardown. |
||
| 22 | */ |
||
| 23 | public function tearDown() { |
||
| 24 | Mock::disableAll(); |
||
| 25 | } |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @covers Automattic\Jetpack\Terms_Of_Service->agree |
||
| 29 | */ |
||
| 30 | public function test_agree() { |
||
| 31 | $this->mock_function( 'do_action', null, 'jetpack_agreed_to_terms_of_service' ); |
||
| 32 | $this->terms_of_service->expects( $this->once() )->method( 'set_agree' )->willReturn( null ); |
||
| 33 | |||
| 34 | $this->terms_of_service->agree(); |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @covers Automattic\Jetpack\Terms_Of_Service->revoke |
||
| 39 | */ |
||
| 40 | public function test_revoke() { |
||
| 41 | $this->mock_function( 'do_action', null, 'jetpack_reject_to_terms_of_service' ); |
||
| 42 | $this->terms_of_service->expects( $this->once() )->method( 'set_reject' )->willReturn( null ); |
||
| 43 | |||
| 44 | $this->terms_of_service->reject(); |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @covers Automattic\Jetpack\Terms_Of_Service->has_agreed |
||
| 49 | */ |
||
| 50 | public function test_has_agreed_before_the_site_agrees() { |
||
| 51 | $this->terms_of_service->expects( $this->once() )->method( 'get_raw_has_agreed' )->willReturn( false ); |
||
| 52 | $this->assertFalse( $this->terms_of_service->has_agreed() ); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @covers Automattic\Jetpack\Terms_Of_Service->has_agreed |
||
| 57 | */ |
||
| 58 | public function test_has_agreed_is_development_mode() { |
||
| 59 | $this->terms_of_service->expects( $this->once() )->method( 'get_raw_has_agreed' )->willReturn( true ); |
||
| 60 | // is_development_mode |
||
| 61 | $this->terms_of_service->expects( $this->once() )->method( 'is_development_mode' )->willReturn( true ); |
||
| 62 | $this->assertFalse( $this->terms_of_service->has_agreed() ); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @covers Automattic\Jetpack\Terms_Of_Service->has_agreed |
||
| 67 | */ |
||
| 68 | public function test_has_agreed_is_active_mode() { |
||
| 69 | $this->terms_of_service->expects( $this->once() )->method( 'get_raw_has_agreed' )->willReturn( true ); |
||
| 70 | // Not in dev mode... |
||
| 71 | $this->terms_of_service->expects( $this->once() )->method( 'is_development_mode' )->willReturn( false ); |
||
| 72 | |||
| 73 | // Jetpack is active |
||
| 74 | $this->terms_of_service->expects( $this->once() )->method( 'is_active' )->willReturn( true ); |
||
| 75 | |||
| 76 | $this->assertTrue( $this->terms_of_service->has_agreed() ); |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @covers Automattic\Jetpack\Terms_Of_Service->has_agreed |
||
| 81 | */ |
||
| 82 | public function test_has_agreed_is_not_active_mode() { |
||
| 83 | $this->terms_of_service->expects( $this->once() )->method( 'get_raw_has_agreed' )->willReturn( true ); |
||
| 84 | // not in dev mode... |
||
| 85 | $this->terms_of_service->expects( $this->once() )->method( 'is_development_mode' )->willReturn( false ); |
||
| 86 | |||
| 87 | // Jetpack is not active |
||
| 88 | $this->terms_of_service->expects( $this->once() )->method( 'is_active' )->willReturn( false ); |
||
| 89 | |||
| 90 | $this->assertFalse( $this->terms_of_service->has_agreed() ); |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Mock a global function and make it return a certain value. |
||
| 95 | * |
||
| 96 | * @param string $function_name Name of the function. |
||
| 97 | * @param mixed $return_value Return value of the function. |
||
| 98 | * @return phpmock\Mock The mock object. |
||
| 99 | */ |
||
| 100 | protected function mock_function( $function_name, $return_value = null, $called_with = null ) { |
||
| 101 | $builder = new MockBuilder(); |
||
| 102 | $builder->setNamespace( __NAMESPACE__ ) |
||
| 103 | ->setName( $function_name ) |
||
| 104 | ->setFunction( function( $VALUE ) use ( &$return_value, $called_with ) { |
||
| 105 | if ( $called_with ) { |
||
| 106 | $this->assertEquals( $VALUE, $called_with ); |
||
| 107 | } |
||
| 108 | return $return_value; |
||
| 109 | } ); |
||
| 110 | return $builder->build()->enable(); |
||
| 113 |