CampaignTestHandlerTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 41
c 1
b 0
f 0
dl 0
loc 89
rs 10
wmc 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testApiEndpoint() 0 6 1
A getCampaignTestHandlerMock() 0 12 1
A testAtLeastOneEmailInvalid() 0 7 1
A testSendingFailed() 0 7 1
A testIncorrectCampaignHash() 0 7 1
A testEmptyEmail() 0 7 1
A testCampaignNotReadyToSend() 0 7 1
A testCampaignTestSuccessfullySent() 0 5 1
A testCampaignDoesntExist() 0 7 1
1
<?php
2
/**
3
 * File: CampaignTestHandlerTest.php
4
 *
5
 * @author      Maciej Sławik <[email protected]>
6
 * Github:      https://github.com/maciejslawik
7
 */
8
9
namespace MSlwk\FreshMail\Test\Campaign;
10
11
use MSlwk\FreshMail\Handler\Campaign\CampaignTestHandler;
12
use MSlwk\FreshMail\Tests\BaseTest;
13
use PHPUnit\Framework\TestCase;
14
use MSlwk\FreshMail\Error\ErrorHandler;
15
use MSlwk\FreshMail\Exception\Campaign\FreshMailCampaignException;
16
17
/**
18
 * Class CampaignTestHandlerTest
19
 *
20
 * @package MSlwk\FreshMail\Test\Campaign
21
 */
22
class CampaignTestHandlerTest 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 getCampaignTestHandlerMock($sendRequestReturnValue)
31
    {
32
        $campaignTestHandler = $this->getMockBuilder('\MSlwk\FreshMail\Handler\Campaign\CampaignTestHandler')
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
        $campaignTestHandler = /** @scrutinizer ignore-deprecated */ $this->getMockBuilder('\MSlwk\FreshMail\Handler\Campaign\CampaignTestHandler')

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
        $campaignTestHandler->expects($this->once())
38
            ->method('sendRequest')
39
            ->will($this->returnValue($sendRequestReturnValue));
40
41
        return $campaignTestHandler;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $campaignTestHandler 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 testCampaignTestSuccessfullySent()
45
    {
46
        $campaignTestHandler = $this->getCampaignTestHandlerMock('{"status":"OK"}');
47
        $returnValue = $campaignTestHandler->testCampaign('id_hash', ['[email protected]']);
48
        self::assertNull($returnValue);
49
    }
50
51
    public function testApiEndpoint()
52
    {
53
        $accountCreateHandler = new CampaignTestHandler(new ErrorHandler(), '', '');
54
        $expectedApiEndpoint = '/rest/campaigns/sendTest';
55
        $returnedApiEndpoint = $this->getApiEndpoint($accountCreateHandler);
56
        self::assertEquals($expectedApiEndpoint, $returnedApiEndpoint);
57
    }
58
59
    public function testIncorrectCampaignHash()
60
    {
61
        $campaignTestHandler = $this->getCampaignTestHandlerMock(
62
            '{"errors":[{ "message":"Incorrect campaign hash", "code":"1721" }], "status":"errors"}'
63
        );
64
        $this->expectException(FreshMailCampaignException::class);
65
        $campaignTestHandler->testCampaign('id_hash', ['[email protected]']);
66
    }
67
68
    public function testEmptyEmail()
69
    {
70
        $campaignTestHandler = $this->getCampaignTestHandlerMock(
71
            '{"errors":[{ "message":"Missing email address", "code":"1722" }], "status":"errors"}'
72
        );
73
        $this->expectException(FreshMailCampaignException::class);
74
        $campaignTestHandler->testCampaign('id_hash', ['[email protected]']);
75
    }
76
77
    public function testAtLeastOneEmailInvalid()
78
    {
79
        $campaignTestHandler = $this->getCampaignTestHandlerMock(
80
            '{"errors":[{ "message":"At least one email is invalid", "code":"1723" }], "status":"errors"}'
81
        );
82
        $this->expectException(FreshMailCampaignException::class);
83
        $campaignTestHandler->testCampaign('id_hash', ['[email protected]']);
84
    }
85
86
    public function testCampaignDoesntExist()
87
    {
88
        $campaignTestHandler = $this->getCampaignTestHandlerMock(
89
            '{"errors":[{ "message":"Requested campaign doesnt exist", "code":"1724" }], "status":"errors"}'
90
        );
91
        $this->expectException(FreshMailCampaignException::class);
92
        $campaignTestHandler->testCampaign('id_hash', ['[email protected]']);
93
    }
94
95
    public function testCampaignNotReadyToSend()
96
    {
97
        $campaignTestHandler = $this->getCampaignTestHandlerMock(
98
            '{"errors":[{ "message":"Campaign not ready to send", "code":"1725" }], "status":"errors"}'
99
        );
100
        $this->expectException(FreshMailCampaignException::class);
101
        $campaignTestHandler->testCampaign('id_hash', ['[email protected]']);
102
    }
103
104
    public function testSendingFailed()
105
    {
106
        $campaignTestHandler = $this->getCampaignTestHandlerMock(
107
            '{"errors":[{ "message":"Sending the campaign failed", "code":"1726" }], "status":"errors"}'
108
        );
109
        $this->expectException(FreshMailCampaignException::class);
110
        $campaignTestHandler->testCampaign('id_hash', ['[email protected]']);
111
    }
112
}
113