ErrorHandlerTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. /**
  3. * File for ErrorHandler function tests.
  4. * @package Phrity > Util > ErrorHandler
  5. */
  6. declare(strict_types=1);
  7. namespace Phrity\Util;
  8. use ErrorException;
  9. use RuntimeException;
  10. use Phrity\Util\ErrorHandler;
  11. use PHPUnit\Framework\TestCase;
  12. /**
  13. * ErrorHandler test class.
  14. */
  15. class ErrorHandlerTest extends TestCase
  16. {
  17. /**
  18. * Set up for all tests
  19. */
  20. public function setUp(): void
  21. {
  22. error_reporting(-1);
  23. }
  24. public function testSetNull(): void
  25. {
  26. $handler = new ErrorHandler();
  27. $handler->set();
  28. // Verify exception
  29. try {
  30. trigger_error('An error');
  31. } catch (ErrorException $e) {
  32. $this->assertEquals('An error', $e->getMessage());
  33. $this->assertEquals(0, $e->getCode());
  34. $this->assertEquals(E_USER_NOTICE, $e->getSeverity());
  35. $this->assertNull($e->getPrevious());
  36. }
  37. // Restore handler
  38. $this->assertTrue($handler->restore());
  39. }
  40. public function testSetThrowable(): void
  41. {
  42. $handler = new ErrorHandler();
  43. $handler->set(new RuntimeException('A provided exception', 23));
  44. // Verify exception
  45. try {
  46. trigger_error('An error');
  47. } catch (RuntimeException $e) {
  48. $this->assertEquals('A provided exception', $e->getMessage());
  49. $this->assertEquals(23, $e->getCode());
  50. $this->assertNotNull($e->getPrevious());
  51. $prev = $e->getPrevious();
  52. $this->assertEquals('An error', $prev->getMessage());
  53. $this->assertEquals(0, $prev->getCode());
  54. $this->assertEquals(E_USER_NOTICE, $prev->getSeverity());
  55. $this->assertNull($prev->getPrevious());
  56. }
  57. // Restore handler
  58. $this->assertTrue($handler->restore());
  59. }
  60. public function testSetCallback(): void
  61. {
  62. $handler = new ErrorHandler();
  63. $result = null;
  64. $handler->set(function ($error) use (&$result) {
  65. $result = [
  66. 'message' => $error->getMessage(),
  67. 'code' => $error->getCode(),
  68. 'severity' => $error->getSeverity(),
  69. ];
  70. });
  71. // Verify exception
  72. trigger_error('An error');
  73. $this->assertEquals([
  74. 'message' => 'An error',
  75. 'code' => 0,
  76. 'severity' => E_USER_NOTICE,
  77. ], $result);
  78. // Restore handler
  79. $this->assertTrue($handler->restore());
  80. }
  81. public function testWithNull(): void
  82. {
  83. $handler = new ErrorHandler();
  84. $check = false;
  85. // No exception
  86. $result = $handler->with(function () {
  87. return 'Code success';
  88. });
  89. $this->assertEquals('Code success', $result);
  90. // Verify exception
  91. try {
  92. $result = $handler->with(function () use (&$check) {
  93. trigger_error('An error');
  94. $check = true;
  95. return 'Code success';
  96. });
  97. } catch (ErrorException $e) {
  98. $this->assertEquals('An error', $e->getMessage());
  99. $this->assertEquals(0, $e->getCode());
  100. $this->assertEquals(E_USER_NOTICE, $e->getSeverity());
  101. $this->assertNull($e->getPrevious());
  102. }
  103. $this->assertFalse($check);
  104. // Verify that exception is thrown
  105. $this->expectException('ErrorException');
  106. $result = $handler->with(function () {
  107. trigger_error('An error');
  108. return 'Code success';
  109. });
  110. }
  111. public function testWithThrowable(): void
  112. {
  113. $handler = new ErrorHandler();
  114. $check = false;
  115. // No exception
  116. $result = $handler->with(function () {
  117. return 'Code success';
  118. });
  119. $this->assertEquals('Code success', $result);
  120. // Verify exception
  121. try {
  122. $result = $handler->with(function () use (&$check) {
  123. trigger_error('An error');
  124. $check = true;
  125. return 'Code success';
  126. }, new RuntimeException('A provided exception', 23));
  127. } catch (RuntimeException $e) {
  128. $this->assertEquals('A provided exception', $e->getMessage());
  129. $this->assertEquals(23, $e->getCode());
  130. $this->assertNotNull($e->getPrevious());
  131. $prev = $e->getPrevious();
  132. $this->assertEquals('An error', $prev->getMessage());
  133. $this->assertEquals(0, $prev->getCode());
  134. $this->assertEquals(E_USER_NOTICE, $prev->getSeverity());
  135. $this->assertNull($prev->getPrevious());
  136. }
  137. $this->assertFalse($check);
  138. // Verify that exception is thrown
  139. $this->expectException('RuntimeException');
  140. $result = $handler->with(function () {
  141. trigger_error('An error');
  142. return 'Code success';
  143. }, new RuntimeException('A provided exception', 23));
  144. }
  145. public function testWithCallback(): void
  146. {
  147. $handler = new ErrorHandler();
  148. $check = false;
  149. // No error invoked
  150. $result = $handler->with(function () {
  151. return 'Code success';
  152. }, function ($error) {
  153. return $error;
  154. });
  155. $this->assertEquals('Code success', $result);
  156. // An error is invoked
  157. $result = $handler->with(function () use (&$check) {
  158. trigger_error('An error');
  159. $check = true;
  160. return 'Code success';
  161. }, function ($error) {
  162. return $error;
  163. });
  164. $this->assertFalse($check);
  165. $this->assertEquals('An error', $result->getMessage());
  166. $this->assertEquals(0, $result->getCode());
  167. $this->assertEquals(E_USER_NOTICE, $result->getSeverity());
  168. $this->assertNull($result->getPrevious());
  169. }
  170. public function testWithAllNull(): void
  171. {
  172. $handler = new ErrorHandler();
  173. $check = false;
  174. // No error invoked
  175. $result = $handler->withAll(function () {
  176. return 'Code success';
  177. });
  178. $this->assertEquals('Code success', $result);
  179. // Verify exception
  180. try {
  181. $result = $handler->withAll(function () use (&$check) {
  182. trigger_error('An error');
  183. $check = true;
  184. return 'Code success';
  185. });
  186. } catch (ErrorException $e) {
  187. $this->assertEquals('An error', $e->getMessage());
  188. $this->assertEquals(0, $e->getCode());
  189. $this->assertEquals(E_USER_NOTICE, $e->getSeverity());
  190. $this->assertNull($e->getPrevious());
  191. }
  192. $this->assertTrue($check);
  193. // Verify that exception is thrown
  194. $this->expectException('ErrorException');
  195. $result = $handler->withAll(function () {
  196. trigger_error('An error');
  197. return 'Code success';
  198. });
  199. }
  200. public function testWithAllThrowable(): void
  201. {
  202. $handler = new ErrorHandler();
  203. $check = false;
  204. // No exception
  205. $result = $handler->withAll(function () {
  206. return 'Code success';
  207. });
  208. $this->assertEquals('Code success', $result);
  209. // Verify exception
  210. try {
  211. $result = $handler->withAll(function () use (&$check) {
  212. trigger_error('An error');
  213. $check = true;
  214. return 'Code success';
  215. }, new RuntimeException('A provided exception', 23));
  216. } catch (RuntimeException $e) {
  217. $this->assertEquals('A provided exception', $e->getMessage());
  218. $this->assertEquals(23, $e->getCode());
  219. $this->assertNotNull($e->getPrevious());
  220. $prev = $e->getPrevious();
  221. $this->assertEquals('An error', $prev->getMessage());
  222. $this->assertEquals(0, $prev->getCode());
  223. $this->assertEquals(E_USER_NOTICE, $prev->getSeverity());
  224. $this->assertNull($prev->getPrevious());
  225. }
  226. $this->assertTrue($check);
  227. // Verify that exception is thrown
  228. $this->expectException('RuntimeException');
  229. $result = $handler->withAll(function () {
  230. trigger_error('An error');
  231. return 'Code success';
  232. }, new RuntimeException('A provided exception', 23));
  233. }
  234. public function testWithAllCallback(): void
  235. {
  236. $handler = new ErrorHandler();
  237. $check = false;
  238. // No error invoked
  239. $result = $handler->withAll(function () {
  240. return 'Code success';
  241. }, function ($error, $result) {
  242. return $error;
  243. });
  244. $this->assertEquals('Code success', $result);
  245. // An error is invoked
  246. $result = $handler->withAll(function () use (&$check) {
  247. trigger_error('An error');
  248. trigger_error('Another error', E_USER_WARNING);
  249. $check = true;
  250. return 'Code success';
  251. }, function ($errors, $result) {
  252. return ['errors' => $errors, 'result' => $result];
  253. });
  254. $this->assertTrue($check);
  255. $this->assertEquals('Code success', $result['result']);
  256. $this->assertEquals('An error', $result['errors'][0]->getMessage());
  257. $this->assertEquals(0, $result['errors'][0]->getCode());
  258. $this->assertEquals(E_USER_NOTICE, $result['errors'][0]->getSeverity());
  259. $this->assertNull($result['errors'][0]->getPrevious());
  260. $this->assertEquals('Another error', $result['errors'][1]->getMessage());
  261. $this->assertEquals(0, $result['errors'][1]->getCode());
  262. $this->assertEquals(E_USER_WARNING, $result['errors'][1]->getSeverity());
  263. $this->assertNull($result['errors'][1]->getPrevious());
  264. }
  265. }