Passed
Push — master ( 8005df...2d92e4 )
by Darío
03:27
created

ErrorTraitTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 85
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testCheckingClassWhenHasErrors() 0 6 1
A testGettingNonStandardErrorsFromClass() 0 9 1
A testGettingStandardErrorsFromClass() 0 23 1
A testNonExistingError() 0 19 2
1
<?php
2
/**
3
 * DronePHP (http://www.dronephp.com)
4
 *
5
 * @link      http://github.com/Pleets/DronePHP
6
 * @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org)
7
 * @license   http://www.dronephp.com/license
8
 * @author    Darío Rivera <[email protected]>
9
 */
10
11
namespace DroneTest\Error;
12
13
use Drone\Error\Errno;
14
use Drone\Error\ErrorTrait;
15
use PHPUnit\Framework\TestCase;
16
17
class ErrorTraitTest extends TestCase
18
{
19
    /**
20
     * Tests if a class has errors
21
     *
22
     * @return null
23
     */
24
    public function testCheckingClassWhenHasErrors()
25
    {
26
        $ctrl = new MyController();
27
        $ctrl->getContentsFromFile('somefile.txt');
28
29
        $this->assertTrue($ctrl->hasErrors());
30
    }
31
32
    /**
33
     * Tests getting standard errors from a class
34
     *
35
     * @return null
36
     */
37
    public function testGettingStandardErrorsFromClass()
38
    {
39
        $ctrl = new MyController();
40
        $ctrl->getContentsFromFile('somefile.txt');
41
42
        # standard error (i.e. exists in Errno class)
43
        $errors = $ctrl->getErrors();
44
45
        $this->assertSame('FILE_NOT_FOUND', Errno::getErrorNameByCode(key($errors)));
0 ignored issues
show
Bug introduced by
It seems like key($errors) can also be of type string; however, parameter $code of Drone\Error\Errno::getErrorNameByCode() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

45
        $this->assertSame('FILE_NOT_FOUND', Errno::getErrorNameByCode(/** @scrutinizer ignore-type */ key($errors)));
Loading history...
46
47
        # message with replacement
48
        $this->assertSame('No such file or directory \'somefile.txt\'', array_shift($errors));
49
50
        $ctrl2 = new MyController();
51
        $ctrl2->getContentsFromFileWithoutFilenameOnError('somefile2.txt');
52
53
        # standard error (i.e. exists in Errno class)
54
        $errors = $ctrl2->getErrors();
55
56
        $this->assertSame('FILE_NOT_FOUND', Errno::getErrorNameByCode(key($errors)));
57
58
        # message without replacement
59
        $this->assertSame('No such file or directory', array_shift($errors));
60
    }
61
62
    /**
63
     * Tests getting non-standard errors from a class
64
     *
65
     * @return null
66
     */
67
    public function testGettingNonStandardErrorsFromClass()
68
    {
69
        $ctrl = new MyController();
70
        $ctrl->getContentsFromUrl('http://a34DvatgaAat4675sfASGag53665yaghaff546ghsfhSDF6767ssfGy.fdassdFSG');
71
72
        # non-standard error (i.e. not exists in Errno class)
73
        $errors = $ctrl->getErrors();
74
75
        $this->assertSame('URL not found', array_shift($errors));
76
    }
77
78
    /**
79
     * Tests wrong use of ErrorTrait
80
     *
81
     * @return null
82
     */
83
    public function testNonExistingError()
84
    {
85
        $ctrl = new MyController();
86
87
        $errorObject = null;
88
        $message = "No exception";
89
90
        try
91
        {
92
            $ctrl->addNonExistingError();
93
        }
94
        catch (\Exception $e)
95
        {
96
            $errorObject = ($e instanceof \LogicException);
97
            $message = $e->getMessage();
98
        }
99
        finally
100
        {
101
            $this->assertTrue($errorObject, $message);
102
        }
103
    }
104
}
105
106
class MyController
107
{
108
    use ErrorTrait;
109
110
    public function getContentsFromFile($filename)
111
    {
112
        if (file_exists($filename))
113
            return file_get_contents($filename);
114
        else
115
            $this->error(Errno::FILE_NOT_FOUND, $filename);
0 ignored issues
show
Bug introduced by
The method error() does not exist on DroneTest\Error\MyController. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

115
            $this->/** @scrutinizer ignore-call */ 
116
                   error(Errno::FILE_NOT_FOUND, $filename);
Loading history...
116
    }
117
118
    public function getContentsFromFileWithoutFilenameOnError($filename)
119
    {
120
        if (file_exists($filename))
121
            return file_get_contents($filename);
122
        else
123
            $this->error(Errno::FILE_NOT_FOUND);
124
    }
125
126
    public function getContentsFromUrl($filename)
127
    {
128
        $headers = get_headers($url);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $url seems to be never defined.
Loading history...
129
        $exists = stripos($headers[0],"200 OK") ? true : false;
130
131
        if ($exists)
132
            return file_get_contents($filename);
133
        else
134
            $this->error("URL not found");
135
    }
136
137
    public function addNonExistingError()
138
    {
139
        $this->error(854155);
140
    }
141
}