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))); |
|
|
|
|
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://a34Dvasdhs.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
|
|
|
$ctrl->addNonExistingError(); |
92
|
|
|
} catch (\Exception $e) { |
93
|
|
|
$errorObject = ($e instanceof \LogicException); |
94
|
|
|
$message = $e->getMessage(); |
95
|
|
|
} finally { |
96
|
|
|
$this->assertTrue($errorObject, $message); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
class MyController |
102
|
|
|
{ |
103
|
|
|
use ErrorTrait; |
104
|
|
|
|
105
|
|
|
public function getContentsFromFile($filename) |
106
|
|
|
{ |
107
|
|
|
if (file_exists($filename)) { |
108
|
|
|
return file_get_contents($filename); |
109
|
|
|
} else { |
110
|
|
|
$this->error(Errno::FILE_NOT_FOUND, $filename); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function getContentsFromFileWithoutFilenameOnError($filename) |
115
|
|
|
{ |
116
|
|
|
if (file_exists($filename)) { |
117
|
|
|
return file_get_contents($filename); |
118
|
|
|
} else { |
119
|
|
|
$this->error(Errno::FILE_NOT_FOUND); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function getContentsFromUrl($url) |
124
|
|
|
{ |
125
|
|
|
$headers = @get_headers($url) || ""; |
126
|
|
|
$exists = stripos($headers[0], "200 OK") ? true : false; |
127
|
|
|
|
128
|
|
|
if ($exists) { |
129
|
|
|
return file_get_contents($url); |
130
|
|
|
} else { |
131
|
|
|
$this->error("URL not found"); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function addNonExistingError() |
136
|
|
|
{ |
137
|
|
|
$this->error(854155); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|