Passed
Push — master ( 7c85b2...443a3c )
by Andrey
03:17
created

IntercomChannelTest::testItCanSendMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 25
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * @link      http://horoshop.ua
4
 * @copyright Copyright (c) 2015-2018 Horoshop TM
5
 * @author    Andrey Telesh <[email protected]>
6
 */
7
8
namespace FtwSoft\NotificationChannels\Intercom\Tests;
9
10
use FtwSoft\NotificationChannels\Intercom\Exceptions\InvalidArgumentException;
11
use FtwSoft\NotificationChannels\Intercom\Exceptions\MessageIsNotCompleteException;
12
use FtwSoft\NotificationChannels\Intercom\Exceptions\RequestException;
13
use FtwSoft\NotificationChannels\Intercom\IntercomChannel;
14
use FtwSoft\NotificationChannels\Intercom\IntercomMessage;
15
use FtwSoft\NotificationChannels\Intercom\Tests\Mocks\TestNotifiable;
16
use FtwSoft\NotificationChannels\Intercom\Tests\Mocks\TestNotification;
17
use GuzzleHttp\Exception\BadResponseException;
18
use GuzzleHttp\Psr7\Request;
19
use Illuminate\Notifications\Notification;
20
use Intercom\IntercomMessages;
21
use Mockery\Adapter\Phpunit\MockeryTestCase;
22
use Intercom\IntercomClient;
23
24
class IntercomChannelTest extends MockeryTestCase
25
{
26
27
    /**
28
     * @var \Intercom\IntercomMessages|\Mockery\Mock
29
     */
30
    private $intercomMessages;
31
32
    /**
33
     * @var \Intercom\IntercomClient
34
     */
35
    private $intercom;
36
37
    /**
38
     * @var \FtwSoft\NotificationChannels\Intercom\IntercomChannel
39
     */
40
    private $channel;
41
42
    protected function setUp()
43
    {
44
        parent::setUp();
45
46
        $this->intercom = new IntercomClient(null, null);
47
        $this->intercomMessages = \Mockery::mock(IntercomMessages::class, $this->intercom);
48
        $this->intercom->messages = $this->intercomMessages;
0 ignored issues
show
Documentation Bug introduced by
$this->intercomMessages is of type Mockery\MockInterface, but the property $intercomMessages was declared to be of type Mockery\Mock|Intercom\IntercomMessages. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
Documentation Bug introduced by
It seems like $this->intercomMessages of type Mockery\MockInterface is incompatible with the declared type Intercom\IntercomMessages of property $messages.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
49
        $this->channel = new IntercomChannel($this->intercom);
50
    }
51
52
    public function testItCanSendMessage(): void
53
    {
54
        $notification = new TestNotification(
55
            IntercomMessage::create('Hello World!')
56
                ->from(123)
57
                ->toUserId(321)
58
        );
59
60
        $this->intercomMessages->shouldReceive('create')
0 ignored issues
show
Bug introduced by
The method shouldReceive() does not exist on Intercom\IntercomMessages. ( Ignorable by Annotation )

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

60
        $this->intercomMessages->/** @scrutinizer ignore-call */ 
61
                                 shouldReceive('create')

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
61
            ->once()
62
            ->with([
63
                'body'         => 'Hello World!',
64
                'message_type' => 'inapp',
65
                'from'         => [
66
                    'type' => 'admin',
67
                    'id'   => '123'
68
                ],
69
                'to'           => [
70
                    'type' => 'user',
71
                    'id'   => '321'
72
                ]
73
            ]);
74
75
        $this->channel->send(new TestNotifiable(), $notification);
76
        $this->assertPostConditions();
77
    }
78
79
    public function testInThrowsAnExceptionWhenNotificationIsNotAnIntercomNotification()
80
    {
81
        $notification = new Notification();
82
83
        $this->expectException(InvalidArgumentException::class);
84
        $this->channel->send(new TestNotifiable(), $notification);
85
    }
86
87
    public function testItThrowsAnExceptionWhenRecipientIsNotProvided()
88
    {
89
        $notification = new TestNotification(
90
            IntercomMessage::create('Hello World!')
91
                ->from(123)
92
        );
93
94
        $this->expectException(MessageIsNotCompleteException::class);
95
        $this->channel->send(new TestNotifiable(), $notification);
96
    }
97
98
    public function testItThrowsAnExceptionSomeOfRequiredParamsAreNotDefined()
99
    {
100
        $notification = new TestNotification(
101
            IntercomMessage::create()
102
                ->from(123)
103
                ->toUserId(321)
104
        );
105
106
        $this->expectException(MessageIsNotCompleteException::class);
107
        $this->channel->send(new TestNotifiable(), $notification);
108
    }
109
110
    public function testItThrowsRequestExceptionOnGuzzleBadResponseException(): void
111
    {
112
        $this->intercomMessages->shouldReceive('create')
113
            ->once()
114
            ->andThrow(new BadResponseException('Test case', new Request('post', 'http://foo.bar')));
115
116
        $notification = new TestNotification(
117
            IntercomMessage::create('Hello World!')
118
                ->from(123)
119
                ->toUserId(321)
120
        );
121
122
        $this->expectException(RequestException::class);
123
124
        $this->channel->send(new TestNotifiable(), $notification);
125
    }
126
127
    public function testItGetsToFromRouteNotificationForIntercomMethod(): void
128
    {
129
        $this->intercomMessages->shouldReceive('create');
130
        
131
        $message = IntercomMessage::create('Hello World!')
132
            ->from(123);
133
        $notification = new TestNotification($message);
134
135
        $expected = ['type' => 'user', 'id' => 321];
136
        $this->channel->send(new TestNotifiable($expected), $notification);
137
138
        $this->assertEquals($expected, $message->payload['to']);
139
    }
140
141
}