Completed
Push — master ( 5e7886...331704 )
by Greg
03:33
created

AAA_RunnerErrorTest::testErrorIsHandled()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 18
nc 1
nop 0
1
<?php
2
use Robo\Robo;
3
4
use Robo\Config\ConfigProcessor;
5
use Robo\Config\YamlConfigLoader;
6
7
/**
8
 * This test must run first. :(
9
 */
10
class AAA_RunnerErrorTest extends \Codeception\TestCase\Test
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
11
{
12
    /**
13
     * @var \Robo\Runner
14
     */
15
    private $runner;
16
17
    /**
18
     * @var \CodeGuy
19
     */
20
    protected $guy;
21
22
    public function _before()
23
    {
24
        $this->runner = new \Robo\Runner('\Robo\RoboFileFixture');
25
    }
26
27
    public function testHandleError()
28
    {
29
        $tmpLevel = error_reporting();
30
31
        $this->assertFalse($this->runner->handleError());
32
        error_reporting(0);
33
        $this->assertTrue($this->runner->handleError());
34
35
        error_reporting($tmpLevel);
36
    }
37
38
    public function testErrorIsHandled()
39
    {
40
        $tmpLevel = error_reporting();
41
42
        // Set error_get_last to a known state.  Note that it can never be
43
        // reset; see http://php.net/manual/en/function.error-get-last.php
44
        @trigger_error('control');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
45
        $error_description = error_get_last();
46
        $this->assertEquals('control', $error_description['message']);
47
        @trigger_error('');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
48
        $error_description = error_get_last();
49
        $this->assertEquals('', $error_description['message']);
50
51
        // Set error_reporting to a non-zero value.  In this instance,
52
        // 'trigger_error' would abort our test script, so we use
53
        // @trigger_error so that execution will continue.  With our
54
        // error handler in place, the value of error_get_last() does
55
        // not change.
56
        error_reporting(E_USER_ERROR);
57
        set_error_handler(array($this->runner, 'handleError'));
58
        @trigger_error('test error', E_USER_ERROR);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
59
        $error_description = error_get_last();
60
        $this->assertEquals('', $error_description['message']);
61
62
        // Set error_reporting to zero.  Now, even 'trigger_error'
63
        // does not abort execution.  The value of error_get_last()
64
        // still does not change.
65
        error_reporting(0);
66
        trigger_error('test error 2', E_USER_ERROR);
67
        $error_description = error_get_last();
68
        $this->assertEquals('', $error_description['message']);
69
70
        error_reporting($tmpLevel);
71
    }
72
}
73