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 |
||
| 16 | class AutomatedEditsTest extends WebTestCase |
||
| 17 | { |
||
| 18 | /** @var Container The DI container. */ |
||
| 19 | protected $container; |
||
| 20 | |||
| 21 | /** @var AutomatedEditsHelper The API Helper object to test. */ |
||
| 22 | protected $aeh; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Set up the AutomatedEditsHelper object for testing. |
||
| 26 | */ |
||
| 27 | public function setUp() |
||
| 28 | { |
||
| 29 | $client = static::createClient(); |
||
| 30 | $this->container = $client->getContainer(); |
||
| 31 | $this->aeh = new AutomatedEditsHelper($this->container); |
||
| 32 | $this->cache = $this->container->get('cache.app'); |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Test that the merge of per-wiki config and global config works |
||
| 37 | */ |
||
| 38 | public function testTools() |
||
| 39 | { |
||
| 40 | $tools = $this->aeh->getTools('en.wikipedia.org'); |
||
| 41 | |||
| 42 | $this->assertArraySubset( |
||
| 43 | [ |
||
| 44 | 'regex' => '\(\[\[WP:HG', |
||
| 45 | 'tag' => 'huggle', |
||
| 46 | 'link' => 'w:en:Wikipedia:Huggle', |
||
| 47 | 'revert' => 'Reverted edits by.*?WP:HG', |
||
| 48 | ], |
||
| 49 | $tools['Huggle'] |
||
| 50 | ); |
||
| 51 | |||
| 52 | $this->assertEquals(1, array_count_values(array_keys($tools))['Huggle']); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Make sure the right tool is detected |
||
| 57 | */ |
||
| 58 | public function testTool() |
||
| 59 | { |
||
| 60 | $this->assertArraySubset( |
||
| 61 | [ |
||
| 62 | 'name' => 'Huggle', |
||
| 63 | 'regex' => '\(\[\[WP:HG', |
||
| 64 | 'tag' => 'huggle', |
||
| 65 | 'link' => 'w:en:Wikipedia:Huggle', |
||
| 66 | 'revert' => 'Reverted edits by.*?WP:HG', |
||
| 67 | ], |
||
| 68 | $this->aeh->getTool( |
||
| 69 | 'Level 2 warning re. [[Barack Obama]] ([[WP:HG|HG]]) (3.2.0)', |
||
| 70 | 'en.wikipedia.org' |
||
| 71 | ) |
||
| 72 | ); |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Tests that given edit summary is properly asserted as a revert |
||
| 77 | */ |
||
| 78 | public function testIsAutomated() |
||
| 79 | { |
||
| 80 | $this->assertTrue($this->aeh->isAutomated( |
||
| 81 | 'Level 2 warning re. [[Barack Obama]] ([[WP:HG|HG]]) (3.2.0)', |
||
| 82 | 'en.wikipedia.org' |
||
| 83 | )); |
||
| 84 | $this->assertFalse($this->aeh->isAutomated( |
||
| 85 | 'You should try [[WP:Huggle]]', |
||
| 86 | 'en.wikipedia.org' |
||
| 87 | )); |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Test that the revert-related tools of getTools() are properly fetched |
||
| 92 | */ |
||
| 93 | public function testRevertTools() |
||
| 94 | { |
||
| 95 | $tools = $this->aeh->getTools('en.wikipedia.org'); |
||
| 96 | |||
| 97 | $this->assertArraySubset( |
||
| 98 | ['Huggle' => [ |
||
| 99 | 'regex' => '\(\[\[WP:HG', |
||
| 100 | 'tag' => 'huggle', |
||
| 101 | 'link' => 'w:en:Wikipedia:Huggle', |
||
| 102 | 'revert' => 'Reverted edits by.*?WP:HG', |
||
| 103 | ]], |
||
| 104 | $tools |
||
| 105 | ); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Was the edit a revert, based on the edit summary? |
||
| 110 | */ |
||
| 111 | public function testIsRevert() |
||
| 112 | { |
||
| 113 | $this->assertTrue($this->aeh->isRevert( |
||
| 114 | 'Reverted edits by Mogultalk (talk) ([[WP:HG|HG]]) (3.2.0)', |
||
| 115 | 'en.wikipedia.org' |
||
| 116 | )); |
||
| 117 | $this->assertFalse($this->aeh->isRevert( |
||
| 118 | 'You should have reverted this edit using [[WP:HG|Huggle]]', |
||
| 119 | 'en.wikipedia.org' |
||
| 120 | )); |
||
| 121 | } |
||
| 122 | } |
||
| 123 |