Passed
Push — master ( 863f0a...b0d82b )
by Darío
01:40
created

ExceptionTest::testExceptionStoring()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 17
nc 2
nop 0
dl 0
loc 32
rs 9.7
c 0
b 0
f 0
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
        {
27
            throw new ExceptionException("This is an storable exception");
28
        }
29
        catch (ExceptionException $e)
30
        {
31
            mkdir('exceptions');
32
33
            $file = 'exceptions/exception-' . date('dmY') . '.json';
34
            $storage = new \Drone\Exception\Storage($file);
35
36
            $errorCode = $storage->store($e);
37
38
            $this->assertTrue(is_string($errorCode));
39
40
            $json_object  = (file_exists($file)) ? json_decode(file_get_contents($file)) : array();
41
            $array_object = \Drone\Util\ArrayDimension::objectToArray($json_object);
42
43
            $error_qty = array_keys($array_object);
44
45
            $this->assertEquals(1, count($error_qty));
46
47
            $firstError = array_shift(array_keys($array_object));
0 ignored issues
show
Bug introduced by
array_keys($array_object) cannot be passed to array_shift() as the parameter $array expects a reference. ( Ignorable by Annotation )

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

47
            $firstError = array_shift(/** @scrutinizer ignore-type */ array_keys($array_object));
Loading history...
48
            $unserialized = unserialize($array_object[$firstError]["object"]);
49
50
            $this->assertEquals("This is an storable exception", $unserialized->getMessage());
51
        }
52
53
        $shell = new \Drone\FileSystem\Shell();
54
        $shell->rm('exceptions', true);
55
    }
56
57
    /**
58
     * Tests if an exception could be stored when the file target does not exists
59
     *
60
     * @return null
61
     */
62
    public function testExceptionStoringFail()
63
    {
64
        try
65
        {
66
            throw new ExceptionException("This is an storable exception too");
67
        }
68
        catch (ExceptionException $e)
69
        {
70
            $date = date('dmY');
71
            $file = 'nofolder/exception-' . $date . '.json';
72
            $storage = new \Drone\Exception\Storage($file);
73
74
            $errorCode = $storage->store($e);
75
76
            $this->assertNotTrue(is_string($errorCode));
77
78
            $errors = $storage->getErrors();
79
            $error = array_shift($errors);
80
81
            $this->assertEquals("No such file or directory 'nofolder/'", $error);
82
        }
83
    }
84
85
    /**
86
     * Tests if several exceptions could be stored
87
     *
88
     * @return null
89
     */
90
    public function testCumulativeExceptionStoring()
91
    {
92
        mkdir('exceptions');
93
        $date = date('dmY');
94
95
        try
96
        {
97
            throw new ExceptionException("This is an storable exception");
98
        }
99
        catch (ExceptionException $e)
100
        {
101
            $file = 'exceptions/exception-' . $date . '.json';
102
            $storage = new \Drone\Exception\Storage($file);
103
            $storage->store($e);
104
        }
105
106
        try
107
        {
108
            throw new ExceptionException("This is an storable exception too");
109
        }
110
        catch (ExceptionException $e)
111
        {
112
            $file = 'exceptions/exception-' . $date . '.json';
113
            $storage = new \Drone\Exception\Storage($file);
114
            $storage->store($e);
115
116
            $json_object  = (file_exists($file)) ? json_decode(file_get_contents($file)) : array();
117
            $array_object = \Drone\Util\ArrayDimension::objectToArray($json_object);
118
119
            $error_qty = array_keys($array_object);
120
121
            $this->assertEquals(2, count($error_qty));
122
123
            $errorKeys = array_keys($array_object);
124
125
            $firstError  = array_shift($errorKeys);
126
            $secondError = array_shift($errorKeys);
127
128
            $firstUnserialized  = unserialize($array_object[$firstError]["object"]);
129
            $secondUnserialized = unserialize($array_object[$secondError]["object"]);
130
131
            $this->assertEquals("This is an storable exception", $firstUnserialized->getMessage());
132
            $this->assertEquals("This is an storable exception too", $secondUnserialized->getMessage());
133
        }
134
135
        $shell = new \Drone\FileSystem\Shell();
136
        $shell->rm('exceptions', true);
137
    }
138
}