Completed
Push — master ( 427abe...993846 )
by Franco
15:31
created

BlogArchiveWidgetTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 96
Duplicated Lines 26.04 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 2
cbo 4
dl 25
loc 96
rs 10

6 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
B testArchiveMonthlyWithNewPostsAdded() 0 24 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 12: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
    public function testArchiveMonthlyWithNewPostsAdded()
85
    {
86
        $original = Versioned::current_stage();
0 ignored issues
show
Bug introduced by
The method current_stage() does not seem to exist on object<SilverStripe\Versioned\Versioned>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
87
        Versioned::reading_stage('Stage');
0 ignored issues
show
Bug introduced by
The method reading_stage() does not seem to exist on object<SilverStripe\Versioned\Versioned>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
88
89
        $widget = $this->objFromFixture('BlogArchiveWidget', 'archive-monthly');
90
        $archive = $widget->getArchive();
91
92
        $this->assertCount(3, $archive, 'Three months are shown in the blog archive list from fixtures');
93
94
        SS_Datetime::set_mock_now('2018-01-01 12:00:00');
95
96
        $newPost = new BlogPost;
97
        $newPost->ParentID = $this->objFromFixture('Blog', 'my-blog')->ID;
98
        $newPost->Title = 'My new blog post';
99
        $newPost->PublishDate = '2018-01-01 08:00:00'; // Same day as the mocked now, but slightly earlier
100
        $newPost->write();
101
102
        $archive = $widget->getArchive();
103
104
        $this->assertCount(4, $archive, 'Four months are shown in the blog archive list after new post added');
105
106
        Versioned::reading_stage($original);
0 ignored issues
show
Bug introduced by
The method reading_stage() does not seem to exist on object<SilverStripe\Versioned\Versioned>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
107
    }
108
}
109