SpamTestHandlerTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 41
c 2
b 0
f 0
dl 0
loc 84
rs 10
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testEmptyContent() 0 7 1
A getSpamTestHandlerMock() 0 12 1
A testSpamTestSuccessful() 0 9 1
A testIncorrectFromAddress() 0 7 1
A testApiEndpoint() 0 6 1
A testDailyLimitExceeded() 0 7 1
A testEmptySubject() 0 7 1
A testSystemError() 0 7 1
1
<?php
2
/**
3
 * File: SpamTestHandlerTest.php
4
 *
5
 * @author      Maciej Sławik <[email protected]>
6
 * Github:      https://github.com/maciejslawik
7
 */
8
9
namespace MSlwk\FreshMail\Test\SpamTest;
10
11
use MSlwk\FreshMail\Handler\SpamTest\SpamTestHandler;
12
use MSlwk\FreshMail\Tests\BaseTest;
13
use PHPUnit\Framework\TestCase;
14
use MSlwk\FreshMail\Error\ErrorHandler;
15
use MSlwk\FreshMail\Exception\SpamTest\FreshMailSpamTestException;
16
17
/**
18
 * Class SpamTestHandlerTest
19
 *
20
 * @package MSlwk\FreshMail\Test\SpamTest
21
 */
22
class SpamTestHandlerTest extends TestCase
23
{
24
    use BaseTest;
25
26
    /**
27
     * @param $sendRequestReturnValue
28
     * @return \PHPUnit_Framework_MockObject_MockObject
0 ignored issues
show
Bug introduced by
The type PHPUnit_Framework_MockObject_MockObject was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
     */
30
    public function getSpamTestHandlerMock($sendRequestReturnValue)
31
    {
32
        $spamTestHandler = $this->getMockBuilder('\MSlwk\FreshMail\Handler\SpamTest\SpamTestHandler')
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated: https://github.com/sebastianbergmann/phpunit/pull/3687 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

32
        $spamTestHandler = /** @scrutinizer ignore-deprecated */ $this->getMockBuilder('\MSlwk\FreshMail\Handler\SpamTest\SpamTestHandler')

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
33
            ->setConstructorArgs([new ErrorHandler(), '', ''])
34
            ->setMethods(['sendRequest'])
35
            ->getMock();
36
37
        $spamTestHandler->expects($this->once())
38
            ->method('sendRequest')
39
            ->will($this->returnValue($sendRequestReturnValue));
40
41
        return $spamTestHandler;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $spamTestHandler returns the type PHPUnit\Framework\MockObject\MockObject which is incompatible with the documented return type PHPUnit_Framework_MockObject_MockObject.
Loading history...
42
    }
43
44
    public function testSpamTestSuccessful()
45
    {
46
        $expectedTestsFailed = ['test1'];
47
        $expectedScore = 3.2;
48
        $spamTestHandler = $this->getSpamTestHandlerMock('{"status":"OK","tests":'
49
            . json_encode($expectedTestsFailed) . ',"score":' . $expectedScore . '}');
50
        $returnData = $spamTestHandler->spamCheck('Test', 'Test');
51
        self::assertEquals($expectedTestsFailed, $returnData->tests);
52
        self::assertEquals($expectedScore, $returnData->score);
53
    }
54
55
    public function testApiEndpoint()
56
    {
57
        $accountCreateHandler = new SpamTestHandler(new ErrorHandler(), '', '');
58
        $expectedApiEndpoint = '/rest/spam_test/check';
59
        $returnedApiEndpoint = $this->getApiEndpoint($accountCreateHandler);
60
        self::assertEquals($expectedApiEndpoint, $returnedApiEndpoint);
61
    }
62
63
    public function testEmptySubject()
64
    {
65
        $spamTestHandler = $this->getSpamTestHandlerMock(
66
            '{"errors":[{ "message":"Empty subject", "code":"1901" }], "status":"errors"}'
67
        );
68
        $this->expectException(FreshMailSpamTestException::class);
69
        $spamTestHandler->spamCheck('Test', 'Test');
70
    }
71
72
    public function testIncorrectFromAddress()
73
    {
74
        $spamTestHandler = $this->getSpamTestHandlerMock(
75
            '{"errors":[{ "message":"Incorrect from_address", "code":"1902" }], "status":"errors"}'
76
        );
77
        $this->expectException(FreshMailSpamTestException::class);
78
        $spamTestHandler->spamCheck('Test', 'Test');
79
    }
80
81
    public function testEmptyContent()
82
    {
83
        $spamTestHandler = $this->getSpamTestHandlerMock(
84
            '{"errors":[{ "message":"Empty contect", "code":"1904" }], "status":"errors"}'
85
        );
86
        $this->expectException(FreshMailSpamTestException::class);
87
        $spamTestHandler->spamCheck('Test', 'Test');
88
    }
89
90
    public function testSystemError()
91
    {
92
        $spamTestHandler = $this->getSpamTestHandlerMock(
93
            '{"errors":[{ "message":"Spam-test system error", "code":"1906" }], "status":"errors"}'
94
        );
95
        $this->expectException(FreshMailSpamTestException::class);
96
        $spamTestHandler->spamCheck('Test', 'Test');
97
    }
98
99
    public function testDailyLimitExceeded()
100
    {
101
        $spamTestHandler = $this->getSpamTestHandlerMock(
102
            '{"errors":[{ "message":"Daily limit exceeded", "code":"1907" }], "status":"errors"}'
103
        );
104
        $this->expectException(FreshMailSpamTestException::class);
105
        $spamTestHandler->spamCheck('Test', 'Test');
106
    }
107
108
109
}
110