Completed
Push — master ( e42a4c...f187a0 )
by Daniel
03:17
created

CommentListTest::setUp()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 16

Duplication

Lines 23
Ratio 95.83 %
Metric Value
dl 23
loc 24
rs 8.9713
cc 1
eloc 16
nc 1
nop 0
1
<?php
2
3
class CommentListTest extends FunctionalTest
0 ignored issues
show
Coding Style Compatibility introduced by Gordon Anderson
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
6
    public static $fixture_file = 'comments/tests/CommentsTest.yml';
7
8
    protected $extraDataObjects = array(
9
        'CommentableItem',
10
        'CommentableItemEnabled',
11
        'CommentableItemDisabled'
12
    );
13
14 View Code Duplication
    public function setUp()
0 ignored issues
show
Duplication introduced by Gordon Anderson
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...
15
    {
16
        parent::setUp();
17
        Config::nest();
18
19
        // Set good default values
20
        Config::inst()->update('CommentsExtension', 'comments', array(
21
            'enabled' => true,
22
            'enabled_cms' => false,
23
            'require_login' => false,
24
            'require_login_cms' => false,
25
            'required_permission' => false,
26
            'require_moderation_nonmembers' => false,
27
            'require_moderation' => false,
28
            'require_moderation_cms' => false,
29
            'frontend_moderation' => false,
30
            'frontend_spam' => false,
31
        ));
32
33
        // Configure this dataobject
34
        Config::inst()->update('CommentableItem', 'comments', array(
35
            'enabled_cms' => true
36
        ));
37
    }
38
39
    public function tearDown()
40
    {
41
        Config::unnest();
42
        parent::tearDown();
43
    }
44
45
    public function testGetForeignClass()
46
    {
47
        $item = $this->objFromFixture('CommentableItem', 'first');
48
49
        // This is the class the Comments are related to
50
        $this->assertEquals('CommentableItem',
0 ignored issues
show
Bug introduced by Gordon Anderson
The method assertEquals() does not seem to exist on object<CommentListTest>.

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...
51
                                $item->Comments()->getForeignClass());
52
    }
53
54 View Code Duplication
    public function testAddNonComment()
0 ignored issues
show
Duplication introduced by Gordon Anderson
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...
55
    {
56
        $item = $this->objFromFixture('CommentableItem', 'first');
57
        $comments = $item->Comments();
58
        $this->assertEquals(4, $comments->count());
0 ignored issues
show
Bug introduced by Gordon Anderson
The method assertEquals() does not seem to exist on object<CommentListTest>.

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...
59
        $member = Member::get()->first();
60
        try {
61
            $comments->add($member);
62
            $this->fail('Should not have been able to add member to comments');
0 ignored issues
show
Bug introduced by Gordon Anderson
The method fail() does not seem to exist on object<CommentListTest>.

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...
63
        } catch (InvalidArgumentException $e) {
64
            $this->assertEquals(
0 ignored issues
show
Bug introduced by Gordon Anderson
The method assertEquals() does not seem to exist on object<CommentListTest>.

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
                'CommentList::add() expecting a Comment object, or ID value',
66
                $e->getMessage()
67
            );
68
        }
69
    }
70
71
    public function testAddComment()
72
    {
73
        $item = $this->objFromFixture('CommentableItem', 'first');
74
        $firstComment = $this->objFromFixture('Comment', 'firstComA');
0 ignored issues
show
Unused Code introduced by Gordon Anderson
$firstComment is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
75
        $comments = $item->Comments();//->sort('Created');
0 ignored issues
show
Unused Code Comprehensibility introduced by Gordon Anderson
84% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
76
77
        foreach ($comments as $comment) {
78
            error_log($comment->ID . ' ' . $comment->Created .' ' . $comment->Comment);
79
        }
80
81
        $this->assertEquals(4, $comments->count());
0 ignored issues
show
Bug introduced by Gordon Anderson
The method assertEquals() does not seem to exist on object<CommentListTest>.

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...
82
        $newComment = new Comment();
83
        $newComment->Name = 'Fred Bloggs';
84
        $newComment->Comment = 'This is a test comment';
85
        $newComment->write();
86
87
        $comments->add($newComment);
88
        // As a comment has been added, there should be 5 comments now
89
        $this->assertEquals(5, $item->Comments()->count());
0 ignored issues
show
Bug introduced by Gordon Anderson
The method assertEquals() does not seem to exist on object<CommentListTest>.

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...
90
91
        $newComment2 = new Comment();
92
        $newComment2->Name = 'John Smith';
93
        $newComment2->Comment = 'This is another test comment';
94
        $newComment2->write();
95
96
        // test adding the same comment by ID
97
        $comments->add($newComment2->ID);
98
        $this->assertEquals(6, $item->Comments()->count());
0 ignored issues
show
Bug introduced by Gordon Anderson
The method assertEquals() does not seem to exist on object<CommentListTest>.

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...
99
100
        $this->setExpectedException(
0 ignored issues
show
Bug introduced by Gordon Anderson
The method setExpectedException() does not seem to exist on object<CommentListTest>.

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...
101
            'InvalidArgumentException',
102
            "CommentList::add() can't be called until a single foreign ID is set"
103
        );
104
        $list = new CommentList('CommentableItem');
105
        $list->add($newComment);
106
    }
107
108
    public function testRemoveComment()
109
    {
110
        // remove by comment
111
        $item = $this->objFromFixture('CommentableItem', 'first');
112
        $this->assertEquals(4, $item->Comments()->count());
0 ignored issues
show
Bug introduced by Gordon Anderson
The method assertEquals() does not seem to exist on object<CommentListTest>.

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...
113
        $comments = $item->Comments();
114
        $comment = $comments->first();
115
        $comments->remove($comment);
116
117
        // now remove by ID
118
        $comments = $item->Comments();
119
        $comment = $comments->first();
120
        $comments->remove($comment->ID);
121
        $this->assertEquals(2, $item->Comments()->count());
0 ignored issues
show
Bug introduced by Gordon Anderson
The method assertEquals() does not seem to exist on object<CommentListTest>.

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...
122
    }
123
124 View Code Duplication
    public function testRemoveNonComment()
0 ignored issues
show
Duplication introduced by Gordon Anderson
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...
125
    {
126
        $item = $this->objFromFixture('CommentableItem', 'first');
127
        $this->assertEquals(4, $item->Comments()->count());
0 ignored issues
show
Bug introduced by Gordon Anderson
The method assertEquals() does not seem to exist on object<CommentListTest>.

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...
128
        $comments = $item->Comments();
129
130
        // try and remove a non comment
131
        $member = Member::get()->first();
132
133
134
135
        try {
136
            $comments->remove($member);
137
            $this->fail('Should not have been able to remove member from comments');
0 ignored issues
show
Bug introduced by Gordon Anderson
The method fail() does not seem to exist on object<CommentListTest>.

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...
138
        } catch (InvalidArgumentException $e) {
139
            $this->assertEquals(
0 ignored issues
show
Bug introduced by Gordon Anderson
The method assertEquals() does not seem to exist on object<CommentListTest>.

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...
140
                'CommentList::remove() expecting a Comment object, or ID',
141
                $e->getMessage()
142
            );
143
        }
144
    }
145
}
146