Completed
Pull Request — master (#540)
by Daniel
10:54
created

BlogArchiveWidgetTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 94
Duplicated Lines 26.6 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 4
dl 25
loc 94
rs 10
c 0
b 0
f 0

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 17 1
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
class BlogArchiveWidgetTest extends SapphireTest
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    protected static $fixture_file = 'BlogArchiveWidgetTest.yml';
6
7
    public function setUp()
8
    {
9
        if (!class_exists('Widget')) {
10
            self::$fixture_file = null;
11
            parent::setUp();
12
            $this->markTestSkipped('Test requires silverstripe/widgets to be installed.');
0 ignored issues
show
Bug introduced by
The method markTestSkipped() does not seem to exist on object<BlogArchiveWidgetTest>.

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...
13
        }
14
15
        SS_Datetime::set_mock_now('2017-09-20 12:00:00');
16
17
        parent::setUp();
18
    }
19
20
    public function tearDown()
21
    {
22
        parent::tearDown();
23
24
        SS_Datetime::clear_mock_now();
25
    }
26
27 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...
28
    {
29
        $widget = $this->objFromFixture('BlogArchiveWidget', 'archive-monthly');
30
        $archive = $widget->getArchive();
31
32
        $this->assertInstanceOf('SS_List', $archive);
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<BlogArchiveWidgetTest>.

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...
33
        $this->assertCount(3, $archive);
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<BlogArchiveWidgetTest>.

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...
34
        $this->assertDOSContains(array(
35
            array('Title' => 'August 2017'),
36
            array('Title' => 'September 2017'),
37
            array('Title' => 'May 2015'),
38
        ), $archive);
39
    }
40
41
    public function testArchiveMonthlyFromLive()
42
    {
43
        $original = Versioned::current_stage();
44
45
        $this->objFromFixture('BlogPost', 'post-b')->doPublish();
46
        Versioned::reading_stage('Live');
47
48
        $widget = $this->objFromFixture('BlogArchiveWidget', 'archive-monthly');
49
        $archive = $widget->getArchive();
50
51
        $this->assertCount(1, $archive);
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<BlogArchiveWidgetTest>.

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...
52
        $this->assertDOSContains(array(
53
            array('Title' => 'August 2017'),
54
        ), $archive);
55
56
        Versioned::reading_stage($original);
57
    }
58
59 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...
60
    {
61
        $widget = $this->objFromFixture('BlogArchiveWidget', 'archive-yearly');
62
        $archive = $widget->getArchive();
63
64
        $this->assertInstanceOf('SS_List', $archive);
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<BlogArchiveWidgetTest>.

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...
65
        $this->assertCount(2, $archive);
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<BlogArchiveWidgetTest>.

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...
66
        $this->assertDOSContains(array(
67
            array('Title' => '2017'),
68
            array('Title' => '2015'),
69
        ), $archive);
70
    }
71
72
    public function testArchiveMonthlyWithNewPostsAdded()
73
    {
74
        $original = Versioned::current_stage();
75
        Versioned::reading_stage('Stage');
76
77
        $widget = $this->objFromFixture('BlogArchiveWidget', 'archive-monthly');
78
        $archive = $widget->getArchive();
79
80
        $this->assertCount(3, $archive, 'Three months are shown in the blog archive list from fixtures');
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<BlogArchiveWidgetTest>.

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...
81
82
        SS_Datetime::set_mock_now('2018-01-01 12:00:00');
83
84
        $newPost = new BlogPost;
85
        $newPost->ParentID = $this->objFromFixture('Blog', 'my-blog')->ID;
86
        $newPost->Title = 'My new blog post';
87
        $newPost->PublishDate = '2018-01-01 08:00:00'; // Same day as the mocked now, but slightly earlier
88
        $newPost->write();
89
90
        $archive = $widget->getArchive();
91
92
        $this->assertCount(4, $archive, 'Four months are shown in the blog archive list after new post added');
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<BlogArchiveWidgetTest>.

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...
93
94
        Versioned::reading_stage($original);
95
    }
96
}
97