Completed
Push — master ( d068c5...99fa9d )
by Antonio
03:22 queued 58s
created

MailMessageWorkerTest::testRunMethodOnSuccess()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 25

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 36
rs 8.8571
cc 1
eloc 25
nc 1
nop 0
1
<?php
2
namespace Da\Mailer\Test\Queue\Cli;
3
4
use Da\Mailer\Mailer;
5
use Da\Mailer\Model\MailMessage;
6
use Da\Mailer\Queue\Cli\MailMessageWorker;
7
use Da\Mailer\Event\Event;
8
use Mockery;
9
use PHPUnit_Framework_TestCase;
10
use Swift_Message;
11
12
class MailMessageWorkerTest extends PHPUnit_Framework_TestCase
13
{
14
15
    public function testRunMethodOnSuccess()
16
    {
17
        $swift = new Swift_Message();
18
        $mockedMailMessage = Mockery::mock(MailMessage::class);
19
20
        $mockedMailMessage
21
            ->shouldReceive('asSwiftMessage')
22
            ->once()
23
            ->andReturn($swift);
24
25
        $mockedMailer = Mockery::mock( Mailer::class);
26
27
        $mockedMailer
28
            ->shouldReceive('sendSwiftMessage')
29
            ->once()
30
            ->with($swift)
31
            ->andReturn(null);
32
33
        $mailMessageWorker = new MailMessageWorker($mockedMailer, $mockedMailMessage);
34
        $eventResponse = null;
35
        $failedRecipientsResponse = 0;
36
37
        $handler = function(Event $event) use (&$eventResponse, &$failedRecipientsResponse){
38
            $data = $event->getData();
39
            $eventResponse = $data[0];
40
            $failedRecipientsResponse = $data[1];
41
        };
42
        $onSuccessEvent = new Event($handler);
43
44
        $mailMessageWorker->attach('onSuccess', $onSuccessEvent);
45
46
        $mailMessageWorker->run();
47
48
        $this->assertEquals($eventResponse, $mockedMailMessage);
49
        $this->assertEquals($failedRecipientsResponse, null);
50
    }
51
52 View Code Duplication
    public function testRunMethodOnFailure()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54
        $swift = new Swift_Message();
55
        $mockedMailMessage = Mockery::mock(MailMessage::class);
56
57
        $mockedMailMessage
58
            ->shouldReceive('asSwiftMessage')
59
            ->once()
60
            ->andReturn($swift);
61
62
        $mockedMailMessage->to = '[email protected]';
0 ignored issues
show
Bug introduced by
Accessing to on the interface Mockery\MockInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
63
64
        $mockedMailer = Mockery::mock( Mailer::class);
65
66
        $mockedMailer
67
            ->shouldReceive('sendSwiftMessage')
68
            ->once()
69
            ->with($swift)
70
            ->andReturn(['[email protected]']);
71
72
        $mailMessageWorker = new MailMessageWorker($mockedMailer, $mockedMailMessage);
73
        $eventResponse = null;
74
        $failedRecipientsResponse = 0;
75
76
        $handler = function(Event $event) use (&$eventResponse, &$failedRecipientsResponse){
77
            $data = $event->getData();
78
            $eventResponse = $data[0];
79
            $failedRecipientsResponse = $data[1];
80
        };
81
        $onSuccessEvent = new Event($handler);
82
83
        $mailMessageWorker->attach('onFailure', $onSuccessEvent);
84
85
        $mailMessageWorker->run();
86
87
        $this->assertEquals($eventResponse, $mockedMailMessage);
88
        $this->assertEquals($failedRecipientsResponse, ['[email protected]']);
89
    }
90
91 View Code Duplication
    public function testRunMethodOnFailureDueToException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92
    {
93
        $swift = new Swift_Message();
94
        $mockedMailMessage = Mockery::mock(MailMessage::class);
95
96
        $mockedMailMessage
97
            ->shouldReceive('asSwiftMessage')
98
            ->once()
99
            ->andReturn($swift);
100
101
        $mockedMailMessage->to = '[email protected]';
0 ignored issues
show
Bug introduced by
Accessing to on the interface Mockery\MockInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
102
103
        $mockedMailer = Mockery::mock( Mailer::class);
104
105
        $mockedMailer
106
            ->shouldReceive('sendSwiftMessage')
107
            ->once()
108
            ->with($swift)
109
            ->andThrow('Exception');
110
111
        $mailMessageWorker = new MailMessageWorker($mockedMailer, $mockedMailMessage);
112
        $eventResponse = null;
113
        $failedRecipientsResponse = 0;
114
115
        $handler = function(Event $event) use (&$eventResponse, &$failedRecipientsResponse){
116
            $data = $event->getData();
117
            $eventResponse = $data[0];
118
            $failedRecipientsResponse = $data[1];
119
        };
120
        $onSuccessEvent = new Event($handler);
121
122
        $mailMessageWorker->attach('onFailure', $onSuccessEvent);
123
124
        $mailMessageWorker->run();
125
126
        $this->assertEquals($eventResponse, $mockedMailMessage);
127
        $this->assertEquals($failedRecipientsResponse, ['[email protected]']);
128
    }
129
}
130