1
|
|
|
<?php |
2
|
|
|
use Robo\Result; |
3
|
|
|
use Robo\Exception\TaskExitException; |
4
|
|
|
|
5
|
|
|
class ResultTest extends \Codeception\TestCase\Test { |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @var \CodeGuy |
9
|
|
|
*/ |
10
|
|
|
protected $guy; |
11
|
|
|
|
12
|
|
|
public function testBasics() |
13
|
|
|
{ |
14
|
|
|
$task = new ResultDummyTask(); |
15
|
|
|
$result = new Result($task, 1, 'The foo barred', ['time' => 10]); |
16
|
|
|
|
17
|
|
|
$this->guy->seeInOutput('The foo barred'); |
18
|
|
|
$this->guy->seeInOutput('Exit code 1'); |
19
|
|
|
$this->guy->seeInOutput('10s'); |
20
|
|
|
$this->guy->seeInOutput('[ResultDummyTask]'); |
21
|
|
|
|
22
|
|
|
$this->assertSame($task, $result->getTask()); |
23
|
|
|
$this->assertEquals(1, $result->getExitCode()); |
24
|
|
|
$this->assertEquals('The foo barred', $result->getMessage()); |
25
|
|
|
$data = $result->getData(); |
26
|
|
|
$this->assertEquals(10, $data['time']); |
27
|
|
|
|
28
|
|
|
$taskClone = $result->cloneTask(); |
29
|
|
|
$this->assertNotSame($task, $taskClone); |
30
|
|
|
$this->assertInstanceOf('Robo\Contract\TaskInterface', $taskClone); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testArrayAccess() |
34
|
|
|
{ |
35
|
|
|
$task = new ResultDummyTask(); |
36
|
|
|
$result = new Result($task, 1, 'The foo barred', ['time' => 10]); |
37
|
|
|
$this->assertEquals($result['time'], 10); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testStopOnFail() |
41
|
|
|
{ |
42
|
|
|
$exceptionClass = false; |
43
|
|
|
$task = new ResultDummyTask(); |
44
|
|
|
|
45
|
|
|
Result::$stopOnFail = true; |
46
|
|
|
$result = Result::success($task, "Something that worked"); |
47
|
|
|
try { |
48
|
|
|
$result = Result::error($task, "Something that did not work"); |
49
|
|
|
// stopOnFail will cause Result::error() to throw an exception, |
50
|
|
|
// so we will never get here. If we did, the assert below would fail. |
51
|
|
|
$this->assertTrue($result->wasSuccessful()); |
52
|
|
|
$this->assertTrue(false); |
53
|
|
|
} catch (\Exception $e) { |
54
|
|
|
$exceptionClass = get_class($e); |
55
|
|
|
} |
56
|
|
|
$this->assertEquals(TaskExitException::class, $exceptionClass); |
57
|
|
|
$this->assertTrue($result->wasSuccessful()); |
58
|
|
|
|
59
|
|
|
/* |
60
|
|
|
// This gives an error: |
61
|
|
|
// Exception of class Robo\Exception\TaskExitException expected to |
62
|
|
|
// be thrown, but PHPUnit_Framework_Exception caught |
63
|
|
|
// This happens whether or not the expected exception is thrown |
64
|
|
|
$this->guy->expectException(TaskExitException::class, function() { |
65
|
|
|
// $result = Result::error($task, "Something that did not work"); |
66
|
|
|
$result = Result::success($task, "Something that worked"); |
67
|
|
|
}); |
68
|
|
|
*/ |
69
|
|
|
|
70
|
|
|
Result::$stopOnFail = false; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
class ResultDummyTask implements \Robo\Contract\TaskInterface |
75
|
|
|
{ |
76
|
|
|
public function run() |
77
|
|
|
{ |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|