SendingFailTest::testNoServiceLeft()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
use kalanis\EmailApi\Basics;
4
use kalanis\EmailApi\Exceptions;
5
use kalanis\EmailApi\Interfaces;
6
use kalanis\EmailApi\LocalInfo;
7
use kalanis\EmailApi\Sending;
8
9
10
class HaltedNothingLeft extends LocalInfo\DefaultInfo
11
{
12
    public function whenNoDefinitionIsUsable(): void
13
    {
14
        parent::whenNoDefinitionIsUsable();
15
        throw new Exceptions\EmailException('No service left');
16
    }
17
}
18
19
20
class HaltedSendFail extends LocalInfo\DefaultInfo
21
{
22
    public function whenSendFails(Interfaces\ISending $service, Exceptions\EmailException $ex): void
23
    {
24
        parent::whenSendFails($service, $ex);
25
        throw new Exceptions\EmailException('Catch on failed service', null, $ex);
26
    }
27
}
28
29
30
class HaltedResultFail extends LocalInfo\DefaultInfo
31
{
32
    public function whenResultIsNotSuccessful(Interfaces\ISending $service, Basics\Result $result): void
33
    {
34
        parent::whenResultIsNotSuccessful($service, $result);
35
        throw new Exceptions\EmailException('Catch on failed result');
36
    }
37
38
    public function whenSendFails(Interfaces\ISending $service, Exceptions\EmailException $ex): void
39
    {
40
        parent::whenSendFails($service, $ex);
41
        throw $ex; // pass it through catch
42
    }
43
}
44
45
46
class SendingFailTest extends CommonTestClass
47
{
48
    /**
49
     * @throws Exceptions\EmailException
50
     */
51
    public function testNoServiceSet()
52
    {
53
        $lib = new Sending(new LocalInfo\DefaultInfo(), $this->mockServices(false));
54
        $data = $lib->sendEmail($this->mockContent(), $this->mockUser());
55
        $this->assertFalse($data->getStatus());
56
        $this->assertEquals('Sending failed.', $data->getData());
57
    }
58
59
    /**
60
     * @throws Exceptions\EmailException
61
     */
62
    public function testNoServiceExcept()
63
    {
64
        $lib = new Sending(new HaltedNothingLeft(), $this->mockServices(false));
65
        $this->expectException(Exceptions\EmailException::class);
66
        $lib->sendEmail($this->mockContent(), $this->mockUser());
67
        $this->expectExceptionMessageMatches('No service left');
68
    }
69
70
    /**
71
     * @throws Exceptions\EmailException
72
     */
73
    public function testNoServiceLeft()
74
    {
75
        $lib = new Sending(new LocalInfo\DefaultInfo(), $this->mockServices(true, true, false));
76
        $data = $lib->sendEmail($this->mockContent(), $this->mockUser());
77
        $this->assertFalse($data->getStatus());
78
        $this->assertEquals('Sending failed.', $data->getData());
79
    }
80
81
    /**
82
     * @throws Exceptions\EmailException
83
     */
84
    public function testSendingDied()
85
    {
86
        $lib = new Sending(new HaltedSendFail(), $this->mockServices()->mayReturnFirstUnsuccessful(true));
87
        $data = $lib->sendEmail($this->mockContent(), $this->mockUser());
88
        $this->assertFalse($data->getStatus());
89
        $this->assertEquals('died', $data->getData());
90
    }
91
92
    /**
93
     * @throws Exceptions\EmailException
94
     */
95
    public function testSendingDiedResult()
96
    {
97
        $lib = new Sending(new HaltedResultFail(), $this->mockServices());
98
        $this->expectException(Exceptions\EmailException::class);
99
        $lib->sendEmail($this->mockContent(), $this->mockUser());
100
        $this->expectExceptionMessageMatches('Catch on failed result');
101
    }
102
103
    /**
104
     * @throws Exceptions\EmailException
105
     */
106
    public function testSendingDiedExcept()
107
    {
108
        $lib = new Sending(new HaltedResultFail(), $this->mockServices(true, false));
109
        $this->expectException(Exceptions\EmailException::class);
110
        $lib->sendEmail($this->mockContent(), $this->mockUser());
111
        $this->expectExceptionMessageMatches('die on send');
112
    }
113
114
    protected function mockServices(bool $withDummyService = true, bool $getResult = true, bool $canUseService = true): LocalInfo\ServicesOrdering
115
    {
116
        $ordering = new LocalInfo\ServicesOrdering();
117
        if ($withDummyService) {
118
            $service = new DummyService($canUseService, false, $getResult);
119
            $ordering->addService($service);
120
        }
121
        return $ordering;
122
    }
123
}
124