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 |
||
| 3 | namespace BringYourOwnIdeas\Maintenance\Tests\Forms; |
||
| 4 | |||
| 5 | use BringYourOwnIdeas\Maintenance\Forms\GridFieldRefreshButton; |
||
| 6 | use SilverStripe\Core\Config\Config; |
||
| 7 | use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor; |
||
| 8 | use SilverStripe\Forms\GridField\GridField; |
||
| 9 | use SilverStripe\Dev\SapphireTest; |
||
| 10 | use Symbiote\QueuedJobs\Services\QueuedJobService; |
||
| 11 | |||
| 12 | class GridFieldRefreshButtonTest extends SapphireTest |
||
| 13 | { |
||
| 14 | protected static $fixture_file = 'GridFieldRefreshButtonTest.yml'; |
||
| 15 | |||
| 16 | protected function setUp() |
||
| 17 | { |
||
| 18 | parent::setUp(); |
||
| 19 | |||
| 20 | Config::modify()->set(QueuedJobService::class, 'use_shutdown_function', false); |
||
| 21 | } |
||
| 22 | |||
| 23 | public function testHasRunningJob() |
||
| 24 | { |
||
| 25 | $button = new GridFieldRefreshButton('test'); |
||
| 26 | $this->assertTrue($button->hasActiveJob()); |
||
| 27 | } |
||
| 28 | |||
| 29 | public function testDoesNotHaveCancelledCompletedOrBrokenJob() |
||
| 30 | { |
||
| 31 | $this->completeRunningJob(); |
||
| 32 | |||
| 33 | $button = new GridFieldRefreshButton('test'); |
||
| 34 | $this->assertFalse($button->hasActiveJob()); |
||
| 35 | } |
||
| 36 | |||
| 37 | public function testHandleRefreshDoesNotCreateJobWhenJobIsRunning() |
||
| 38 | { |
||
| 39 | $count = QueuedJobDescriptor::get()->count(); |
||
| 40 | |||
| 41 | $button = new GridFieldRefreshButton('test'); |
||
| 42 | $button->handleRefresh(); |
||
| 43 | |||
| 44 | $this->assertSame($count, QueuedJobDescriptor::get()->count()); |
||
| 45 | } |
||
| 46 | |||
| 47 | public function testHandleRefreshCreatesJobWhenNoJobIsRunning() |
||
| 48 | { |
||
| 49 | $this->completeRunningJob(); |
||
| 50 | |||
| 51 | $count = QueuedJobDescriptor::get()->count(); |
||
| 52 | |||
| 53 | $button = new GridFieldRefreshButton('test'); |
||
| 54 | $button->handleRefresh(); |
||
| 55 | |||
| 56 | $this->assertSame($count + 1, QueuedJobDescriptor::get()->count()); |
||
| 57 | } |
||
| 58 | |||
| 59 | public function testHandleCheckReturnsValidJson() |
||
| 60 | { |
||
| 61 | $button = new GridFieldRefreshButton('test'); |
||
| 62 | $this->assertSame('true', $button->handleCheck()); |
||
| 63 | } |
||
| 64 | |||
| 65 | public function testButtonIsDisabledWhenJobIsRunning() |
||
| 66 | { |
||
| 67 | $button = new GridFieldRefreshButton('test'); |
||
| 68 | |||
| 69 | $gridFieldMock = $this->getGridFieldMock(); |
||
| 70 | |||
| 71 | $output = $button->getHTMLFragments($gridFieldMock); |
||
| 72 | |||
| 73 | $this->assertContains('disabled', $output['test']); |
||
| 74 | } |
||
| 75 | |||
| 76 | public function testButtonIsEnabledWhenNoJobIsRunning() |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Turns the running job in the fixture file into a completed job |
||
| 91 | */ |
||
| 92 | protected function completeRunningJob() |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Mocks and returns a gridfield with name 'TestGridField' and 'Link' method, which returns a url |
||
| 101 | * @return mixed |
||
| 102 | */ |
||
| 103 | protected function getGridFieldMock() |
||
| 119 |