Pleets /
dronephp
| 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\Exception\Exception as ExceptionException; |
||
| 14 | use PHPUnit\Framework\TestCase; |
||
| 15 | |||
| 16 | class ExceptionTest extends TestCase |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Tests if an exception could be stored |
||
| 20 | * |
||
| 21 | * @return null |
||
| 22 | */ |
||
| 23 | public function testExceptionStoring() |
||
| 24 | { |
||
| 25 | try { |
||
| 26 | throw new ExceptionException("This is an storable exception"); |
||
| 27 | } catch (ExceptionException $e) { |
||
| 28 | mkdir('exceptions'); |
||
| 29 | |||
| 30 | $file = 'exceptions/exception-' . date('dmY') . '.json'; |
||
| 31 | $storage = new \Drone\Exception\Storage($file); |
||
| 32 | |||
| 33 | $errorCode = $storage->store($e); |
||
| 34 | |||
| 35 | $this->assertTrue(is_string($errorCode)); |
||
| 36 | |||
| 37 | $json_object = (file_exists($file)) ? json_decode(file_get_contents($file)) : []; |
||
| 38 | $array_object = \Drone\Util\ArrayDimension::objectToArray($json_object); |
||
| 39 | |||
| 40 | $error_qty = array_keys($array_object); |
||
| 41 | |||
| 42 | $this->assertEquals(1, count($error_qty)); |
||
| 43 | |||
| 44 | $firstError = array_shift(array_keys($array_object)); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 45 | $unserialized = unserialize($array_object[$firstError]["object"]); |
||
| 46 | |||
| 47 | $this->assertEquals("This is an storable exception", $unserialized->getMessage()); |
||
| 48 | } |
||
| 49 | |||
| 50 | $shell = new \Drone\FileSystem\Shell(); |
||
| 51 | $shell->rm('exceptions', true); |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Tests if an exception could be stored when the file target does not exists |
||
| 56 | * |
||
| 57 | * @return null |
||
| 58 | */ |
||
| 59 | public function testExceptionStoringFail() |
||
| 60 | { |
||
| 61 | try { |
||
| 62 | throw new ExceptionException("This is an storable exception too"); |
||
| 63 | } catch (ExceptionException $e) { |
||
| 64 | $date = date('dmY'); |
||
| 65 | $file = 'nofolder/exception-' . $date . '.json'; |
||
| 66 | $storage = new \Drone\Exception\Storage($file); |
||
| 67 | |||
| 68 | $errorCode = $storage->store($e); |
||
| 69 | |||
| 70 | $this->assertNotTrue(is_string($errorCode)); |
||
| 71 | |||
| 72 | $errors = $storage->getErrors(); |
||
| 73 | $error = array_shift($errors); |
||
| 74 | |||
| 75 | $this->assertEquals("No such file or directory 'nofolder/'", $error); |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Tests if several exceptions could be stored |
||
| 81 | * |
||
| 82 | * @return null |
||
| 83 | */ |
||
| 84 | public function testCumulativeExceptionStoring() |
||
| 85 | { |
||
| 86 | mkdir('exceptions'); |
||
| 87 | $date = date('dmY'); |
||
| 88 | |||
| 89 | try { |
||
| 90 | throw new ExceptionException("This is an storable exception"); |
||
| 91 | } catch (ExceptionException $e) { |
||
| 92 | $file = 'exceptions/exception-' . $date . '.json'; |
||
| 93 | $storage = new \Drone\Exception\Storage($file); |
||
| 94 | $storage->store($e); |
||
| 95 | } |
||
| 96 | |||
| 97 | try { |
||
| 98 | throw new ExceptionException("This is an storable exception too"); |
||
| 99 | } catch (ExceptionException $e) { |
||
| 100 | $file = 'exceptions/exception-' . $date . '.json'; |
||
| 101 | $storage = new \Drone\Exception\Storage($file); |
||
| 102 | $storage->store($e); |
||
| 103 | |||
| 104 | $json_object = (file_exists($file)) ? json_decode(file_get_contents($file)) : []; |
||
| 105 | $array_object = \Drone\Util\ArrayDimension::objectToArray($json_object); |
||
| 106 | |||
| 107 | $error_qty = array_keys($array_object); |
||
| 108 | |||
| 109 | $this->assertEquals(2, count($error_qty)); |
||
| 110 | |||
| 111 | $errorKeys = array_keys($array_object); |
||
| 112 | |||
| 113 | $firstError = array_shift($errorKeys); |
||
| 114 | $secondError = array_shift($errorKeys); |
||
| 115 | |||
| 116 | $firstUnserialized = unserialize($array_object[$firstError]["object"]); |
||
| 117 | $secondUnserialized = unserialize($array_object[$secondError]["object"]); |
||
| 118 | |||
| 119 | $this->assertEquals("This is an storable exception", $firstUnserialized->getMessage()); |
||
| 120 | $this->assertEquals("This is an storable exception too", $secondUnserialized->getMessage()); |
||
| 121 | } |
||
| 122 | |||
| 123 | $shell = new \Drone\FileSystem\Shell(); |
||
| 124 | $shell->rm('exceptions', true); |
||
| 125 | } |
||
| 126 | } |
||
| 127 |