@@ -66,7 +66,7 @@ public function testRunWithSuccess()
66
66
'start ' => date ('Y-m-d H:i:s ' ),
67
67
'duration ' => '0.00 ' ,
68
68
'output ' => null ,
69
- 'error ' => serialize ( null ) ,
69
+ 'error ' => null ,
70
70
],
71
71
];
72
72
$ this ->seeInDatabase ('settings ' , [
@@ -79,6 +79,53 @@ public function testRunWithSuccess()
79
79
]);
80
80
}
81
81
82
+ /**
83
+ * @throws ReflectionException
84
+ */
85
+ public function testRunWithError ()
86
+ {
87
+ $ task1 = (new Task ('closure ' , static function () {
88
+ echo 'Task 1 ' ;
89
+ }))->daily ('12:05am ' )->named ('task1 ' );
90
+ $ task3 = (new Task ('closure ' , static function () {
91
+ throw new Exception ('Example exception in Task 3 ' );
92
+ }))->daily ('12:00am ' )->named ('task3 ' );
93
+
94
+ $ runner = $ this ->getRunner ([$ task1 , $ task3 ]);
95
+
96
+ ob_start ();
97
+ $ runner ->withTestTime ('12:00am ' )->run ();
98
+ $ output = ob_get_clean ();
99
+
100
+ // Only task 3 should have run
101
+ $ this ->assertSame ('' , $ output );
102
+
103
+ // Get info about the exception
104
+ $ reflection = new ReflectionFunction ($ task3 ->getAction ());
105
+ $ file = $ reflection ->getFileName ();
106
+ $ line = $ reflection ->getStartLine () + 1 ;
107
+
108
+ // Should have logged the stats
109
+ $ expected = [
110
+ [
111
+ 'task ' => 'task3 ' ,
112
+ 'type ' => 'closure ' ,
113
+ 'start ' => date ('Y-m-d H:i:s ' ),
114
+ 'duration ' => '0.00 ' ,
115
+ 'output ' => null ,
116
+ 'error ' => "Exception: 0 - Example exception in Task 3 \nfile: {$ file }: {$ line }" ,
117
+ ],
118
+ ];
119
+ $ this ->seeInDatabase ('settings ' , [
120
+ 'class ' => 'CodeIgniter\Tasks\Config\Tasks ' ,
121
+ 'key ' => 'log-task3 ' ,
122
+ 'value ' => serialize ($ expected ),
123
+ ]);
124
+ $ this ->dontSeeInDatabase ('settings ' , [
125
+ 'key ' => 'log-task1 ' ,
126
+ ]);
127
+ }
128
+
82
129
protected function getRunner (array $ tasks = [])
83
130
{
84
131
$ scheduler = service ('scheduler ' );
0 commit comments