FileNotificationTypeTest::testNotify()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace TonicHealthCheck\Test\Incident\Siren\NotificationType;
4
5
use PHPUnit_Framework_TestCase;
6
use TonicHealthCheck\Incident\IncidentInterface;
7
use TonicHealthCheck\Incident\Siren\NotificationType\FileNotificationType;
8
use TonicHealthCheck\Test\Incident\IncidentCreateTrait;
9
use TonicHealthCheck\Test\Incident\Subject\SubjectCreateTrait;
10
11
/**
12
 * Class FileNotificationTypeTest.
13
 */
14
class FileNotificationTypeTest extends PHPUnit_Framework_TestCase
15
{
16
    use SubjectCreateTrait;
17
    use IncidentCreateTrait;
18
19
    /**
20
     * @var string
21
     */
22
    private $pathMessage;
23
24
    /**
25
     * @var FileNotificationType;
26
     */
27
    private $fileNType;
28
29
    /**
30
     * set up file notification env.
31
     */
32
    public function setUp()
33
    {
34
        $this->setPathMessage(sys_get_temp_dir());
35
36
        $this->setFileNType(new FileNotificationType($this->getPathMessage()));
37
    }
38
39
    /**
40
     * test email notify ok.
41
     */
42
    public function testNotify()
43
    {
44
        $incident = $this->createIncidentMock();
45
46
        $incident->expects($this->any())->method('getStatus')->willReturn(IncidentInterface::STATUS_OK + 1);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in TonicHealthCheck\Incident\IncidentInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
47
48
        $subject = $this->createSubject('[email protected]', '* * * * *');
49
50
        $this->getFileNType()->notify($subject, $incident);
0 ignored issues
show
Bug introduced by
It seems like $incident defined by $this->createIncidentMock() on line 44 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, TonicHealthCheck\Inciden...ificationType::notify() does only seem to accept object<TonicHealthCheck\...dent\IncidentInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
51
52
        $this->assertStringEqualsFile($this->getFileNType()->getSubjectNotifyFilePath($subject), $incident->getMessage());
0 ignored issues
show
Bug introduced by
The method getMessage does only exist in TonicHealthCheck\Incident\IncidentInterface, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
53
    }
54
55
    /**
56
     * Test Message Not Writable throw Exception.
57
     *
58
     * @expectedException \TonicHealthCheck\Incident\Siren\NotificationType\FileNotificationTypeException
59
     * @expectedExceptionMessage Notification directory /test343/tes232 doesn't writable
60
     */
61
    public function testPathMessageNotWritable()
62
    {
63
        $this->getFileNType()->setPathMessage('/test343/tes232');
64
    }
65
    /**
66
     * @return FileNotificationType
67
     */
68
    protected function getFileNType()
69
    {
70
        return $this->fileNType;
71
    }
72
73
    /**
74
     * @param FileNotificationType $fileNType
75
     */
76
    protected function setFileNType(FileNotificationType $fileNType)
77
    {
78
        $this->fileNType = $fileNType;
79
    }
80
81
    /**
82
     * @return mixed
83
     */
84
    protected function getPathMessage()
85
    {
86
        return $this->pathMessage;
87
    }
88
89
    /**
90
     * @param mixed $pathMessage
91
     */
92
    protected function setPathMessage($pathMessage)
93
    {
94
        $this->pathMessage = $pathMessage;
95
    }
96
}
97