GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

CommentControllerTest::testIsSpam()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 23
nc 1
nop 2
1
<?php
2
namespace RbCommentTest\Controller;
3
4
use RbComment\Controller\CommentController;
5
use RbComment\Model\CommentTable;
6
use Zend\Http\Request;
7
use Zend\Mvc\Controller\Plugin\Redirect;
8
use Zend\Mvc\Plugin\FlashMessenger\FlashMessenger;
9
use Zend\ServiceManager\ServiceLocatorInterface;
10
use PHPUnit_Framework_TestCase;
11
use ReflectionClass;
12
use ZendService\Akismet\Akismet;
13
14
class CommentControllerTest extends PHPUnit_Framework_TestCase
15
{
16
    protected $configMock = [
17
        'rb_comment' => [
18
            'strings' => [
19
                'author'  => 'author',
20
                'contact' => 'contact',
21
                'content' => 'content',
22
                'submit'  => 'submit',
23
            ],
24
        ],
25
    ];
26
27
    protected $requestMock;
28
    protected $serviceLocatorMock;
29
    protected $commentTableMock;
30
    protected $akismetServiceMock;
31
32
    public function setUp()
33
    {
34
        $this->serviceLocatorMock = $this->createMock(ServiceLocatorInterface::class);
35
        $this->requestMock        = $this->createMock(Request::class);
36
        $this->commentTableMock   = $this->createMock(CommentTable::class);
37
        $this->akismetServiceMock = $this->createMock(Akismet::class);
38
39
        // Global values
40
        $_SERVER['HTTP_USER_AGENT'] = 'RbComment Testing Suite';
41
    }
42
43
    public function testAddActionOnlyWorksWithPostMethod()
44
    {
45
        // Mocks
46
        $this->requestMock->expects($this->once())
47
                          ->method('isPost')
48
                         ->will($this->returnValue(false));
49
50
        $commentControllerMock =
51
            $this->getMockBuilder(CommentController::class)
52
                 ->setConstructorArgs([
53
                     $this->configMock,
54
                     $this->commentTableMock,
55
                     $this->akismetServiceMock
56
                 ])
57
                 ->setMethods(['getRequest'])
58
                 ->getMock();
59
60
        $commentControllerMock->expects($this->once())
61
                              ->method('getRequest')
62
                              ->willReturn($this->requestMock);
63
64
        $commentControllerMock->addAction();
65
    }
66
67
    public function testAddActionLogsFormErrorsIntoTheRbCommentNamespace()
68
    {
69
        //'contact' key is missing on purpose
70
        $postMock = [
71
            'author'  => 'Tester',
72
            'content' => 'test',
73
            'uri'     => '/test',
74
        ];
75
76
        // Request Mock Setup
77
        $this->requestMock->expects($this->once())
78
                          ->method('isPost')
79
                          ->will($this->returnValue(true));
80
81
        $this->requestMock->expects($this->once())
82
                          ->method('getPost')
83
                          ->will($this->returnValue($postMock));
84
85
        // FlashMessenger Mock
86
        $flashMessengerMock = $this->createMock(FlashMessenger::class);
87
88
        $flashMessengerMock->expects($this->once())
89
                           ->method('setNamespace')
90
                           ->with('RbComment');
91
92
        // Redirect Mock
93
        $redirectMock = $this->createMock(Redirect::class);
94
95
        $redirectMock->expects($this->once())
96
                     ->method('toUrl')
97
                     ->with($postMock['uri'] . '#rbcomment');
98
99
        // CommentController Mock
100
        $commentControllerMock =
101
            $this->getMockBuilder(CommentController::class)
102
                 ->setConstructorArgs([
103
                     $this->configMock,
104
                     $this->commentTableMock,
105
                     $this->akismetServiceMock
106
                 ])
107
                 ->setMethods(['getRequest',  'flashMessenger', 'redirect'])
108
                 ->getMock();
109
110
        $commentControllerMock->expects($this->once())
111
                              ->method('getRequest')
112
                              ->will($this->returnValue($this->requestMock));
113
114
        $commentControllerMock->expects($this->exactly(2))
115
                              ->method('flashMessenger')
116
                              ->will($this->returnValue($flashMessengerMock));
117
118
        $commentControllerMock->expects($this->once())
119
                              ->method('redirect')
120
                              ->will($this->returnValue($redirectMock));
121
122
        $commentControllerMock->addAction();
123
    }
124
125
    /**
126
     * @dataProvider isSpamDataProvider
127
     */
128
    public function testIsSpam($comment, $isSpam)
129
    {
130
        $rbCommentConfig = (object) [
131
            'akismet' => [
132
                'proxy' => [
133
                    'use'     => false,
134
                    'trusted' => [],
135
                    'header'  => '',
136
                ],
137
            ],
138
        ];
139
140
        $akismetServiceMock =
141
            $this->getMockBuilder(Akismet::class)
142
                 ->disableOriginalConstructor()
143
                 ->setMethods(['isSpam'])
144
                 ->getMock();
145
146
        $akismetServiceMock->expects($this->once())
147
                           ->method('isSpam')
148
                           ->will($this->returnValue($isSpam));
149
150
        $commentControllerReflection = new ReflectionClass('RbComment\Controller\CommentController');
151
152
        $isSpamReflection = $commentControllerReflection->getMethod('isSpam');
153
        $isSpamReflection->setAccessible(true);
154
155
        $commentController = new CommentController(
156
            $this->configMock,
157
            $this->commentTableMock,
158
            $akismetServiceMock
159
        );
160
161
        $this->assertEquals($isSpam, $isSpamReflection->invoke($commentController, $comment, $rbCommentConfig));
162
    }
163
164
    /**
165
     * @return array
166
     */
167
    public static function isSpamDataProvider()
168
    {
169
        return [
170
            [
171
                // comment
172
                (object) [
173
                    'author'  => 'not a spammer',
174
                    'contact' => '[email protected]',
175
                    'content' => 'test',
176
                ],
177
                // isSpam
178
                false,
179
            ],
180
            [
181
                // comment
182
                (object) [
183
                    'author'  => 'spammer',
184
                    'contact' => '[email protected]',
185
                    'content' => 'spam',
186
                ],
187
                // isSpam
188
                true,
189
            ],
190
        ];
191
    }
192
}
193