ListCreateHandlerTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 43
c 1
b 0
f 0
dl 0
loc 87
rs 10
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testListCreatedSuccessfully() 0 12 1
A testIncorrectCustomFields() 0 7 1
A testIncorrectFieldType() 0 7 1
A testEmptyName() 0 7 1
A testListCreationFailer() 0 7 1
A testIncorrectTag() 0 7 1
A getListCreateHandlerMock() 0 12 1
A testApiEndpoint() 0 6 1
1
<?php
2
/**
3
 * File: ListCreateHandlerTest.php
4
 *
5
 * @author      Maciej Sławik <[email protected]>
6
 * Github:      https://github.com/maciejslawik
7
 */
8
9
namespace MSlwk\FreshMail\Test\Lists;
10
11
use MSlwk\FreshMail\Handler\Lists\ListCreateHandler;
12
use MSlwk\FreshMail\Tests\BaseTest;
13
use PHPUnit\Framework\TestCase;
14
use MSlwk\FreshMail\Error\ErrorHandler;
15
use MSlwk\FreshMail\Exception\Lists\FreshMailListException;
16
17
/**
18
 * Class ListCreateHandlerTest
19
 *
20
 * @package MSlwk\FreshMail\Test\Lists
21
 */
22
class ListCreateHandlerTest 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 getListCreateHandlerMock($sendRequestReturnValue)
31
    {
32
        $listCreateHandler = $this->getMockBuilder('\MSlwk\FreshMail\Handler\Lists\ListCreateHandler')
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
        $listCreateHandler = /** @scrutinizer ignore-deprecated */ $this->getMockBuilder('\MSlwk\FreshMail\Handler\Lists\ListCreateHandler')

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
        $listCreateHandler->expects($this->once())
38
            ->method('sendRequest')
39
            ->will($this->returnValue($sendRequestReturnValue));
40
41
        return $listCreateHandler;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $listCreateHandler 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 testListCreatedSuccessfully()
45
    {
46
        $expectedCustomFields = (object)['ssfas' => '4124rfd'];
47
        $expectedHash = 'gvrheg';
48
        $listCreateHandler = $this->getListCreateHandlerMock(
49
            '{"status":"OK","hash":"'
50
            . $expectedHash . '","custom_fields":'
51
            . json_encode($expectedCustomFields) . '}'
52
        );
53
        $returnedData = $listCreateHandler->createSubscriberList('Test', 'Test');
54
        self::assertEquals($expectedCustomFields, $returnedData->custom_fields);
55
        self::assertEquals($expectedHash, $returnedData->hash);
56
    }
57
58
    public function testApiEndpoint()
59
    {
60
        $listCreateHandler = new ListCreateHandler(new ErrorHandler(), '', '');
61
        $expectedApiEndpoint = '/rest/subscribers_list/create';
62
        $returnedApiEndpoint = $this->getApiEndpoint($listCreateHandler);
63
        self::assertEquals($expectedApiEndpoint, $returnedApiEndpoint);
64
    }
65
66
    public function testEmptyName()
67
    {
68
        $listCreateHandler = $this->getListCreateHandlerMock(
69
            '{"errors":[{ "message":"Empty list name", "code":"1601" }], "status":"errors"}'
70
        );
71
        $this->expectException(FreshMailListException::class);
72
        $listCreateHandler->createSubscriberList('Test', 'Test');
73
    }
74
75
    public function testListCreationFailer()
76
    {
77
        $listCreateHandler = $this->getListCreateHandlerMock(
78
            '{"errors":[{ "message":"List couldnt be created", "code":"1602" }], "status":"errors"}'
79
        );
80
        $this->expectException(FreshMailListException::class);
81
        $listCreateHandler->createSubscriberList('Test', 'Test');
82
    }
83
84
    public function testIncorrectCustomFields()
85
    {
86
        $listCreateHandler = $this->getListCreateHandlerMock(
87
            '{"errors":[{ "message":"Custom fields incorrect", "code":"1603" }], "status":"errors"}'
88
        );
89
        $this->expectException(FreshMailListException::class);
90
        $listCreateHandler->createSubscriberList('Test', 'Test');
91
    }
92
93
    public function testIncorrectTag()
94
    {
95
        $listCreateHandler = $this->getListCreateHandlerMock(
96
            '{"errors":[{ "message":"Incorrect tag", "code":"1604" }], "status":"errors"}'
97
        );
98
        $this->expectException(FreshMailListException::class);
99
        $listCreateHandler->createSubscriberList('Test', 'Test');
100
    }
101
102
    public function testIncorrectFieldType()
103
    {
104
        $listCreateHandler = $this->getListCreateHandlerMock(
105
            '{"errors":[{ "message":"Incorrect field type", "code":"1605" }], "status":"errors"}'
106
        );
107
        $this->expectException(FreshMailListException::class);
108
        $listCreateHandler->createSubscriberList('Test', 'Test');
109
    }
110
}
111