Completed
Push — master ( b1acc1...427abe )
by Robbie
01:35
created

BlogArchiveWidgetTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 35.21 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 2
cbo 3
dl 25
loc 71
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 12 2
A tearDown() 0 6 1
A testArchiveMonthlyFromStage() 13 13 1
A testArchiveMonthlyFromLive() 0 19 2
A testArchiveYearly() 12 12 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
namespace SilverStripe\Blog\Tests;
4
5
use SilverStripe\Blog\Model\BlogPost;
6
use SilverStripe\Blog\Widgets\BlogArchiveWidget;
7
use SilverStripe\Dev\SapphireTest;
8
use SilverStripe\ORM\FieldType\DBDatetime;
9
use SilverStripe\ORM\SS_List;
10
use SilverStripe\Versioned\Versioned;
11
use SilverStripe\Widgets\Model\Widget;
12
13
class BlogArchiveWidgetTest extends SapphireTest
14
{
15
    protected static $fixture_file = 'BlogArchiveWidgetTest.yml';
16
17
    protected function setUp()
18
    {
19
        if (!class_exists(Widget::class)) {
20
            self::$fixture_file = null;
21
            parent::setUp();
22
            $this->markTestSkipped('Test requires silverstripe/widgets to be installed.');
23
        }
24
25
        DBDatetime::set_mock_now('2017-09-20 00:00:00');
26
27
        parent::setUp();
28
    }
29
30
    protected function tearDown()
31
    {
32
        parent::tearDown();
33
34
        DBDatetime::clear_mock_now();
35
    }
36
37 View Code Duplication
    public function testArchiveMonthlyFromStage()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
    {
39
        $widget = $this->objFromFixture(BlogArchiveWidget::class, 'archive-monthly');
40
        $archive = $widget->getArchive();
41
42
        $this->assertInstanceOf(SS_List::class, $archive);
43
        $this->assertCount(3, $archive);
44
        $this->assertDOSContains([
45
            ['Title' => 'August 2017'],
46
            ['Title' => 'September 2017'],
47
            ['Title' => 'May 2015'],
48
        ], $archive);
49
    }
50
51
    public function testArchiveMonthlyFromLive()
52
    {
53
        $original = Versioned::get_stage();
54
55
        $this->objFromFixture(BlogPost::class, 'post-b')->publishRecursive();
56
        Versioned::set_stage(Versioned::LIVE);
57
58
        $widget = $this->objFromFixture(BlogArchiveWidget::class, 'archive-monthly');
59
        $archive = $widget->getArchive();
60
61
        $this->assertCount(1, $archive);
62
        $this->assertDOSContains([
63
            ['Title' => 'August 2017'],
64
        ], $archive);
65
66
        if ($original) {
67
            Versioned::set_stage($original);
68
        }
69
    }
70
71 View Code Duplication
    public function testArchiveYearly()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
    {
73
        $widget = $this->objFromFixture(BlogArchiveWidget::class, 'archive-yearly');
74
        $archive = $widget->getArchive();
75
76
        $this->assertInstanceOf(SS_List::class, $archive);
77
        $this->assertCount(2, $archive);
78
        $this->assertDOSContains([
79
            ['Title' => '2017'],
80
            ['Title' => '2015'],
81
        ], $archive);
82
    }
83
}
84